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]')"; Link to comment https://forums.phpfreaks.com/topic/217407-insert-in-mysql-array-value-from-drop-select-box-value-cannot-pass-the-var/ 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()? Link to comment https://forums.phpfreaks.com/topic/217407-insert-in-mysql-array-value-from-drop-select-box-value-cannot-pass-the-var/#findComment-1128826 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. Link to comment https://forums.phpfreaks.com/topic/217407-insert-in-mysql-array-value-from-drop-select-box-value-cannot-pass-the-var/#findComment-1128829 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" Link to comment https://forums.phpfreaks.com/topic/217407-insert-in-mysql-array-value-from-drop-select-box-value-cannot-pass-the-var/#findComment-1128836 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! Link to comment https://forums.phpfreaks.com/topic/217407-insert-in-mysql-array-value-from-drop-select-box-value-cannot-pass-the-var/#findComment-1128842 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.