...

View Full Version : PHP Checkboxes & mySQL Database



Myrmidon16
02-26-2012, 11:02 PM
Hey all;

I am working on a project where I have a database of a list of tasks that I have to develop a PHP application that allows for adding, viewing, and deleting items from the database. To delete items, I have them in a checkbox list, and the user must check the box next to the item they wish to remove from the list, and press submit. Here is my PHP code:



<?php

switch($_GET['state']) {

case "add": if(isset($_POST['addtask'])) {
if(empty($_POST['task'])) {
$error['task'] = 'A task is required.';
}

if(sizeof($error) == 0) {
$sql = "INSERT INTO tasks (
task_id,
taskname
) VALUES (
null,
'{$_POST['task']}'
)";
mysql_query($sql);

echo "<p>New task entered.</p>";
}
}
break;

case "delete": if(isset($_POST['submittask'])) {

$taskentry = 'unchecked';

if(isset($_POST['taskentry']) == true) {

$sql = "DELETE FROM tasks WHERE task_id = '{$_GET['taskentry']}'";
mysql_query($sql);
echo "<p>Your post has been deleted.</p>";

}
}
}

?>

<form method="post" action="apps.php?state=add">
<label class="php">New Task</label>
<input type="text" name="task" /><br />
<?php echo "<p>";
echo $error['task'];
echo "</p>";
?>
<input type="submit" name="addtask" value="Add" />
</form>

<form metod="post" action="apps.php?state=delete">
<?php
$sql = "SELECT
task_id,
taskname
FROM
tasks
ORDER BY
task_id";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
echo "<font class='php'>";
echo "<input name='taskentry' type='checkbox'>{$row['taskname']}</input><br />";
echo "</font>";
}
?><br /><br />
<input type="submit" name="submittask" value="Submit" />
</form>


I can't quite figure out how to use the checkboxes in PHP, and so far the only way I have been able to remove items from the list is if I remove them all at once. Can you help me figure out how to properly delete items that are checked on this list from the database? Thank you and please let me know if you have any questions.

mlseim
02-26-2012, 11:23 PM
You need to make a checkbox array with values ...

echo "<input name='taskentry[]' type='checkbox' value='{$row['task_id']}'>{$row['taskname']}</input><br />";


Now, when a checkbox is checked, the array called "taskentry[]" will have
values in them. The array will only contain the ones that were checked.


I can't test this part so make a safe copy before you overwrite it ...




case "delete": if(isset($_POST['submittask'])) {

//$taskentry = 'unchecked';

//if(isset($_POST['taskentry']) == true) {

$tasks=$_POST['taskentry'];
foreach($tasks as $task){

$sql = "DELETE FROM tasks WHERE task_id = '$task'";
mysql_query($sql);
echo "<p>Your post has been deleted.</p>";

}
}

Myrmidon16
02-27-2012, 01:43 AM
Hello mlseim;

Thank you for your response! Unfortunately, the code you provides did not work. I'm still a little confused as to how this works. I added the array like you suggested, but where do I go from there? I want to be able to check one or multiple tasks and click "submit", removing the tasks from the database. Like I said before, the only way I got it to work was by deleting all of the tasks at once, which I thought was because I contained all of the tasks in one line of code. Arrays confuse me a little, if you have any suggestions or I am looking at the problem the wrong way, please let me know.

mlseim
02-27-2012, 04:42 AM
Does your script look like the one below ...
This is how it should look after you entered my changes ...
Let me know if this is failing, and what you mean by "doesn't work".





<?php

switch($_GET['state']) {

case "add": if(isset($_POST['addtask'])) {
if(empty($_POST['task'])) {
$error['task'] = 'A task is required.';
}

if(sizeof($error) == 0) {
$sql = "INSERT INTO tasks (
task_id,
taskname
) VALUES (
null,
'{$_POST['task']}'
)";
mysql_query($sql);

echo "<p>New task entered.</p>";
}
}
break;

case "delete": if(isset($_POST['submittask'])) {

//$taskentry = 'unchecked';

//if(isset($_POST['taskentry']) == true) {

$tasks=$_POST['taskentry'];
foreach($tasks as $task){

$sql = "DELETE FROM tasks WHERE task_id = '$task'";
mysql_query($sql);
echo "<p>Your post has been deleted.</p>";

}
}
}
?>

<form method="post" action="apps.php?state=add">
<label class="php">New Task</label>
<input type="text" name="task" /><br />
<?php echo "<p>";
echo $error['task'];
echo "</p>";
?>
<input type="submit" name="addtask" value="Add" />
</form>

<form metod="post" action="apps.php?state=delete">
<?php
$sql = "SELECT
task_id,
taskname
FROM
tasks
ORDER BY
task_id";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
echo "<font class='php'>";
echo "<input name='taskentry[]' type='checkbox' value='{$row['task_id']}'>{$row['taskname']}</input><br />";
echo "</font>";
}
?><br /><br />
<input type="submit" name="submittask" value="Submit" />
</form>

Myrmidon16
02-27-2012, 07:07 PM
mlseim, my code looks like yours, however the application is still not deleting the checked tasks from the database. I check the task's checkbox, click submit, and the page reloads, however the task is not removed and the echo "Your post has been deleted" doesn't appear. Here is my code, if I missed anything:


<?php

switch($_GET['state']) {

case "add": if(isset($_POST['addtask'])) {
if(empty($_POST['task'])) {
$error['task'] = 'A task is required.';
}

if(sizeof($error) == 0) {
$sql = "INSERT INTO tasks (
task_id,
taskname
) VALUES (
null,
'{$_POST['task']}'
)";
mysql_query($sql);

echo "<p>New task entered.</p>";
}
}
break;

case "delete": if(isset($_POST['submittask'])) {

$tasks=$_POST['taskentry'];
foreach($tasks as $task){
$sql = "DELETE FROM tasks WHERE task_id = '$task'";
mysql_query($sql);
echo "<p>Your post has been deleted.</p>";
}
}
}

?>

<form method="post" action="apps.php?state=add">
<label class="php">New Task</label>
<input type="text" name="task" /><br />
<?php echo "<p>";
echo $error['task'];
echo "</p>";
?>
<input type="submit" name="addtask" value="Add" />
</form>

<form metod="post" action="apps.php?state=delete">
<?php
$sql = "SELECT
task_id,
taskname
FROM
tasks
ORDER BY
task_id";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
echo "<font class='php'>";
echo "<input name='taskentry[]' type='checkbox' value='{$row['task_id']}'>{$row['taskname']}</input><br />";
echo "</font>";
}
?><br /><br />
<input type="submit" name="submittask" value="Submit" />
</form>


Also, here is the site where I am running the application, if you need to look at it in process in order to figure out what is going wrong: http://myrmidon16.x10.mx/sideQUEST/apps.php. I'll keep researching this, as I am relatively new to PHP, but if you can figure anything else, please let me know. Thank you.

Myrmidon16
02-27-2012, 07:31 PM
Alright, I misspelled "method" in my form. It works now, thank you so much for your help. I hate PHP.

mlseim
02-27-2012, 09:39 PM
no problem ... glad you found that typo.

I wasn't sure where I was going to go next.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum