dhorn Posted June 16, 2009 Share Posted June 16, 2009 So I recently was tasked with learning some PHP for work, and today is almost day 2. I started building on top of another page that was already written by someone else, but I'm running into issues. I want to enable the ability to remove multiple items using checkboxes, but I can't get it to work. Here is the code <? //connect to mysql $connect = mysql_connect("localhost", "jamestb", "thomas")or die("Connection Error..."); //open database $con_db = mysql_select_db("potluck") or die("Couldn't open DB: " . mysql_error()); //set up for submit if (isset($_POST['isSubmit'])){ $fname = $_POST['fname']; $lname = $_POST['lname']; $category = $_POST['category']; $item_text = $_POST['item_text']; $sql = "INSERT INTO info (last_name, first_name, course, item) VALUES ('$lname', '$fname', '$category', '$item_text')"; $result = mysql_query($sql) or die (mysql_error()); } //delete checked if (isset($_POST['deleteChecked'])){ $query = "SELECT * FROM info"; $result = mysql_query($query); $count = mysql_num_rows($result); for ($i=0;$i<$count;$i++){ $del_id[$i] = $_POST['checkbox[]']; $sql = "DELETE FROM info WHERE id='del_id'"; $result = mysql_query($sql); } } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Potluck Luncheon</title> <link rel="stylesheet" type="text/css" href="default.css" /> </head> <body> <div id="wrapper"> <div id="pushdown"></div> <font face="Arial, Helvetica, sans-serif"> </p> <?php $query = "SELECT * FROM info"; $result = mysql_query($query) or die(mysql_error()); echo "<table border='1'><tr><th colspan='3' class = 'head'> Salads </th></tr>"; echo "<tr><th width='250'> Item </th><th width='250'> Name </th><th width='250'> Delete? </th></tr>"; while($row = mysql_fetch_array($result)){ if($row['course'] == 0){ $id=$row['id'];//take out the id echo "<tr><td>".$row['item']."</td>"; echo "<td>".$row['first_name'] ."</td>"; echo "<td><input name='checkbox[]' type='checkbox' id='checkbox[]' value='" .$id. "'></td>"; //echo "<td> <a href='delete.php?cmd=delete&id=$id'>Delete</a></tr> </td>"; } } echo "<br /><br /></table>"; $query = "SELECT * FROM info"; $result = mysql_query($query) or die(mysql_error()); echo "<table border='1'><tr><th colspan='3' class = 'head'> Main Course </th></tr>"; echo "<tr><th width='250'> Item </th><th width='250'> Name </th><th width='250'> Delete?</th></tr>"; while($row = mysql_fetch_array($result)){ if($row['course'] == 1){ $id=$row['id'];//take out the id echo "<tr><td>".$row['item']."</td>"; echo "<td>".$row['first_name'] ."</td>"; //echo "<td><input type='Checkbox' name=checkbox[] id='chkbox[]' id='$rows['id']'></td>"; //echo "<td> <a href='delete.php?cmd=delete&id=$id'> Delete</a> </td></tr>"; } } echo "<br /><br /></table>"; $query = "SELECT * FROM info"; $result = mysql_query($query) or die(mysql_error()); echo "<table border='1'><tr><th colspan='3' class = 'head'> Complementary </th></tr>"; echo "<tr><th width='250'> Item </th><th width='250'> Name </th><th width='250'> Delete?</th></tr>"; while($row = mysql_fetch_array($result)){ if($row['course'] == 2){ $id=$row['id'];//take out the id echo "<tr><td>".$row['item']."</td>"; echo "<td>".$row['first_name'] ."</td>"; //echo "<td><input type='Checkbox' name=checkbox[] id='chkbox[]' id='$rows['id']'></td>"; //echo "<td> <a href='delete.php?cmd=delete&id=$id'> Delete</a> </td>"; } } echo "<br /><br /></table>"; $query = "SELECT * FROM info"; $result = mysql_query($query) or die(mysql_error()); echo "<table border='1'><tr><th colspan='3' class = 'head'> Desert </th></tr>"; echo "<tr><th width='250'> Item </th><th width='250'> Name </th><th width='250'>Delete?</th></tr>"; while($row = mysql_fetch_array($result)){ if($row['course'] == 3){ $id=$row['id'];//take out the id echo "<tr><td>".$row['item']."</td>"; echo "<td>".$row['first_name'] ."</td>"; //echo "<td><input type='Checkbox' name=checkbox[] id='chkbox[]' id='$rows['id']'></td>"; //echo "<td> <a href='delete.php?cmd=delete&id=$id'> Delete</a> </td>"; } } echo "<br /><br /></table>"; $query = "SELECT * FROM info"; $result = mysql_query($query) or die(mysql_error()); echo "<table border='1'><tr><th colspan='3' class = 'head'> Drinks </th></tr>"; echo "<tr><th width='250'> Item </th><th width='250'> Name </th><th width='250'> Delete? </th></tr>"; while($row = mysql_fetch_array($result)){ if($row['course'] == 4){ $id=$row['id'];//take out the id echo "<tr><td>".$row['item']."</td>"; echo "<td>".$row['first_name'] ."</td>"; //echo "<td><input type='Checkbox' name=checkbox[] id='chkbox[]' id='$rows['id']'></td>"; //echo "<td> <a href='delete.php?cmd=delete&id=$id'> Delete</a> </td>"; } } echo "<br /><br /></table>"; ?> <br /> <hr /> <form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post"> <fieldset> <p>First Name: <input type="text" name="fname" size="20" /> Last Name: (Optional) <input type="text" name="lname" size="20" /> </p> <legend> Make a Contribution </legend> <br /> <input type="radio" value="0" name="category" /> Salads <input type="radio" value="1" name="category" /> Main Course <input type="radio" value="2" name="category" /> Complementary <input type="radio" value="3" name="category" /> Desert <input type="radio" value="4" name="category" /> Drinks<br /><br /> The Item You are Bringing: <input type="text" name="item_text" size="40" /> <br /><br /></p> <p><i> Please take time to review the above items already being brought. <br /> For multiple items, please complete the form twice.<br /> Thank you, and have a wonderful day!</i></p> <input type="submit" name="isSubmit" value="Submit Carry In" /> <input type="reset" value="Reset Forms" /> <input type="submit" name="deleteChecked" value="Delete Checked" /> </fieldset> </form> <div id="litslabs"> <div id="maintence"> site developed and maintained by: </div> <a href="http://ul.bgsu.edu/litslabs/"><img alt="Developed by: Lits Labs" src="logoblack.png" /></a><br /> <a href="delete_all.php">Delete All</a> </div> </div> </body> </html> Any help or ideas are much appreciated. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/162401-solved-two-submit-buttons-undefined-variable-problem/ Share on other sites More sharing options...
Maq Posted June 16, 2009 Share Posted June 16, 2009 but I can't get it to work. Please, elaborate. It will make it much easier for us to diagnose the problem if we know what happens, doesn't happen, and if there are any errors. Quote Link to comment https://forums.phpfreaks.com/topic/162401-solved-two-submit-buttons-undefined-variable-problem/#findComment-857184 Share on other sites More sharing options...
MadTechie Posted June 16, 2009 Share Posted June 16, 2009 Okay the problem with the delete code (see comments) <?php //delete checked if (isset($_POST['deleteChecked'])){ $query = "SELECT * FROM info"; $result = mysql_query($query); $count = mysql_num_rows($result); for ($i=0;$i<$count;$i++){ $del_id[$i] = $_POST['checkbox[]']; //Nope the [] make it an array $sql = "DELETE FROM info WHERE id='del_id'"; //del_id should be $del_id $result = mysql_query($sql); } } ?> okay heres how your fix your code <?php foreach($_POST['checkbox'] as $del_id) { $sql = "DELETE FROM info WHERE id='$del_id'"; $result = mysql_query($sql); } } ?> BUT this is a better way. as it only uses 1 SQL call <?php //delete checked if (isset($_POST['deleteChecked'])){ /*better option*/ $CB = implode(",",$_POST['checkbox']); $query = "DELETE FROM info WHERE id IN ($CB)"; $result = mysql_query($query); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/162401-solved-two-submit-buttons-undefined-variable-problem/#findComment-857187 Share on other sites More sharing options...
dhorn Posted June 16, 2009 Author Share Posted June 16, 2009 Okay, I tried both of the methods you posted, and here are some errrors I received. For the first method: Warning: Invalid argument supplied for foreach() in /var/www/html/dan/potluck/index.php on line 32 For the second using implode: Notice: Undefined index: checkbox in /var/www/html/dan/potluck/index.php on line 27 Warning: implode() [function.implode]: Bad arguments. in /var/www/html/dan/potluck/index.php on line 27 Quote Link to comment https://forums.phpfreaks.com/topic/162401-solved-two-submit-buttons-undefined-variable-problem/#findComment-857206 Share on other sites More sharing options...
dhorn Posted June 16, 2009 Author Share Posted June 16, 2009 It's now working. I had to expand my form to encapsulate all the check boxes, then I used the implode code that you posted and everything appears to be working great. Thanks, Dan Quote Link to comment https://forums.phpfreaks.com/topic/162401-solved-two-submit-buttons-undefined-variable-problem/#findComment-857399 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.