Question:

How to properly Insert into MySQL from HTML drop down/ select form.

by  |  earlier

0 LIKES UnLike

I ask a similar question a little earlier but didn't get the exact answers I was looking for, or I did just more problems arose afterward.

Here's my original question:

http://answers.yahoo.com/question/index;_ylt=AqasyIF588vYIc3i2WdKOV_sy6IX;_ylv=3?qid=20080804085924AAo7GQs

I have the select form submitting information into the database but when viewing the field it created it displays "Array" instead of the value of the selected field (which is what I'm wanting it to display.

Here's some code I have for the form/php:

<? if ( $_POST['sendbutton'] ) { $sub = "1"; } ?>

<form enctype="multipart/form-data" action="<?php echo $PHP_SELF;?>" method="post" class="cform" id="cfo">

<?php

$region = $_POST['region'];

?>

<li id="li--3">

<label for="region"><span>Region</span></label>...

<select name="region" id="region" class="single" size="1">

<option value="" selected>Please Select One</option>

<option value="downtown">UT Area/Downtown</option>

<option value="north">North</option>

<option value="south">South</option>

<option value="east">East</option>

<option value="west">West</option>

</select>

</li>

<?php

mysql_query("INSERT INTO ".$db['prefix']."apartments (name, address, city, state, zip, description, phone, amenities, website, 1bed, 2bed, 3bed, 4bed, studio, timecode, additional, region) VALUES ('$name', '$address', '$city', '$state', '$zip', '$description', '$phone', '$amenities', '$website', '$br1', '$br2', '$br3', '$br4', '$studio', '".mktime()."', '$additional', '$region')") or die(mysql_error());

?>

</form>

Of course there's lot more code, but this is just little snippets I've extracted that I know the problem resolves around.

So basically once the form is submitted I need which value is selected on the select form to be sent into the database table's cell called region.

Again, it's currently displaying the text Array in the region column where I would like the value of the select option to be.

Thanks in advance.

 Tags:

   Report

1 ANSWERS


  1. There are a few problems with your code:

    First off, your mysql_query statement is bound to get you into trouble. If one of the fields has a &quot; &#039; &quot; in it, your query will fail. Look at the PHP.net site for the proper technique.

    Second, unless you&#039;re uploading files, I would remove the &#039;enctype&#039; of your form. This could cause unwanted problems.

    If you see the &quot;Array&quot; printed out, then necessarily you are dealing with an array. Print out the contents using &quot;print_r(my_array);&quot; to see what then contents are. This might give you a clue of what&#039;s going wrong.

    Now, I imagine that you have a SUBMIT button in your form. If so, I would place the MYSQL INSERT commands at the very start of your page, and when a user clicks the button MySubmitButton, then write this:

    if (!empty( $_POST[ &#039;MySubmitButton&#039; ] ))

    {

    // INSERT your stuff in MySQL...

    }

    Hope this helps! Good luck!

Question Stats

Latest activity: earlier.
This question has 1 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.