anser316 Posted March 30, 2008 Share Posted March 30, 2008 Hi, im having a problem with my code. I am trying to build an ordering system, where it shows the products that need to be ordered, which i have done. It should allow you to tick the products you want to order, which i have also done. The rows selected should then send to another form, and show its details. On this form it should also change one of the the attributes in the table called 'status' from NO STATUS to ORDERED. The problem i am having, is that on the second page, it only shows the drug_id, not the others, i think its because its the value of the checked box. Hence, im also having difficulty with looping an update statement, but il never get that to work until i get the first part right. This code is for the first form - order $result2 =mysql_query("SELECT i.drug_id,i.branch_id, d.drug_name,i.total_stock,i.reorder_level, s.supplier_name FROM drugs d, supplier s, branch_items i,branch b WHERE d.supplier_id=s.supplier_id AND d.drug_id=i.drug_id AND b.branch_id=i.branch_id AND i.total_stock<=i.reorder_level ORDER BY total_stock"); echo "<BR><br>"; echo "<b>Minimum Stock Level Met - Reorder Needed</b><p>"; echo "<form action= conorder.php method=POST>"; echo "<table border='1'>"; echo "<tr> <th>DRUG ID</th> <th>Branch ID</th><th>DRUG NAME</th><th>STOCK</th><th>REORDER LEVEL</th><th>SUPPLIER</th></tr>"; while($row = mysql_fetch_array( $result2 )) { echo "<tr><td>"; echo $row['drug_id']; echo "</td><td>"; echo $row['branch_id']; echo "</td><td>"; echo $row['drug_name']; echo "</td><td>"; echo $row['total_stock']; echo "</td><td>"; echo $row['reorder_level']; echo "</td><td>"; echo $row['supplier_name']; echo "</td><td>"; echo "<input type=checkbox name=ticked[] value='{$row['drug_id']}'>"; echo "</td></tr>"; } echo "</table>"; echo "<input type =submit value= Submit>"; echo "<input type = reset>"; echo "</form>"; This code is for the second form, that retrieves the data - conorder if (isset($_POST['ticked'])){ echo "chosen<br>"; echo "<table border='1'>"; echo "<tr> <th>DRUG ID</th> <th>Branch ID</th><th>DRUG NAME</th> <th>BRANCH</th><th>Reorder Level</th><th>Reorder Quantity</th><th>Total Stock</th><th>Status</th></tr>"; foreach ($_POST['ticked'] as $drug_id){ echo "<tr><td>"; echo $drug_id; echo "</td><td>"; echo "$_POST[branch_id]"; echo "</td><td>"; echo $_POST[drug_name]; echo "</td><td>"; echo $_POST[$branch_details]; echo "</td><td>"; echo $_POST[$reorder_level]; echo "</td><td>"; echo $_POST[$reorder_quantity]; echo "</td><td>"; echo $_POST[$total_stock]; echo "</td><td>"; echo $_POST[$status]; echo "</td></tr>"; } } //$size_array=count($_POST[drug_id]); //echo "$size_array"; if (isset($_POST['ticked'])){ foreach ($_POST['ticked'] as $drug_id){ $result =mysql_query("UPDATE branch_items b SET status =ORDERED WHERE drug_id=".$_POST [drug_id]." AND branch_id=".$_POST [branch_id].""); }} Link to comment https://forums.phpfreaks.com/topic/98723-checkboxes-and-retrieving-data-and-updating-rows/ Share on other sites More sharing options...
Bladescope Posted March 30, 2008 Share Posted March 30, 2008 echo "<input type=checkbox name=ticked[] value='{$row['drug_id']}'>"; Should be echo "<input type=checkbox name=ticked[] value='{$row[drug_id]}'>"; And I don't get in part 2, did you mean to set half of the array id's called by variables (like echo $_POST[$status] because for one, $status hasn't been set, and for two, nothing of name 'status' was POSTed by the previous form :x Link to comment https://forums.phpfreaks.com/topic/98723-checkboxes-and-retrieving-data-and-updating-rows/#findComment-505213 Share on other sites More sharing options...
anser316 Posted March 31, 2008 Author Share Posted March 31, 2008 it didnt make a difference, the drug_id shows with or without the quotes, but the attributes dont. Link to comment https://forums.phpfreaks.com/topic/98723-checkboxes-and-retrieving-data-and-updating-rows/#findComment-505217 Share on other sites More sharing options...
Bladescope Posted March 31, 2008 Share Posted March 31, 2008 Right, well gotcha. echo "<BR><br>"; echo "<b>Minimum Stock Level Met - Reorder Needed</b><p>"; echo "<form action= conorder.php method=POST>"; echo "<table border='1'>"; <?php // ... echo "<tr> <th>DRUG ID</th> <th>Branch ID</th><th>DRUG NAME</th><th>STOCK</th><th>REORDER LEVEL</th><th>SUPPLIER</th></tr>"; while($row = mysql_fetch_array( $result2 )) { echo "<tr><td>"; echo $row['drug_id']; echo "</td><td>"; echo $row['branch_id']; echo "</td><td>"; echo $row['drug_name']; echo "</td><td>"; echo $row['total_stock']; echo "</td><td>"; echo $row['reorder_level']; echo "</td><td>"; echo $row['supplier_name']; echo "</td><td>"; echo "<input type=checkbox name=ticked[] value='{$row['drug_id']}'>"; echo "</td></tr>"; } echo "</table>"; echo "<input type =submit value= Submit>"; echo "<input type = reset>"; echo "</form>"; // ... ?> echo "<tr><td>"; echo $row['drug_id']; echo "</td><td>"; Thing is, all you are doing is taking the values from the database and putting them into a table. To actually post them values to another page, they need to be in input boxes too, e.g. echo "<input type='hidden' name='drug_id' value= '$row[drug_id]' />$row['drug_id']"; or echo "<input type='text' name='drug_id' value= '$row[drug_id]' DISABLED />"; it should work then. Link to comment https://forums.phpfreaks.com/topic/98723-checkboxes-and-retrieving-data-and-updating-rows/#findComment-505222 Share on other sites More sharing options...
anser316 Posted March 31, 2008 Author Share Posted March 31, 2008 sorry i dont really follow what is this for: "/>$row['drug_id']"" i put this in echo "<input type='hidden' name='drug_id' value= '$row[drug_id]' echo "<input type='hidden' name='branch_id' value= '$row[branch_id]' instead of echo $row['drug_id']; echo $row['branch_id']; am i supposed to put them at the end after creating checkbox, because the values disappeared. Link to comment https://forums.phpfreaks.com/topic/98723-checkboxes-and-retrieving-data-and-updating-rows/#findComment-505235 Share on other sites More sharing options...
Bladescope Posted March 31, 2008 Share Posted March 31, 2008 sorry i dont really follow what is this for: "/>$row['drug_id']"" i put this in echo "<input type='hidden' name='drug_id' value= '$row[drug_id]' echo "<input type='hidden' name='branch_id' value= '$row[branch_id]' instead of echo $row['drug_id']; echo $row['branch_id']; am i supposed to put them at the end after creating checkbox, because the values disappeared. inputs that are 'hidden' aren't displayed on the page, but are posted when you submit the form. echo "<input type='hidden' name='drug_id' value= '$row[drug_id]' />$row['drug_id']"; That creates the hidden input, then shows the value in the table after the hidden input. The hidden input is only for the purpose of transferring the value to the page when the form is submitted (via POST), it then displays the value on the page after the hidden input. Here, this should make sense. <?php // ... $result2 =mysql_query("SELECT i.drug_id,i.branch_id, d.drug_name,i.total_stock,i.reorder_level, s.supplier_name FROM drugs d, supplier s, branch_items i,branch b WHERE d.supplier_id=s.supplier_id AND d.drug_id=i.drug_id AND b.branch_id=i.branch_id AND i.total_stock<=i.reorder_level ORDER BY total_stock"); echo "<BR><br>"; echo "<b>Minimum Stock Level Met - Reorder Needed</b><p>"; echo "<form action='conorder.php' method='POST'>"; echo "<table border='1'>"; echo "<tr> <th>DRUG ID</th> <th>Branch ID</th><th>DRUG NAME</th><th>STOCK</th><th>REORDER LEVEL</th><th>SUPPLIER</th></tr>"; while($row = mysql_fetch_array( $result2 )) { echo "<tr><td>"; echo "<input type='hidden' name=drug_id[] value='$row[drug_id]'>"; echo $row['drug_id']; echo "</td><td>"; echo "<input type='hidden' name=branch_id[] value='$row[branch_id]'>"; echo $row['branch_id']; echo "</td><td>"; echo "<input type='hidden' name=drug_name[] value='$row[drug_name]'>"; echo $row['drug_name']; echo "</td><td>"; echo "<input type='hidden' name=total_stock[] value='$row[total_stock]'>"; echo $row['total_stock']; echo "</td><td>"; echo "<input type='hidden' name=reorder_level[] value='$row[reorder_level]'>"; echo $row['reorder_level']; echo "</td><td>"; echo "<input type='hidden' name=supplier_name[] value='$row[supplier_name]'>"; echo $row['supplier_name']; echo "</td><td>"; echo "<input type=checkbox name=ticked[] value='{$row['drug_id']}'>"; echo "</td></tr>"; } echo "</table>"; echo "<input type='submit' value='Submit'>"; echo "<input type='reset'>"; echo "</form>"; // ... ?> And step 2: <?php if (isset($_POST['ticked'])){ $i = 0; echo "chosen<br>"; echo "<table border='1'>"; echo "<tr> <th>DRUG ID</th> <th>Branch ID</th><th>DRUG NAME</th> <th>BRANCH</th><th>Reorder Level</th><th>Reorder Quantity</th><th>Total Stock</th><th>Status</th></tr>"; foreach ($_POST['ticked'] as $drug_id){ echo "<tr><td>"; echo $drug_id; echo "</td><td>"; echo "$_POST[branch_id][$i]"; echo "</td><td>"; echo $_POST[drug_name][$i]; echo "</td><td>"; // I haven't changed anything past this point, because I don't understand it. echo $_POST[$branch_details]; echo "</td><td>"; echo $_POST[$reorder_level]; echo "</td><td>"; echo $_POST[$reorder_quantity]; echo "</td><td>"; echo $_POST[$total_stock]; echo "</td><td>"; echo $_POST[$status][$i]; echo "</td></tr>"; $i++; } } //$size_array=count($_POST[drug_id]); //echo "$size_array"; if (isset($_POST['ticked'])){ foreach ($_POST['ticked'] as $drug_id){ $result =mysql_query("UPDATE branch_items b SET status =ORDERED WHERE drug_id=".$_POST [drug_id]." AND branch_id=".$_POST [branch_id].""); }} ?> Link to comment https://forums.phpfreaks.com/topic/98723-checkboxes-and-retrieving-data-and-updating-rows/#findComment-505242 Share on other sites More sharing options...
anser316 Posted March 31, 2008 Author Share Posted March 31, 2008 Yep, the data shows now thanks a lot., however, the result data shows the same for all the rows, apart from drug_id DRUGID ID DRUG NAME STOC RE SUPPLIER 5 2 sampledrug7 4 4 Unichem 7 2 sampledrug7 4 4 Unichem In fact it shows the details for the second key for the first one. Link to comment https://forums.phpfreaks.com/topic/98723-checkboxes-and-retrieving-data-and-updating-rows/#findComment-505267 Share on other sites More sharing options...
anser316 Posted March 31, 2008 Author Share Posted March 31, 2008 solved thanks Link to comment https://forums.phpfreaks.com/topic/98723-checkboxes-and-retrieving-data-and-updating-rows/#findComment-505360 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.