Jump to content

Insert problems from form (with error)


nomadsoul

Recommended Posts

G'day Freaks

 

MySql version 5.1.4

 

MyTable:

id -ai pk

firstname varchar(20)

lastname varchar(20

dob date(yyyy-m-d)

gender varchar(1)

 

MyProblem

 

Heres the error after clicking submit:

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in C:\xampp\htdocs\nomadsoul\mysql_insert.php  on line 21(the sql variable)

 

Here's the code (with php and html):

 

<html>

<body>

 

<form action="mysql_insert.php" method="post">

Firstname: <input type="text" name="firstname" />

Lastname: <input type="text" name="lastname" />

Dob: <input type="text" name="dob" />

Gender: <input type="text" name="gender" />

<input type="submit" />

</form>

 

</body>

</html>

<?php

require ("connect.php");

$firstname = $_POST['firstname'];

$lastname = $_POST['lastname'];

$dob = $_POST['dob'];

$gender = $_POST['gender'];

$sql="INSERT INTO people(firstname, lastname, dob, gender)
VALUES('$_POST['firstname']','$_POST['lastname']','$_POST['dob']','$_POST['gender'])";

 

when I comment out the query the error goes away and the form shows.

 

The query doesn't need place values for the id ai does it?  I'm pretty sure it dosen't

Hope I've provided enough info.  Sorry if I didn't

All of my other php CRUDs  are working.

This last Insert script has been the most stubborn.

 

 

?>

Link to comment
https://forums.phpfreaks.com/topic/224682-insert-problems-from-form-with-error/
Share on other sites

There is simple problem with insert query. Please write it below:

$sql="INSERT INTO people(firstname, lastname, dob, gender) VALUES

('".$_POST['firstname']."','".$_POST['lastname']."',".$_POST['dob'].",'".$_POST['gender']."')";

 

 

It is a good practice to store all forms value in a variable and than use in different place.

Used the query function like this:  $sql=mysql_query("INSERT INTO people(firstname, lastname, dob, gender) VALUES

('".$_POST['firstname']."','".$_POST['lastname']."',".$_POST['dob'].",'".$_POST['gender']."')");

 

and now INSERT is working great.

 

Thanks again suresh

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.