Search code examples
mysqldelete-rowcorresponding-records

Identificate row's id and delete the object from db


I'm a beginner in web development. I made a simple table that shows the id,answer and date of notes that I insert into the db.I need a simple script that shows a delete button for each note. I would like to know how can I get the id of each row I need to delete. Here's my php code:

<html>
<body>
<?php
 include '..//config.php';
 $result = mysql_query("SELECT * FROM **** Order by `id` ASC");
echo "<center><table width='400' border='1'>
<tr>
<th bgcolor='#D6D6D6' >ID</th>
<th bgcolor='#D6D6D6'>Answer</th>
<th bgcolor='#D6D6D6'>Date</th>
<th bgcolor='#D6D6D6'>Preview</th>
<th bgcolor='#D6D6D6'>Delete</th>
</tr>";


while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td bgcolor='#3399FF' scope='col width ='10%'>" . $row['id'] . "</td>";
  echo "<td width ='10%'>" . $row['answer'] . "</td>";
  echo "<td  bgcolor='#3399FF' scope='col width ='10%'>" . $row['date'] . " </td>";   
     if ($row['hidden'] > 'http://www.********/upload/upload/') {
  echo "<td width ='10%'><a href =" . $row['hidden'] . "><center>View</center></a></td>"; } else {echo "<td width ='10%'>" . $row[''] . "</td>";}
   echo "<td width ='10%'><a href = 'dlt.php'>Delete</a></td>";
   $row_id = $row['id'];
          }
  echo "</tr width ='10%'>"; 
  echo "</table></center>";
?>
</body>
</html>

Solution

  • It's not very clear from your question what your requirements are but I am assuming you want to know how you can pass the id of the entry when you click the "Delete" link (which is linked to dlt.php).

    You can pass a query string parameter to dlt.php with the id of the entry.

    I have cleaned up your code and modified it below:

    <!Doctype html>
    <html>
        <body>
        <?php
            include '../config.php';
            $result = mysql_query("SELECT * FROM **** Order by `id` ASC");
        ?>
            <center>
                <table width='400' border='1'>
                    <tr>
                        <th bgcolor='#D6D6D6'>ID</th>
                        <th bgcolor='#D6D6D6'>Answer</th>
                        <th bgcolor='#D6D6D6'>Date</th>
                        <th bgcolor='#D6D6D6'>Preview</th>
                        <th bgcolor='#D6D6D6'>Delete</th>
                    </tr>
    
                    <?php
                        while ($row = mysql_fetch_array($result)) {
    
                            $row_id = $row['id'];
                            ?>
                            <tr>
                                <td bgcolor='#3399FF' scope='col' width='10%'><?php echo $row_id; ?></td>
                                <td width='10%'><?php $row['answer']; ?></td>
                                <td  bgcolor='#3399FF' scope='col' width ='10%'><?php $row['date']; ?></td>
                                <td width ='10%'>
                                    <?php
                                        // I am not exactly sure what this line was meant to do in your code
                                        if ($row['hidden'] > 'http://www.********/upload/upload/') {
                                            echo "<a href='" . $row['hidden'] . "'><center>View</center></a>";
                                        }
                                    ?>
                                </td>
                                <td width ='10%'><a href='dlt.php?id=<?php echo $row_id; ?>'>Delete</a></td>
    
                            </tr>
    
                            <?php
                        }
    
                    ?>
                </table>
            </center>
        </body>
    </html>
    

    Then in your dlt.php file you can retrieve the id to be deleted using the following line:

    if (isset($_GET['id']) {
        $delete_id = $_GET['id'];
    }
    else {
        // no 'id' has been passed.
        die('Nothing to delete!');
    }
    $result = mysql_query("DELETE FROM **** WHERE `id` = $delete_id");
    

    Since you are new to web development (or development in general) you should be very careful about a couple of things:

    1. Write clean code with proper indentations. It helps you spot and debug your problems very quickly.
    2. Write W3C standards compliant code.
    3. Understand the technology API you want to use and beware of deprecated functions. For example in PHP mysql_* functions are deprecated. You should use mysqli or PDO instead.

    I'd put in more points but the list can actually get very long. These should be good for starters.