Hey all this is a pretty simple question, basically I connect to a mysql database, and iterate a list of names like this:

$result = mysql_query("SELECT * FROM Students");

echo "<table border=\"1\">";

while($row = mysql_fetch_array($result))
  {
  print "
    <tr>
         <td>
  " 
  . $row['Student'] .
  "
        </td>
        <td>
  <form name=\"FRMdelete" . $row['Student'] . "\" action=\"deletestudent.php\" method=\"POST\" style=\"margin-bottom:0;\">
  <input name=\"delete\" type=\"submit\" id=\"" . $row['Student'] . "\" value=\"Delete\" OnClick=\"return confirm('Are you sure you want to Delete "  . $row['Student'] . "?')\">
  </form>
         </td>
     </tr>
  ";
  }
echo"</table>";
mysql_close($con);

Then I using POST, I pass the name of the student from the ID of the input to deletestudent.php, which looks like this:

<?php
$studentremoved = $_POST['delete'];
$sql = "DELETE FROM Students WHERE Student='" . $studentremoved . "'";
$con = mysql_connect("localhost","root","password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_query($sql);
mysql_close($con);
header("Location: ".$_SERVER['HTTP_REFERER']);  
?>

For some reason the scripts execute without throwing and errors to my debugger, but it doesn't delete the record =/

What am I missing, I know its probably obvious. Sorry for the sloppy code, I am a beginner =/

Dani AI

Generated

Quick summary for : two very common issues explain why nothing was deleted. First, an element's id is never sent with the form — only inputs with a name (and their value) are posted. In your form the submit button is named delete and its value is "Delete", so $_POST['delete'] will be the string "Delete", not the student name. Second, your deletion script opens a connection but does not select a database before running the query, which will also prevent the DELETE from affecting the intended table.

Build on ’s suggestion by posting a proper identifier (preferably the numeric primary key) in a hidden field and, as suggested, confirm the DB user has DELETE privileges. For debugging: temporarily remove the header redirect, do a var_dump($_POST) (or log it), and check the DB error after the query (or use PDO with exceptions) so you see any MySQL error messages.

Example approach (send a student id from the row, and use a prepared statement on the server):

<form method="post" action="deletestudent.php">
  <input type="hidden" name="student_id" value="<?php echo (int)$row['id']; ?>">
  <input type="submit" name="delete" value="Delete">
</form>
<?php
// deletestudent.php (PDO example)
$id = isset($_POST['student_id']) ? (int)$_POST['student_id'] : 0;
if ($id) {
  $pdo = new PDO('mysql:host=localhost;dbname=your_db;charset=utf8mb4','user','pass',[PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION]);
  $stmt = $pdo->prepare('DELETE FROM Students WHERE id = :id');
  $stmt->execute([':id'=>$id]);
}
header('Location: ' . $_SERVER['HTTP_REFERER']);

Quick checklist: confirm the right POST key is sent, use an id rather than a name, escape or use prepared statements to avoid SQL injection, select the correct database before querying, and temporarily disable the redirect so you can see errors.

Recommended Answers

All 3 Replies

Your form should look like this:

<form ...>
  <input type="hidden" name="student" value="$row['student']" />
  <input type="submit" name="delete" value="Delete" />
</form>

Then you can get which student to delete on deletestudent.php via $_POST. (you can use isset($_POST['delete']) to see if they clicked "Delete" but $_POST will hold the value "Delete").

The id attribute in html is used for javascript or CSS to select that particular element.

Something simple but have you given your database user permissions to delete?

Im also an old newbie and still trying to get my head round it all. lol

Might also be a good idea to do

if (!mysql_query($sql)){
  die (mysql_error());
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.