eldan88 Posted June 7, 2013 Share Posted June 7, 2013 Hey, I have a list of store names displayed in the script below. I am trying to delete the whole store row, by selecting the store and running a delete query. I am having trouble passing the store id to the $store_id variable through a checkbox. I am want to select a store via checkbox, and pass the ID to the $store_id using the POST super global. Does anyone have any suggestions. Below is my script. <?php require_once("include/database_connection.php"); global $connection; ?> <?php if(isset($_POST['submit'])) { $store_id = ""; // How do I pass the store_id to the $store_id variable by selecting the store below //$delete_query = "DELETE FROM stores WHERE store_id = {$store_id} "; echo $store_id; } ?> <form action="delete_stores.php" method="post"> <?php $count=1; $get_all_stores = "SELECT * FROM stores "; $query = mysql_query($get_all_stores, $connection); while($display_store = mysql_fetch_array($query)) { echo "<input type=\"checkbox\" value=\"{$display_store['store_id']}\">" . $count . " " . $display_store['store] . "</input>" ."<br>"; $count++;} ?> <input type="submit" name="delete" value="delete"> </form> Quote Link to comment https://forums.phpfreaks.com/topic/278900-need-help-passing-a-value-through-a-checkbox/ Share on other sites More sharing options...
AbraCadaver Posted June 7, 2013 Share Posted June 7, 2013 (edited) Couple things: 1. There is no $_POST['submit'] because you named your submit button "delete". 2. You're missing a quote here: $display_store['store] 3. Your checkbox input doesn't have a name. If you name it "store" then you would access $_POST['store']. However, you have the possibility of multiple stores, so you would need a stores array like name="stores[]". You would then need to loop through each store in the array and delete them, or better put them all in one query probably using the WHERE IN (). Edited June 7, 2013 by AbraCadaver Quote Link to comment https://forums.phpfreaks.com/topic/278900-need-help-passing-a-value-through-a-checkbox/#findComment-1434678 Share on other sites More sharing options...
eldan88 Posted June 7, 2013 Author Share Posted June 7, 2013 Couple things: 1. There is no $_POST['submit'] because you named your submit button "delete". 2. You're missing a quote here: $display_store['store] 3. Your checkbox input doesn't have a name. If you name it "store" then you would access $_POST['store']. However, you have the possibility of multiple stores, so you would need a stores array like name="stores[]". You would then need to loop through each store in the array and delete them, or better put them all in one query probably using the WHERE IN (). Okay got you! I have made the changes. I first need to figure out how to i pass in multiple store id's in the array. This is what I cam came up with now, but not getting any luck :/ <?php require_once("database_connection.php"); global $connection; ?> <?php if(isset($_POST['submit'])) { $store_array = array(); $store_array[] = $_POST['store_id']; echo $store_array; } ?> <form action="delete_stores.php" method="post"> <?php $count=1; $get_all_stores = "SELECT * FROM stores "; $query = mysql_query($get_all_stores, $connection); while($display_store = mysql_fetch_array($query)) { echo "<input type=\"checkbox\" value=\"{$display_store['store_id']}\" name=\"store_id\">" . $count . " " . $display_store['store'] . "</input>" ."<br>"; $count++;} ?> <input type="submit" name="submit" value="submit"> </form> Quote Link to comment https://forums.phpfreaks.com/topic/278900-need-help-passing-a-value-through-a-checkbox/#findComment-1434679 Share on other sites More sharing options...
Barand Posted June 7, 2013 Share Posted June 7, 2013 However, you have the possibility of multiple stores, so you would need a stores array like name="stores[]". You would then need to loop through each store in the array and delete them, or better put them all in one query probably using the WHERE IN (). Quote Link to comment https://forums.phpfreaks.com/topic/278900-need-help-passing-a-value-through-a-checkbox/#findComment-1434681 Share on other sites More sharing options...
eldan88 Posted June 7, 2013 Author Share Posted June 7, 2013 However, you have the possibility of multiple stores, so you would need a stores array like name="stores[]". You would then need to loop through each store in the array and delete them, or better put them all in one query probably using the WHERE IN (). Barand, I did a foeach loop on the array but its only giving me back one store_id when I select multiple stores and click on submit. Below is the code I am working with if(isset($_POST['submit'])) { $store_array = array(); $store_array[] = $_POST['store_id']; foreach ($store_array as $store_id) { echo $store_id; Quote Link to comment https://forums.phpfreaks.com/topic/278900-need-help-passing-a-value-through-a-checkbox/#findComment-1434687 Share on other sites More sharing options...
DavidAM Posted June 7, 2013 Share Posted June 7, 2013 The name of the INPUT field should use the [] array construct: <INPUT name="store_id[]" ... The reference in the code should not: $store_array = $_POST['store_id'] Quote Link to comment https://forums.phpfreaks.com/topic/278900-need-help-passing-a-value-through-a-checkbox/#findComment-1434708 Share on other sites More sharing options...
eldan88 Posted June 7, 2013 Author Share Posted June 7, 2013 The name of the INPUT field should use the [] array construct: <INPUT name="store_id[]" ... The reference in the code should not: $store_array = $_POST['store_id'] What should the reference be? Can you provide me with a full working example? Quote Link to comment https://forums.phpfreaks.com/topic/278900-need-help-passing-a-value-through-a-checkbox/#findComment-1434741 Share on other sites More sharing options...
DavidAM Posted June 7, 2013 Share Posted June 7, 2013 The name of the INPUT field should use the [] array construct: <INPUT name="store_id[]" ... The reference in the code should not: $store_array = $_POST['store_id'] Oops! typo; should have said: "The reference in the code should be: $store_array = $_POST['store_id']" Quote Link to comment https://forums.phpfreaks.com/topic/278900-need-help-passing-a-value-through-a-checkbox/#findComment-1434743 Share on other sites More sharing options...
AbraCadaver Posted June 7, 2013 Share Posted June 7, 2013 The above are both working examples just as they are. if(isset($_POST['submit'])) { $store_array = $_POST['store_id']; } And echo "<input type=\"checkbox\" value=\"{$display_store['store_id']}\" name=\"store_id[]\">{$display_store['store']}</input><br>"; Quote Link to comment https://forums.phpfreaks.com/topic/278900-need-help-passing-a-value-through-a-checkbox/#findComment-1434744 Share on other sites More sharing options...
eldan88 Posted June 7, 2013 Author Share Posted June 7, 2013 The above are both working examples just as they are. if(isset($_POST['submit'])) { $store_array = $_POST['store_id']; } And echo "<input type=\"checkbox\" value=\"{$display_store['store_id']}\" name=\"store_id[]\">{$display_store['store']}</input><br>"; Abracadaver. Thanks for that tip. It now worked! Just to confirm. Why did we put the [] after store_id is it to let the $_POST super global to except an array? Quote Link to comment https://forums.phpfreaks.com/topic/278900-need-help-passing-a-value-through-a-checkbox/#findComment-1434765 Share on other sites More sharing options...
eldan88 Posted June 8, 2013 Author Share Posted June 8, 2013 The above are both working examples just as they are. if(isset($_POST['submit'])) { $store_array = $_POST['store_id']; } And echo "<input type=\"checkbox\" value=\"{$display_store['store_id']}\" name=\"store_id[]\">{$display_store['store']}</input><br>"; Hey I have one more question. I am trying to do a fetch array on the ID that it brings back. However when I try to do a fetch array it keeps coming back with Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/eldan88/public_html/delete_stores.php on line 22 Below is the code. Any suggesstions? <?php require_once("include/database_connection.php"); global $connection; function get_all_stores() { $query = "SELECT * FROM stores "; return $query; } function get_store_by_id($store_id) { $query = "SELECT * FROM stores WHERE id = {$store_id} "; return $query; } ?> <?php if(isset($_POST['submit'])) { $store = $_POST['store_id']; foreach ($store as $store_id) { $store = get_store_by_id($store_id); mysql_fetch_array($store); } } Quote Link to comment https://forums.phpfreaks.com/topic/278900-need-help-passing-a-value-through-a-checkbox/#findComment-1434769 Share on other sites More sharing options...
Solution Barand Posted June 8, 2013 Solution Share Posted June 8, 2013 You are not running the query, just defining a string function get_store_by_id($store_id) { $query = "SELECT * FROM stores WHERE id = {$store_id} "; $result = mysql_query($query); return $result; } Quote Link to comment https://forums.phpfreaks.com/topic/278900-need-help-passing-a-value-through-a-checkbox/#findComment-1434791 Share on other sites More sharing options...
DavidAM Posted June 8, 2013 Share Posted June 8, 2013 In general, you want to avoid running queries in a loop. It would be better to issue a single select for all of the selected IDs and then loop over the results: if(isset($_POST['submit'])) { // Since they are checkboxes, it is possible that the store_id element does not exist if (isset($_POST['store_id'])) { $store = array_map('intval', $_POST['store_id']); // Protect against invalid input $sql = 'SELECT * FROM stores WHERE id IN (' . implode(',', $store) . ')'; $res = mysql_query($sql); if ($res) { while ($row = mysql_fetch_assoc($res)){ // Process the data } } } }Assuming, of course that the store id is an integer. Quote Link to comment https://forums.phpfreaks.com/topic/278900-need-help-passing-a-value-through-a-checkbox/#findComment-1434794 Share on other sites More sharing options...
eldan88 Posted June 8, 2013 Author Share Posted June 8, 2013 You are not running the query, just defining a string function get_store_by_id($store_id) { $query = "SELECT * FROM stores WHERE id = {$store_id} "; $result = mysql_query($query); return $result; } Yes you right! I can't believe that has slipped!! Thank you for pointing that out! Quote Link to comment https://forums.phpfreaks.com/topic/278900-need-help-passing-a-value-through-a-checkbox/#findComment-1434798 Share on other sites More sharing options...
eldan88 Posted June 8, 2013 Author Share Posted June 8, 2013 In general, you want to avoid running queries in a loop. It would be better to issue a single select for all of the selected IDs and then loop over the results: if(isset($_POST['submit'])) { // Since they are checkboxes, it is possible that the store_id element does not exist if (isset($_POST['store_id'])) { $store = array_map('intval', $_POST['store_id']); // Protect against invalid input $sql = 'SELECT * FROM stores WHERE id IN (' . implode(',', $store) . ')'; $res = mysql_query($sql); if ($res) { while ($row = mysql_fetch_assoc($res)){ // Process the data } } } }Assuming, of course that the store id is an integer. DavidAm, Thanks for the useful tip. I am new to this. But I am going to look up what array map is, and how it works with the query you wrote below it. Quote Link to comment https://forums.phpfreaks.com/topic/278900-need-help-passing-a-value-through-a-checkbox/#findComment-1434799 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.