Search code examples
phphtmlbuttoneditsql-delete

Being able to delete data from the click of a button on a page which also deletes the data from the database


I have multiple text boxes within a table on my web page which is populated from a form on my website users fill out. I have the feature of being able to delete each row as well as edit each row of data displayed on my website. The problem I'm having with it is only the last row of the table can be edited/deleted. For example, When I click the delete button on the first row of the table, it deletes the last row for some reason and not the first row. Also, it's the same with the update/edit button, only the last row can be modified and not anything above the last row of the table on my website.

More information: form_id is the primary key within my database.

My code:

 <?php
  $con = @mysql_connect("localhost","root","");
  if (!$con){
  die("Can not connect: " . mysql_error());
  }
  mysql_select_db("formsystem", $con);

  if(isset($_POST['update'])){
    $UpdateQuery = "UPDATE form SET form_name='$_POST[name]', form_description='$_POST[description]' WHERE form_id='$_POST[hidden]'";               
    mysql_query($UpdateQuery, $con);
};

if(isset($_POST['delete'])){
$DeleteQuery = "DELETE FROM form WHERE form_id='$_POST[hidden]'";          
mysql_query($DeleteQuery, $con);
};

  $sql = "SELECT * FROM form";

  $myData = mysql_query($sql,$con);

   echo "<table>
   <tr>
   <th>Title</th>
   <th>Description</th>
   <th></th>
   <th></th>
   <th></th>
   </tr>";
   while($record = mysql_fetch_array($myData)){
   echo "<form action=findGroup.php method=post>";
   echo "<tr>";
   echo "<td>" ."<input type=text name=name value='" . $record['form_name'] . "'/> </td>";
   echo "<td>" ."<input type=text name=description value='" . $record['form_description'] . "'/> </td>";
   echo "<td>" ."<input type=hidden name=hidden value='" . $record['form_id'] . "'/></td>";
   echo "<td>" ."<input type=submit name=update value='update" . "'/> </td>";
   echo "<td>" ."<input type=submit name=delete value='delete" . "'/> </td>";
   echo "</tr>";
  }
  echo "</table>";

?>

Solution

  • Update

    Enclose the form element properly:

    <?php
      $con = @mysql_connect("localhost","root","");
      if (!$con){
        die("Can not connect: " . mysql_error());
      }
        mysql_select_db("formsystem", $con);
    
        if(isset($_POST['update'])){
          $UpdateQuery = "UPDATE form SET form_name='".$_POST['name']."', form_description='".$_POST['description']."' WHERE form_id='".$_POST['hidden']."';";               
        mysql_query($UpdateQuery, $con);
      };
    
      if(isset($_POST['delete'])){
        $DeleteQuery = "DELETE FROM form WHERE form_id='".$_POST['hidden']."';";          
        mysql_query($DeleteQuery, $con);
      };
    
      $sql = "SELECT * FROM form";
    
      $myData = mysql_query($sql,$con);
    
      echo "<table>
       <tr>
       <th>Title</th>
       <th>Description</th>
       <th></th>
       <th></th>
       <th></th>
       </tr>";
       while($record = mysql_fetch_array($myData)){
         echo "<form action=findGroup.php method=post>";
         echo "<tr>";
         echo "<td>" ."<input type=text name=name value='" . $record['form_name'] . "'/> </td>";
         echo "<td>" ."<input type=text name=description value='" . $record['form_description'] . "'/> </td>";
         echo "<td>" ."<input type=hidden name=hidden value='" . $record['form_id'] . "'/></td>";
         echo "<td>" ."<input type=submit name=update value='update" . "'/> </td>";
         echo "<td>" ."<input type=submit name=delete value='delete" . "'/> </td>";
         echo "</tr>"
         echo "</form>";
       }
      echo "</table>";
    
    ?>
    

    And for security issue, it's better to wrap variable using mysqli_real_escape_string, for example:

    "DELETE FROM form WHERE form_id='".mysqli_real_escape_string($_POST['hidden'])."';";
    

    But this is another question, here is the thread.