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
https://forums.phpfreaks.com/topic/83084-solved-check-checkbox/
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
https://forums.phpfreaks.com/topic/83084-solved-check-checkbox/#findComment-422681
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
https://forums.phpfreaks.com/topic/83084-solved-check-checkbox/#findComment-426897
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
https://forums.phpfreaks.com/topic/83084-solved-check-checkbox/#findComment-427082
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.