...

View Full Version : unexpected T_string



jmr3460
03-04-2009, 01:48 AM
What is wrong with this insert. This is the error:
Parse error: syntax error, unexpected T_STRING in /home3/simplic5/public_html/php/index.php on line 48

This is the code:

<?php
if (isset($_POST['submit'])){
if ($_POST['submit'] == 'save'){
query ('INSERT INTO james (uid, name, age, email, comment) VALUES (NULL, \'$_POST['name'] \', \'$_POST['age']\', \'$_POST['email']\', \'$_POST['comment']\'');
}
}


?>


I know that you guys hear this all the time. I got ahold of some video tuts and I typed this as instructed but I can not find the problem, I have double checked a thousand times. I read that it could be missing a line terminator(;) or double quotes.

So you will know what I have learned. This statement should be saying that if the form isset then if submit is save then insert the arrays into the table called james. The line that starts with query if line 48.

shadowmaniac
03-04-2009, 02:16 AM
Your query is "terminating" prematurely as you're not escaping the single quotes within the $_POST[]s

jmr3460
03-04-2009, 02:22 AM
I thought that that was what the backslashes were for.

jmr3460
03-04-2009, 02:33 AM
I tried something.




<?php
if (isset($_POST['submit'])){
if ($_POST['submit'] == 'save'){
query ('INSERT INTO james (uid, name, age, email, comment) VALUES (NULL, '$_POST[\'name\']', '$_POST[\'age\']', '$_POST[\'email\']', '$_POST[\'comment\']'')
}
}


?>


And I got a different error:

Parse error: syntax error, unexpected T_VARIABLE in /home3/simplic5/public_html/php/index.php on line 48

sea4me
03-04-2009, 02:36 AM
Try this:


<?php
if (isset($_POST['submit'])){
if ($_POST['submit'] == 'save'){
query ("INSERT INTO james (uid, name, age, email, comment) VALUES (NULL, '$_POST['name']', '$_POST['age']', '$_POST['email']', '$_POST['comment']");
}
}
?>

Using " is the same as ' and since you have ' where post is, you should use " for the outer one (sorry if I'm confusing)
Your also forgot the ; after the query()

BTW: query is function right?
Not mysql_query right?

jmr3460
03-04-2009, 02:49 AM
Yes query is been made a function with a statement that goes like this:


function query($mysql_query){

}

I added the double quotes and the semicolon and now I get this error.
Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home3/simplic5/public_html/php/index.php on line 48

shadowmaniac
03-04-2009, 03:04 AM
What I meant in my post above is something like this:


query ('INSERT INTO james (uid, name, age, email, comment) VALUES (NULL, \'$_POST[\'name\'] \', \'$_POST[\'age\']\', \'$_POST[\'email\']\', \'$_POST[\'comment\']\'');

Besides the opening and closing quote for the whole query, you have to escape every other quotes in-between. Though honestly, I've never written a query like that so I'm not even sure if it works or not :P

My preferred way of writing queries:


query ("INSERT INTO james (uid, name, age, email, comment) VALUES (NULL,
'".$_POST['name']."',
'".$_POST['age']."',
'".$_POST['email']."',
'".$_POST['comment']."');

Broke the query into multiple lines so you can see the quotes properly; gets kinda confusing sometimes.

sea4me
03-04-2009, 03:07 AM
shadowmaniac: you forgot to end your " before ) :)


query ("INSERT INTO james (uid, name, age, email, comment) VALUES (NULL,
'".$_POST['name']."',
'".$_POST['age']."',
'".$_POST['email']."',
'".$_POST['comment']."'");

sea4me
03-04-2009, 03:09 AM
Yes query is been made a function with a statement that goes like this:


function query($mysql_query){

}

I added the double quotes and the semicolon and now I get this error.
Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home3/simplic5/public_html/php/index.php on line 48

Did you just add the " and the ; ?

Replace the whole code with

<?php
if (isset($_POST['submit'])){
if ($_POST['submit'] == 'save'){
query ("INSERT INTO james (uid, name, age, email, comment) VALUES (NULL, '$_POST['name']', '$_POST['age']', '$_POST['email']', '$_POST['comment']");
}
}
?>

shadowmaniac
03-04-2009, 03:13 AM
shadowmaniac: you forgot to end your " before ) :)

Indeed I did; glad I mentioned it gets confusing too *whistles*

jmr3460
03-04-2009, 03:16 AM
Hey thanks I had to add a double at the end and that took care of my error. I now need to find out why I can't add to my table. Thank you for the help!!!!!!!!!!!!!!! Thanks to both.

sea4me
03-04-2009, 04:01 AM
Your welcome! :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum