Hi Everyone
I'm fairly new to PHP and struggling a bit. I'm trying to delete some records from MySQL via php and checkboxes my code is below if anyone can help me. Thanks in advance
<?php
include("config.php");
include("contentdb.php");
$id= $_POST["id"];
$question= $_POST["question"];
$opt1= $_POST["opt1"];
$opt2= $_POST["opt2"];
$opt3= $_POST["opt3"];
$answer= $_POST["answer"];
$Query="INSERT into qtable (id,question,opt1,opt2,opt3,answer)
values ('$id','$question', '$opt1' , '$opt2', '$opt3' , '$answer')";
$result = mysql_query($Query) ;
if (!$result)
{
print ("Your information has been passed into current database!<BR>\n");
} else {
print ("The query could not be executed for inserting your data!<BR>");
}
$query = "SELECT * FROM qtable ORDER BY id";
$result = mysql_query($query)
or die ("Couldn't execute query for collecting your data.");
/* Display results in a table */
print ("<TABLE BORDER=1 WIDTH=\"90%\" CELLSPACING=2 CELLPADDING=2 ALIGN=CENTER>\n");
print ("<TR ALIGN=CENTER onmouseover=blue>\n");
print ("<TD ALIGN=CENTER ><b><font color=red>No</font></TD>\n");
print ("<TD ALIGN=CENTER ><b><font color=red>Question</font></TD>\n");
print ("<TD ALIGN=CENTER ><b><font color=red>Option 1</font></TD>\n");
print ("<TD ALIGN=CENTER ><b><font color=red>Option 2</font></TD>\n");
print ("<TD ALIGN=CENTER ><b><font color=red>Option 3</font></TD>\n");
print ("<TD ALIGN=CENTER ><b><font color=red>Answer</font></TD>\n");
print ("<TD ALIGN=CENTER ><font color=blue size=2>Del/Edit</font></TD>\n");
print ("</TR>\n");
if ($result = mysql_query($query)) { // see if any rows were returned
if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_array($result)) {
extract($row);
print ("<TR ALIGN=CENTER >\n");
print( "<TD ALIGN=CENTER >$row[id]</TD>\n");
print ("<TD ALIGN=CENTER >$row[question]</TD>\n");
print ("<TD ALIGN=CENTER >$row[opt1]</TD>\n");
print ("<TD ALIGN=CENTER >$row[opt2]</TD>\n");
print ("<TD ALIGN=CENTER >$row[opt3]</TD>\n");
print ("<TD ALIGN=CENTER >$row[answer]</TD>\n");
print ("<TD ALIGN=CENTER ><input type=checkbox name=delete value=\"$id\"></font></TD>\n");
print ("</tr>");
}
mysql_free_result($result);
} else {
echo "Error in query: $query. ".mysql_error(); // print error message
}
}
print ("</TABLE>\n");
echo "<input type=submit value=Delete > ";
$id= $_POST["id"];
if (isset($Delete)) {
$toDelete = implode(', ', $_POST['id']);
$query = "DELETE FROM `qtable` WHERE ID IN ($toDelete)";
if(mysql_query($query)) {
echo 'Hooray, the filthy rows were deleted';
}
else echo 'Bad luck schmuck<br>' . mysql_error();
}
// close connection
mysql_close($db);
?>