954,587 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to delete using checkboxes

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!
\n");
} else {
print ("The query could not be executed for inserting your data!
");
}
$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 ("\n");
print ("\n");
print ("No\n");
print ("Question\n");
print ("Option 1\n");
print ("Option 2\n");
print ("Option 3\n");
print ("Answer\n");
print ("Del/Edit\n");
print ("\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 ("\n");
print( "$row[id]\n");
print ("$row[question]\n");
print ("$row[opt1]\n");
print ("$row[opt2]\n");
print ("$row[opt3]\n");
print ("$row[answer]\n");

print ("\n");
print ("");
}
mysql_free_result($result);
} else {
echo "Error in query: $query. ".mysql_error(); // print error message
}
}
print ("\n");
echo " ";

$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
' . mysql_error();
}
// close connection
mysql_close($db);
?>
[/PHP]

zed420
Newbie Poster
6 posts since Dec 2006
Reputation Points: 10
Solved Threads: 0
 

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.

Puckdropper
Posting Pro
500 posts since Jul 2004
Reputation Points: 23
Solved Threads: 23
 

Sorry I didn't explain very well, the problem is nothing happens when I'm pressing the Delete button. It doesn't delete, laugh or cry (lol)
Cheers

zed420
Newbie Poster
6 posts since Dec 2006
Reputation Points: 10
Solved Threads: 0
 

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


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.

UrbanSky
Light Poster
42 posts since Oct 2006
Reputation Points: 10
Solved Threads: 4
 

Thanks a million Chuck I'll do this, see how it goes.

zed420
Newbie Poster
6 posts since Dec 2006
Reputation Points: 10
Solved Threads: 0
 

Just tried every thing what you suggested but still no joy.

zed420
Newbie Poster
6 posts since Dec 2006
Reputation Points: 10
Solved Threads: 0
 
Just tried every thing what you suggested but still no joy.


<?
switch($action)
{
case 'delete':
$checklist = $_POST["checkme"];
for($i=0; $i">

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

php_coder
Light Poster
34 posts since Dec 2006
Reputation Points: 10
Solved Threads: 0
 

Thanks coder I'm gonna have a go at your code.

zed420
Newbie Poster
6 posts since Dec 2006
Reputation Points: 10
Solved Threads: 0
 
Thanks coder I'm gonna have a go at your code.


Hi
Was that useful?

php_coder
Light Poster
34 posts since Dec 2006
Reputation Points: 10
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."
Question Field cannot be blank.";
$opt1= $_POST["opt1"];
if (empty($opt1))
$errorString =$errorString."
Option 1 Field cannot be blank.";
$opt2= $_POST["opt2"];
if (empty($opt2))
$errorString =$errorString."
Option 2 Field cannot be blank.";
$opt3= $_POST["opt3"];
if (empty($opt3))
$errorString =$errorString."
Option 3 Field cannot be blank.";
$answer= $_POST["answer"];
if (empty($answer))
$errorString =$errorString."
Answer Field cannot be blank.";
if (!empty($errorString))
{
echo "There were some errors :
". $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!
\n");
} else {
print ("The query could not be executed for inserting your data!
");
}
$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 ("\n");
print ("\n");
print ("No\n");
print ("Question\n");
print ("Option 1\n");
print ("Option 2\n");
print ("Option 3\n");
print ("Answer\n");
print ("Del/Edit\n");
print ("\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 ("\n");
print( "$row[id]\n");
print ("$row[question]\n");
print ("$row[opt1]\n");
print ("$row[opt2]\n");
print ("$row[opt3]\n");
print ("$row[answer]\n");
?>

<?php
print ("");
}
mysql_free_result($result);
} else {
echo "Error in query: $query. ".mysql_error(); // print error message
}
}
print ("\n");
?>

<?
echo "
\n";
// close connection
mysql_close($db);
?>

zed420
Newbie Poster
6 posts since Dec 2006
Reputation Points: 10
Solved Threads: 0
 
I've tried it mate didn't quite work but some thing did work here it is


hi buddy
The mistake in ur code i think is this


Give the name as this:name="<?='checkme[]';?>" where checkme[] is the nameof the checkbox.Do not hv to assign value in the name parameter
catch the value of checkbox as $checklist = $_POST["checkme"];
Then loop it through......
for($i=0; $i$remove = $checklist[$i];
$query=mysql_query("DELETE FROM materialmaster WHERE material_code='$remove'");// Notice that i am deleting using the checkbox var $remove
}
That's it. make these changes......sure it will work.gudluck

php_coder
Light Poster
34 posts since Dec 2006
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You