| | |
How to delete using checkboxes
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Dec 2006
Posts: 6
Reputation:
Solved Threads: 0
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]
<?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);
?>
[/PHP]
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]
<?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);
?>
[/PHP]
•
•
Join Date: Jul 2004
Posts: 494
Reputation:
Solved Threads: 21
Ok, what is your problem, exactly?
You should always quote your HTML properly. Don't skip out on the double quotes because PHP's echo/print statements don't play nice. Escape your double quotes with \"
I had problems with editing values from checkboxes because I skipped out on the quotes.
You should always quote your HTML properly. Don't skip out on the double quotes because PHP's echo/print statements don't play nice. Escape your double quotes with \"
I had problems with editing values from checkboxes because I skipped out on the quotes.
www.uncreativelabs.net
Old computers are getting to be a lost art. Here at Uncreative Labs, we still enjoy using the old computers. Sometimes we want to see how far a particular system can go, other times we use a stock system to remind ourselves of what we once had.
Old computers are getting to be a lost art. Here at Uncreative Labs, we still enjoy using the old computers. Sometimes we want to see how far a particular system can go, other times we use a stock system to remind ourselves of what we once had.
•
•
Join Date: Oct 2006
Posts: 42
Reputation:
Solved Threads: 4
Well unless I am beening really blind (which is not unusal as I am only on my second coffee of the day).
You do not seem to have a form tag, so when you are clicking the submit button the browser has no idea what to do. I guess that you are trying to load the page on to itself so try wrapping the input fields in
<form method="post" action="<?= $_SERVER['PHP_SELF']?>\" >
</form>
Also i am not sure wheather isset($Delete) will work. you will need to use isset($_POST['submit']) (and add the value name="submit" to your submit button otherwise the $_POST key is not set.
After that we are on to the delete query itself. $_POST['id'] exists but i do not think that it is the value that you want because the checkbox is named delete. so you will want to referance $_POST['delete'].
You also need to reoder you script I think so that the delete is done before you out put the form other wise it will take an extra reload to view the changes.
You do not seem to have a form tag, so when you are clicking the submit button the browser has no idea what to do. I guess that you are trying to load the page on to itself so try wrapping the input fields in
<form method="post" action="<?= $_SERVER['PHP_SELF']?>\" >
</form>
Also i am not sure wheather isset($Delete) will work. you will need to use isset($_POST['submit']) (and add the value name="submit" to your submit button otherwise the $_POST key is not set.
After that we are on to the delete query itself. $_POST['id'] exists but i do not think that it is the value that you want because the checkbox is named delete. so you will want to referance $_POST['delete'].
You also need to reoder you script I think so that the delete is done before you out put the form other wise it will take an extra reload to view the changes.
Last edited by UrbanSky; Dec 7th, 2006 at 7:55 am.
•
•
Join Date: Dec 2006
Posts: 34
Reputation:
Solved Threads: 0
<?
switch($action)
{
case 'delete':
$checklist = $_POST["checkme"];
for($i=0; $i<count($checklist); $i++)
{
echo "print";
$remove = $checklist[$i];
$query=mysql_query("DELETE FROM materialmaster WHERE material_code='$remove'");
}
if(!$query){
echo "failed connection";
}
else
header("location:showmm.php");
break;
}
// declare the checkbox as an array in the table
<td><input type="checkbox" name="<?='checkme[]';?>" value="<?php echo $row["material_code"];?>">
</Td>
//where $row is
$sql="SELECT * FROM materialmaster order by material_code";
$result=mysql_query($sql);
$row=mysql_fetch_array($result)
You need to replace the table,field names etc.... the catch is that checkbox is an array.hope it solves
switch($action)
{
case 'delete':
$checklist = $_POST["checkme"];
for($i=0; $i<count($checklist); $i++)
{
echo "print";
$remove = $checklist[$i];
$query=mysql_query("DELETE FROM materialmaster WHERE material_code='$remove'");
}
if(!$query){
echo "failed connection";
}
else
header("location:showmm.php");
break;
}
// declare the checkbox as an array in the table
<td><input type="checkbox" name="<?='checkme[]';?>" value="<?php echo $row["material_code"];?>">
</Td>
//where $row is
$sql="SELECT * FROM materialmaster order by material_code";
$result=mysql_query($sql);
$row=mysql_fetch_array($result)
You need to replace the table,field names etc.... the catch is that checkbox is an array.hope it solves
•
•
Join Date: Dec 2006
Posts: 6
Reputation:
Solved Threads: 0
I've tried it mate didn't quite work but some thing did work here it is
•
•
•
•
<?php
include("config.php");
include("contentdb.php");
if($_POST['delete']) {
foreach($_POST as $id) { // This will loop through the checked checkboxes
mysql_query("DELETE FROM qtable WHERE id='$id'"); // This deletes the record from the database
}
}
$errorString = "";
$id= $_POST["id"];
$question= $_POST["question"];
if (empty($question))
$errorString =$errorString."<br>Question Field cannot be blank.";
$opt1= $_POST["opt1"];
if (empty($opt1))
$errorString =$errorString."<br>Option 1 Field cannot be blank.";
$opt2= $_POST["opt2"];
if (empty($opt2))
$errorString =$errorString."<br>Option 2 Field cannot be blank.";
$opt3= $_POST["opt3"];
if (empty($opt3))
$errorString =$errorString."<br>Option 3 Field cannot be blank.";
$answer= $_POST["answer"];
if (empty($answer))
$errorString =$errorString."<br>Answer Field cannot be blank.";
if (!empty($errorString))
{
echo "There were some errors :<br>". $errorString;
}
else //Otherwise, insert the data into database
{
}
$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.");
?>
<form name="action" id="action" method="post" action="<?=$_SERVER['PHP_SELF']?>">
<?
/* 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");
?>
<TD ALIGN=CENTER ><input type="checkbox" name="<?=$row[id]?>" id="<?=$row[id]?>" value="<?=$row[id]?>"/></TD>
<?php
print ("</tr>");
}
mysql_free_result($result);
} else {
echo "Error in query: $query. ".mysql_error(); // print error message
}
}
print ("</TABLE>\n");
?>
<center><input type="submit" name="delete" id="delete" value="Delete Selected" /></center>
<?
echo "</form><br>\n";
// close connection
mysql_close($db);
?>
![]() |
Similar Threads
- MySQL Checkbox Multiple data Delete? (PHP)
- How to get value of selected dynamic checkboxes? (ASP.NET)
- html checkboxes... (PHP)
- Event Handlers for buttons created in ItemDataBound (ASP.NET)
Other Threads in the PHP Forum
- Previous Thread: Has anyone used this....
- Next Thread: Loop Problem,plz help
| Thread Tools | Search this Thread |
301 apache api array autosuggest beginner beneath binary broadband broken button cakephp checkbox class cms code compression countingeverycharactersfromastring crack cron curl database date decode display dynamic echo email error file files folder form forms function functions google href htaccess html httppost image include insert integration ip javascript joomla limit link links login mail match md5 menu mlm multiple mysql mysql_real_escape_string oop paypal pdf php problem protocol query radio random recursion remote script search searchbox server session sessions sms smtp source space sql strip_tags survey syntax system table tutorial update upload url validator variable video virus votedown web website window.onbeforeunload=closeme; youtube





