Jump to content

Challenge is how to Insert values from the multiple selection field to MySQL


lampquest

Recommended Posts


Thanks a bunch Ober.

1)It's a MySQL database.
2)have a connection to the database
3)and infact was able to insert data from all other fields in my form.

The only problem I have being able take all the values from a user's multiple selection and write it to the database field meant for that. If user selects 4 option values, my code is able to print all the selected values to the browser but not able all to the database....only one, ie the last selected option is being inserted.

This is how  my  current code looks like:

foreach ( $HTTP_POST_VARS as $key=>$value )
{
if ( gettype( $value ) == "array" )
{
print "$key ==
";
foreach ( $value as $two_dim_value )


print ".........$two_dim_value
";



}
else
{
print "$key == $value
\n";
}
}


// insertion into database



$reg_date = date("Y-m-d");

$sql = "insert into agent (email, fname, lname, licence, bus_name, bus_phone, bus_address, state, mobile, password, region, date) values ('$email', '$fname', '$lname', '$license', '$bname', '$bphone', '$baddress', '$state', '$mphone', '$password', '$two_dim_value', '$reg_date')";

$result = mysql_query($sql);
if (!$result)
    {
echo "<center><font color='red'>Query Error</font>

Error Inserting Entries !</center>";
    }
else
                  {

      // send a mail


                    }

The  $two_dim_value represents the selected values.

Any will be highly appreciated pls.

Femi
You are looping through the array and replacing the value of $two_dim_value everytime it loops through hence at the end of the loop $two_dim_value is equal to the last option. In order to insert all values into 1 field, you will have to create a string out of them using something like implode()

$new_string=implode(",", $array_name); /* this will place all values of an array into a string seperated by comma */

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.