Myrmidon16
02-26-2012, 10: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.
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.