Jump to content

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STR


Recommended Posts

Hi all,

Does anybody can help me with this error...?

 

I am trying create a simple form which insert data into mysql table called 'sample'

 

Here is my code...

 


<?php

$connection = mysql_connect("localhost","root", "123");

	if(!$connection)
	{
		die("db connection error" .mysql_error());
	}

$db_select = mysql_select_db("project", $connection);

	if(!$db_select)
	{
		die("db select error" .mysql_error());
	}

$sql = "INSERT INTO sample (id,firstname,lastname,bio,gender)
	    VALUES ('$_POST['ID_']','$_POST['firstname']','$_POST['lastname']','$_POST['bio']','$_POST['gender']')";

	if(!$sql)		
	{
		die('Error: ' . mysql_error());
	}


	echo "1 record added";

?>


 

 

And every time I am getting this annoying error

 

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in C:\wamp\www\project\process.php on line 19

 

and Line 19 is

('$_POST['ID_']','$_POST['firstname']','$_POST['lastname']','$_POST['bio']','$_POST['gender']')";

 

Thanks in advance..! :-)

1. never insert $_POST values directly into a query, this makes the script vulnerable to SQL injection.

Run the values through mysql_real_escape_string if the values are strings, and either cast them to type int or use intval for integers.

 

2. To answer your actual question, this is a concatenation error, the line in question should read:

 

$sql = "INSERT INTO sample (id,firstname,lastname,bio,gender)
	    VALUES ('{$_POST['ID_']}','{$_POST['firstname']}','{$_POST['lastname']}','{$_POST['bio']}','{$_POST['gender']}')";

1. never insert $_POST values directly into a query, this makes the script vulnerable to SQL injection.

Run the values through mysql_real_escape_string if the values are strings, and either cast them to type int or use intval for integers.

 

2. To answer your actual question, this is a concatenation error, the line in question should read:

 

$sql = "INSERT INTO sample (id,firstname,lastname,bio,gender)
	    VALUES ('{$_POST['ID_']}','{$_POST['firstname']}','{$_POST['lastname']}','{$_POST['bio']}','{$_POST['gender']}')";

 

Hi thanks for your explanation and advice... :-) It works...

 

I just started to learn PHP a few hours ago....

 

 

 

well, whatever reference told you to add $_POST values into a query like that (if any), throw it out.

 

thanks, :-)

 

I'm following w3schools...

 

don't.

Use a resource like tizag or books.

The PHP Manual has tuts as well.

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.