krv Posted March 17, 2009 Share Posted March 17, 2009 Hello, I have a script that allows you to select line items(checkbox) that hold a product id (hidden field, cs2[]) and $ amount (text field, cs3[]). I try to pass the checked items with a foreach() function to then create an array of queries. All i get is the first text box 2x in the database. Any input is appreciated. Thanks. Example of script: if (isset($_POST['scl']) && !empty($_POST['cs1'])) { if ($_POST['checkbox'] == 0) { $nc = "<span style=\"background-color: #fff1a8;\">No services selected</span>"; } else { foreach ($_POST['checkbox'] as $id) { $queries = array(); for ($i = 0; $i < count($id); $i++) { $cs1 = $_POST['cs1']; $cs2 = $_POST['cs2']; $cs3 = $_POST['cs3']; if (!get_magic_quotes_gpc()) { $cs2[$i] = addslashes($cs2[$i]); $cs3[$i] = addslashes($cs3[$i]); } $queries[] = "('$cs1 ','$cs2[$i]','$cs3[$i]')"; } $piece = implode(", ", $queries); $query = "INSERT INTO cust_services (cs1,cs2,cs3) VALUES $piece"; $result = mysql_query($query) or die(mysql_error()); // header("Refresh:0;url="); $nc = "<span style=\"background-color: #fff1a8;\">Service Added</span>"; } // end checkbox loop } } else { $nc = '<span style="background-color: #fff1a8;">Please select a facility</span>'; } Quote Link to comment https://forums.phpfreaks.com/topic/149852-solved-sql-duplicate-entries/ Share on other sites More sharing options...
fenway Posted March 17, 2009 Share Posted March 17, 2009 Huh? Quote Link to comment https://forums.phpfreaks.com/topic/149852-solved-sql-duplicate-entries/#findComment-786999 Share on other sites More sharing options...
krv Posted March 17, 2009 Author Share Posted March 17, 2009 Refresh the page for new image. when i check the boxes that i want to insert into database i get the first line value and it duplicates. Quote Link to comment https://forums.phpfreaks.com/topic/149852-solved-sql-duplicate-entries/#findComment-787015 Share on other sites More sharing options...
xylex Posted March 17, 2009 Share Posted March 17, 2009 It's not a MySQL issue. You need to back way up, your code doesn't match what you're saying, so please explain what it is you trying to do, and post your HTML form as well. Quote Link to comment https://forums.phpfreaks.com/topic/149852-solved-sql-duplicate-entries/#findComment-787117 Share on other sites More sharing options...
krv Posted March 17, 2009 Author Share Posted March 17, 2009 My error was in my for() loop. I was trying to have a list of items that pulled from a database display in a table. Within each row there would be a hidden field with an item id and a text field to enter in a value that associates with the item id. Then i wanted to be able to check which items i wanted to save and also store the value. I was having an issue inserting the Correct value with the item id. I fixed this by subtracting 1 or $i -1 due to an issue where the items and values would insert into the database but would be off by one. if (isset($_POST['scl']) && !empty($_POST['cs1'])) { if ($_POST['checkbox'] == 0) { $nc = " <span style=\"background-color: #fff1a8;\">No services selected</span>"; } else { foreach ($_POST['checkbox'] as $id) { $queries = array(); for ($i = 0; $i < count($id); $i++) { $cs1 = $_POST['cs1']; $cs2 = $_POST['cs2']; $cs3 = $_POST['cs3']; if (!get_magic_quotes_gpc()) { $cs2[$i] = addslashes($cs2[$i]); $cs3[$i] = addslashes($cs3[$i]); } $ls = $id -1; $queries[] = "('$cs1 ','$id','$cs3[$ls]')"; } $piece = implode(", ", $queries); $query = "INSERT INTO cust_services (cs1,cs2,cs3) VALUES $piece"; $result = mysql_query($query) or die(mysql_error()); // header("Refresh:0;url="); $nc = "<span style=\"background-color: #fff1a8;\">Service Added</span>"; } // end checkbox loop } } else { $nc = '<span style="background-color: #fff1a8;">Please select a facility</span>'; } Quote Link to comment https://forums.phpfreaks.com/topic/149852-solved-sql-duplicate-entries/#findComment-787134 Share on other sites More sharing options...
krv Posted March 17, 2009 Author Share Posted March 17, 2009 Subtracting fails. still stuck Quote Link to comment https://forums.phpfreaks.com/topic/149852-solved-sql-duplicate-entries/#findComment-787181 Share on other sites More sharing options...
fenway Posted March 19, 2009 Share Posted March 19, 2009 Subtracting fails. still stuck Sorry, I still have no idea what the problem is. Quote Link to comment https://forums.phpfreaks.com/topic/149852-solved-sql-duplicate-entries/#findComment-788265 Share on other sites More sharing options...
krv Posted March 25, 2009 Author Share Posted March 25, 2009 Solution if (isset($_POST['scl']) && !empty($_POST['cs1'])) { if ($_POST['checkbox'] == 0) { $nc = " <span style=\"background-color: #fff1a8;\">No services selected</span>"; } else { foreach ($_POST['checkbox'] as $id) { $queries = array(); //for ($i = 0; $i < count($id); $i++) //{ // $cs2 = $_POST['cs2']; // $cs3 = $_POST['cs3']; // if (!get_magic_quotes_gpc()) //{ // $cs2[$i] = addslashes($cs2[$i]); // $cs3[$i][$id] = addslashes($cs3[$i]); //} //$ls = $id - 1; $itemId = $_POST['cs2'][$id]; $itemValue = $_POST['cs3'][$id]; $queries[] = "('$cs1 ','$itemId','$itemValue')"; //} $piece = implode(", ", $queries); $query = "INSERT INTO cust_services (cs1,cs2,cs3) VALUES $piece"; $result = mysql_query($query) or die(mysql_error()); $nc = "<span style=\"background-color: #fff1a8;\">Service Added</span>"; header("Refresh:0;URL=services?c=1"); } // end checkbox loop } } <?php $query = "SELECT id,s1,s2 FROM services"; $result = mysql_query($query) or die("Can't create query: " . mysql_error()); while ($row = mysql_fetch_array($result)) { echo " <tr style=\" background-color: #ffffff\" onMouseover=\"this.style.backgroundColor='lightblue';\" onMouseout=\"this.style.backgroundColor='white';\"> <td class='ul'>$row[s1]</td> <td class='ul'><input type=\"hidden\" name=\"cs2[$row[id]]\" value=\"$row[id]\"><input type=\"text\" name=\"cs3[$row[id]]\" value=\"\" /></td> <td><input type='checkbox' name='checkbox[]' value='$row[id]'></td> </tr>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/149852-solved-sql-duplicate-entries/#findComment-793939 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.