1

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.

1 Answer 1

3

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')";
Sign up to request clarification or add additional context in comments.

1 Comment

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. :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.