Jump to content

[SOLVED] Check checkbox


Ken2k7

Recommended Posts

Say I have:

 

 

<form method='post' name='whatever'>

<input type='checkbox' name='1' /> Something 1

<input type='checkbox' name='2' /> Something 2

.

.

.

<input type='submit' name='submit' value='submit' />

</form>

 

 

Is there a way of using PHP to first check all the input and see if its checked and then if it is, remove that entry from the database?

 

- Note that the input list is generated with PHP and I will handle the remove line. I just need to know how to check with PHP if the input value is checked. I was thinking of something like:

 

<?php

if ($_POST['submit']){
foreach ($p as $_POST){
// check if it is checked
// if so, remove it (again, I will write this line so you don't have to)
}}

?>

 

Thanks in advance,

Ken

Link to comment
Share on other sites

The query looks like

 

DELETE FROM tablename WHERE id IN ('1', '29', '67')

 

which is an easier way of doing

 

DELETE FROM tablename WHERE (id = '1') OR (id = '29') OR (id = '67')

:o

 

AWESOME! Thank you. I did not know that.

 

Thank you and happy holidays,

Ken

 

 

P.S.: I'm leaving this one open for now. I will close it once I get this working. :)

Link to comment
Share on other sites

Uh this doesn't work for me. Am I doing something wrong. Here's the code:

 

<?php

include_once "test_db.php";

$query = "SELECT number FROM numbers";
$result = mysql_query($query, $database->db_connect) or die(mysql_error());

echo "<form method='post'>";
while ($row = mysql_fetch_assoc($result)){
echo "<input type='checkbox' name='del[]' />".$row['number']."<br />";
}
echo "<input type='submit' name='submit' value='submit' /></form>";

if (isset($_POST['submit'])){
$delete = join(",", $_POST['del']);
mysql_query("DELETE FROM numbers WHERE number IN ('$delete')");
echo "success";
}

?>

 

In case you're wondering about how the SQL table is set up, here:

 

Table: numbers

Column: number int( 8 )

 

That's it. Just testing to see if this works and it doesn't. It doesn't delete the rows I checked off. Please help.

 

Thanks in advance,

Ken

Link to comment
Share on other sites

Thanks Barand.

 

Only problem now is that it only deletes the first checkbox, but I want it to delete multiple ones.

 

Code as of now:

<?php

include_once "test_db.php";

$query = "SELECT number FROM numbers";
$result = mysql_query($query, $database->db_connect) or die(mysql_error());

echo "<form method='post'>";
while ($row = mysql_fetch_assoc($result)){
echo "<input type='checkbox' value='{$row['number']}' name='del[]' />".$row['number']."<br />";
}
echo "<input type='submit' name='submit' value='submit' /></form>";

if (isset($_POST['submit'])){
$delete = join(",", $_POST['del']);
mysql_query("DELETE FROM numbers WHERE number IN ('$delete')");
echo "success";
}

?>

 

Please help.

 

Thanks in advance,

Ken

Link to comment
Share on other sites

i think the code is fine but  i also think you should remove the ' ' in your in clause

 

so this line

mysql_query("DELETE FROM numbers WHERE number IN ('$delete')");

should be

mysql_query("DELETE FROM numbers WHERE number IN ($delete)");

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.