Jump to content

Insert in MySQL Array Value from Drop Select Box Value - Cannot Pass the Var


OldWest
 Share

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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 ! :D ... It works as normal now with $_POST[city]. SOLVED!

Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.