Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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 get 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] => [email protected] 

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.

share|improve this question

1 Answer

up vote 3 down vote accepted

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')";
share|improve this answer
damn, you are quick... :) – Gergely Havlicsek Jun 1 '11 at 16:40
Hi and thanks for your quick response @tofutim, I've just amended it but when I view the database these fields are still not populated, this leads me to think the field types in the database are not right or something. I'll change some of these field types and see if this fixes the problem. But thanks for pointing out what I was doing wrong. :-) – Novice_2011 Jun 1 '11 at 16:56

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.