turpentyne Posted July 23, 2010 Share Posted July 23, 2010 please please please, somebody help. I'm new at php, and I'm going nuts trying to figure out this one task. In one form, I need to enter the results of two sets of checkboxes into two tables. Nothing has worked, so I'm going back to basics and building from there. I built this code below. It works but only puts the first row in, matching the first checkbox selected. Nothing else Here's the code: <?php if (isset($_POST['submitted'])) { $errors = array(); $item_id = ($_POST['item_id']); $column2 = ($_POST['column2_id']); if (empty($errors)) { require ('databaseConnect.php'); foreach ($_POST['column2_id'] AS $key => $column2) { $query = "INSERT INTO tablename(item_id, column2_id) VALUES ('$item_id', '$column2')"; $result = mysql_query ($query); if ($result) { echo 'one plant has been added'; exit(); } else { echo 'system error. No plant added'; echo '<p>' . mysql_error() . '<br><br>query:' . $query . '</p>'; exit(); } mysql_close(); } } } ?> <body> <FORM action="test.php" method="post"> <input type="text" name="item_id" value="<?php if(isset($_POST['item_id'])) echo $_POST['item_id']; ?>" /> <br> <input type="checkbox" name="column2_id[]" value="1" > one <br> <input type="checkbox" name="column2_id[]" value="2" > two <br> <input type="checkbox" name="column2_id[]" value="3" > three <br> </fieldset><br><br> <input type="hidden" name="submitted" value="TRUE"> <input type="submit" /> </form> Quote Link to comment https://forums.phpfreaks.com/topic/208642-foreach-only-puts-first-check-box-in-db/ Share on other sites More sharing options...
PravinS Posted July 23, 2010 Share Posted July 23, 2010 Try like this for loop $column2 = $_POST['column2_id']; for ($i=0;$i<count($column2);$i++) { echo $column2[$i]; } Quote Link to comment https://forums.phpfreaks.com/topic/208642-foreach-only-puts-first-check-box-in-db/#findComment-1090044 Share on other sites More sharing options...
turpentyne Posted July 23, 2010 Author Share Posted July 23, 2010 Ok, I tried that. Unfortunately, it's doing the same thing. Only the first row goes into the database. Here's what I wrote: for ($i=0;$i<count($column2);$i++) { $query = "INSERT INTO tablename(item_id, column2_id) VALUES ('$item_id', '$column2[$i]')"; $result = mysql_query ($query); if ($result) { echo 'your items have been added'; Quote Link to comment https://forums.phpfreaks.com/topic/208642-foreach-only-puts-first-check-box-in-db/#findComment-1090185 Share on other sites More sharing options...
Pikachu2000 Posted July 23, 2010 Share Posted July 23, 2010 Are the checkboxes checked when the form is submitted? They're only present in the $_POST array if they're actually checked. Quote Link to comment https://forums.phpfreaks.com/topic/208642-foreach-only-puts-first-check-box-in-db/#findComment-1090190 Share on other sites More sharing options...
turpentyne Posted July 23, 2010 Author Share Posted July 23, 2010 hehe! I wish it was that simple. In the actual form, there are several checkboxes and at least half of them were checked. Quote Link to comment https://forums.phpfreaks.com/topic/208642-foreach-only-puts-first-check-box-in-db/#findComment-1090201 Share on other sites More sharing options...
Pikachu2000 Posted July 23, 2010 Share Posted July 23, 2010 Alright then, to start debugging, see what the $_POST array actually contains. put echo '<pre>'; print_r($_POST); echo '</pre>'; at the head of the script and see if it contains the values you'd expect it to. Quote Link to comment https://forums.phpfreaks.com/topic/208642-foreach-only-puts-first-check-box-in-db/#findComment-1090205 Share on other sites More sharing options...
turpentyne Posted July 23, 2010 Author Share Posted July 23, 2010 ok, I've done that. It's definitely knows what I've clicked. Still only the first entry goes into the database. Here's what it created. Array ( [item_id] => 20 [column2_id] => Array ( [0] => 1 [1] => 5 [2] => 9 [3] => 10 ) [submitted] => TRUE ) one item has been added Quote Link to comment https://forums.phpfreaks.com/topic/208642-foreach-only-puts-first-check-box-in-db/#findComment-1090226 Share on other sites More sharing options...
Pikachu2000 Posted July 23, 2010 Share Posted July 23, 2010 Found it (I think). You have a curly bracket out of place. add one after $result = mysql_query ($query);, and remove one just prior to the closing ?> tag. Try that, and see if it needs any further tweaking. Quote Link to comment https://forums.phpfreaks.com/topic/208642-foreach-only-puts-first-check-box-in-db/#findComment-1090237 Share on other sites More sharing options...
turpentyne Posted July 23, 2010 Author Share Posted July 23, 2010 uh-oh! Doing that, generated this error on reloading of the page: Warning: Invalid argument supplied for foreach() in /filepath/htdocs/filename.php on line 15 Fatal error: Call to undefined function mysql_real_query() in /filepath/htdocs/filename.php on line 18 Quote Link to comment https://forums.phpfreaks.com/topic/208642-foreach-only-puts-first-check-box-in-db/#findComment-1090254 Share on other sites More sharing options...
Pikachu2000 Posted July 23, 2010 Share Posted July 23, 2010 Can you post the code as it is now, with the revisions? Quote Link to comment https://forums.phpfreaks.com/topic/208642-foreach-only-puts-first-check-box-in-db/#findComment-1090257 Share on other sites More sharing options...
turpentyne Posted July 23, 2010 Author Share Posted July 23, 2010 Sure... here it is below: (by the way, how do you guys post this code in those windows, in the forum? are you just putting it in an html form text box?) <html> <title>title</title> <?php echo '<pre>'; print_r($_POST); echo '</pre>'; if (isset($_POST['submitted'])) { $errors = array(); $item_id = ($_POST['plant_id']); $column2 = ($_POST['column2_id']); if (empty($errors)) { require ('databaseconnect.php'); for ($i=0;$i<count($medicine);$i++) { $query = "INSERT INTO tablename(item_id, column2_id) VALUES ('$item_id', '$column2[$i]')"; $result = mysql_query ($query); } if ($result) { echo 'items have been added'; exit(); } else { echo 'system error. Not added'; echo '<p>' . mysql_error() . '<br><br>query:' . $query . '</p>'; exit(); } mysql_close(); } } ?> <body> <FORM style="border: 1px dotted red; padding: 2px" action="self_formname.php" method="post"> item id field:<br> only enter numbers here<br> //this will be a hidden passed variable in finished form <input type="text" name="item_id" value="<?php if(isset($_POST['item_id'])) echo $_POST['item_id']; ?>" /> <br><br> characteristics:<br> <input type="checkbox" name="column2_id[]" value="1" > one <br> <input type="checkbox" name="column2_id[]" value="2" > two <br> <input type="checkbox" name="column2_id[]" value="3" > three <br> <input type="checkbox" name="column2_id[]" value="4" > four <br> <input type="checkbox" name="column2_id[]" value="5" > five <br> <input type="checkbox" name="column2_id[]" value="6" > six <br> <input type="checkbox" name="column2_id[]" value="7" > Seven <br> </fieldset><br><br> <input type="hidden" name="submitted" value="TRUE"> <input type="submit" /> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/208642-foreach-only-puts-first-check-box-in-db/#findComment-1090267 Share on other sites More sharing options...
wildteen88 Posted July 23, 2010 Share Posted July 23, 2010 You're original code you used is correct <?php if (isset($_POST['submitted'])) { $errors = array(); $item_id = ($_POST['item_id']); $column2 = ($_POST['column2_id']); if (empty($errors)) { require ('databaseConnect.php'); foreach ($_POST['column2_id'] AS $key => $column2) { $query = "INSERT INTO tablename(item_id, column2_id) VALUES ('$item_id', '$column2')"; $result = mysql_query ($query); if ($result) { echo 'one plant has been added'; exit(); } else { echo 'system error. No plant added'; echo '<p>' . mysql_error() . '<br><br>query:' . $query . '</p>'; exit(); } mysql_close(); } } } ?> <body> <FORM action="test.php" method="post"> <input type="text" name="item_id" value="<?php if(isset($_POST['item_id'])) echo $_POST['item_id']; ?>" /> <br> <input type="checkbox" name="column2_id[]" value="1" > one <br> <input type="checkbox" name="column2_id[]" value="2" > two <br> <input type="checkbox" name="column2_id[]" value="3" > three <br> </fieldset><br><br> <input type="hidden" name="submitted" value="TRUE"> <input type="submit" /> </form> The reason why your script is only adding one value into your database is because you're callingexit (on line 21) when your query successfully inserts a new row. 19. if ($result) { 20. echo 'one plant has been added'; 21. exit(); 22. 23. } else { Delete exit(); and your script will execute as you intended. (by the way, how do you guys post this code in those windows, in the forum? are you just putting it in an html form text box?) Sure, wrap your code within code tags ( or ) Quote Link to comment https://forums.phpfreaks.com/topic/208642-foreach-only-puts-first-check-box-in-db/#findComment-1090275 Share on other sites More sharing options...
Pikachu2000 Posted July 23, 2010 Share Posted July 23, 2010 To have the code in the code or php boxes, just post it within . . . or . . . bbcode tags. I've just tested this, and it should work fine. There is no plant_id field in the form, there is item_id. See my comment in the code regarding that, also, to submit a form to itself, you can simply leave it as <form action="". . . There's no need to specify the script name. <html> <title>title</title> <?php if (isset($_POST['submitted'])) { $errors = array(); $item_id = ($_POST['item_id']); // This was $_POST['plant_id'], which didn't exist in the form. $column2 = ($_POST['column2_id']); if (empty($errors)) { require ('databaseconnect.php'); foreach ($column2 as $val ) { $query = "INSERT INTO tablename(item_id, column2_id) VALUES ('$item_id', '$val')"; mysql_query($query) or die( mysql_error() ); // echo $query . '<br />'; } if ($result) { echo 'items have been added'; } } else { echo 'system error. Not added'; echo '<p>' . mysql_error() . '<br><br>query:' . $query . '</p>'; } mysql_close(); } ?> <body> <FORM style="border: 1px dotted red; padding: 2px" action="" method="post"> item id field:<br> only enter numbers here<br> //this will be a hidden passed variable in finished form <input type="text" name="item_id" value="<?php if(isset($_POST['item_id'])) echo $_POST['item_id']; ?>" /> <br><br> characteristics:<br> <input type="checkbox" name="column2_id[]" value="1" > one <br> <input type="checkbox" name="column2_id[]" value="2" > two <br> <input type="checkbox" name="column2_id[]" value="3" > three <br> <input type="checkbox" name="column2_id[]" value="4" > four <br> <input type="checkbox" name="column2_id[]" value="5" > five <br> <input type="checkbox" name="column2_id[]" value="6" > six <br> <input type="checkbox" name="column2_id[]" value="7" > Seven <br> </fieldset><br><br> <input type="hidden" name="submitted" value="TRUE"> <input type="submit" /> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/208642-foreach-only-puts-first-check-box-in-db/#findComment-1090277 Share on other sites More sharing options...
turpentyne Posted July 23, 2010 Author Share Posted July 23, 2010 thanks on the info for how to post code here. unfortunately, removing the exit(); didn't help. It still only posted one item, and gave me a huge list of errors about connecting to the database. Quote Link to comment https://forums.phpfreaks.com/topic/208642-foreach-only-puts-first-check-box-in-db/#findComment-1090278 Share on other sites More sharing options...
Pikachu2000 Posted July 23, 2010 Share Posted July 23, 2010 Well, that's a start. At least we can narrow things down. Paste the errors here. Quote Link to comment https://forums.phpfreaks.com/topic/208642-foreach-only-puts-first-check-box-in-db/#findComment-1090280 Share on other sites More sharing options...
turpentyne Posted July 23, 2010 Author Share Posted July 23, 2010 ah... I was changing the names of things 'to protect the innocent' and forgot one. that explains the plant_id. It is the same as item_id Quote Link to comment https://forums.phpfreaks.com/topic/208642-foreach-only-puts-first-check-box-in-db/#findComment-1090283 Share on other sites More sharing options...
turpentyne Posted July 23, 2010 Author Share Posted July 23, 2010 hallelujah!!! the script Pikachu2000 posted is working. Now I have to go through it to see the differences between that script and mine, so I can understand where I was going wrong. Quote Link to comment https://forums.phpfreaks.com/topic/208642-foreach-only-puts-first-check-box-in-db/#findComment-1090286 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.