Problem inserting array data into database
Would someone please have at look at the code below, which is to process data submitted via a form and insert this data into a MySQL database.
<? $con = mysql_connect("localhost","username","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("database_name", $con);
$name=mysql_real_escape_string(serialize($_POST['fullname15']));
$bdate=mysql_real_escape_string(serialize($_POST['birthdate19']));
$sql="INSERT INTO bookings (full_name, dob, email) VALUES ('$_POST[name]','$_POST[bdate]','$_POST[email]')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "Form Sent to Database";
mysql_close($con)
?>`
I used a form builder called JotForm to build my form and when the form is submitted it sends the data to my process.php script above. The problem is I can't g开发者_运维问答et some of this data into my database. At the moment the only field that is successful is the email one.
The data is being submitted to my script successfully because the 'var_dump' shows me:
[fullname15] => Array ( [0] => Joe [1] => Bloggs )
[birthdate19] => Array ( [0] => 14 [1] => November [2] => 1921 )
[email16] => joebloggss@sample.com
I am new to programming and having tried for several hours to get the fullname and birthdate to insert into my database fields I've almost giving up. Any help would be kindly appreciated.
You are not sending anything into the $sql statement. $_POST['name'], for example, is empty. To debug in the future, print out $sql for examination.
$name=mysql_real_escape_string(serialize($_POST['fullname15']));
$bdate=mysql_real_escape_string(serialize($_POST['birthdate19']));
$email=mysql_real_escape_string(serialize($_POST['email16']));
$sql="INSERT INTO bookings (full_name, dob, email) VALUES ('$name','$bdate','$email')";
精彩评论