Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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