OldWest Posted November 1, 2010 Share Posted November 1, 2010 I am simply trying to insert a value generated from an array in a while loop, but it seems the value is not global and I can't pass it as I like. I did some research on this, but could not find an answer that solved my issue... Here is my select box code which is working perfect: <select name="city"> <?php $sql = "SELECT id, city_name FROM cities ". "ORDER BY city_name"; $results_set = (mysqli_query($cxn, $sql)) or die("Was not able to produce the result set!"); while($row = mysqli_fetch_array($results_set)) { echo "<option value=$row[id]>$row[city_name]</option>"; } ?> </select> Are any variables defined in a while loop global to the while loop only? Here is my SQL which you can see my $row[id] being passed thru field city_id... The value is being generated as supposed to based on value like: value="1", value="2" etc.. for the select options for each city name. So the values are there... But I CANNOT get that numerical id to pass to the database when submitting my form. Any ideas for a workaround to get this value passing as normal? if (isset($_POST['addPosting'])) { $query = "INSERT INTO Postings (id, city_id, title, description) VALUES ('','$row[id]','$_POST[title]','$_POST[description]')"; Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted November 1, 2010 Share Posted November 1, 2010 Is anything else being inserted? Are you checking for errors with mysqli_error()? Quote Link to comment Share on other sites More sharing options...
OldWest Posted November 1, 2010 Author Share Posted November 1, 2010 Is anything else being inserted? Are you checking for errors with mysqli_error()? All values are being passed perfetly fine. I added mysqli_error() to the insert query, and no error is being thrown. Apparently the value is not being passed outside the while loop... I know I am missing something important in this process, but can't put my finger on it. Lost.. going to keep trying at it. Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted November 1, 2010 Share Posted November 1, 2010 On second look, why are you trying to use $row['id'] to insert after a form submission? Should probably be $_POST['city'] . . . Also, your associative array keys should be quoted. If you're having problems echoing them with the quotes, either use string concatenation, "string stuff " . $array['index'] . "more string" or enclose them in curly braces, "string stuff {$array['index']} more string" Quote Link to comment Share on other sites More sharing options...
OldWest Posted November 1, 2010 Author Share Posted November 1, 2010 On second look, why are you trying to use $row['id'] to insert after a form submission? Should probably be $_POST['city'] . . . Also, your associative array keys should be quoted. If you're having problems echoing them with the quotes, either use string concatenation, "string stuff " . $array['index'] . "more string" or enclose them in curly braces, "string stuff {$array['index']} more string" I can't believe I was screwing up that badly ! ... It works as normal now with $_POST[city]. SOLVED! Quote Link to comment 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.