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 "
  . $row['Student'] .
  <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'] . "?')\">

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

$studentremoved = $_POST['delete'];
$sql = "DELETE FROM Students WHERE Student='" . $studentremoved . "'";
$con = mysql_connect("localhost","root","password");
if (!$con)
  die('Could not connect: ' . mysql_error());
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 =/

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" />

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, learning, and sharing knowledge.