Page 1 of 1

Parse error: syntax error, unexpected $end

Posted: Sun Aug 02, 2009 1:58 pm
by crazymao
edit: nvm i solved it. instead i have a different problem. here is my code:
<?php
$newuser = $_get['$newuser'];
$con = mysql_connect("","","");

if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("shalev", $con);

mysql_query("INSERT INTO userinfo (name)
VALUES ($newuser)");

mysql_close($con);
echo "Success"
?>

what i want to accomplish is to insert into blabla the value recalled from the previous page, $newuser. but when i check my database it doesnt insert.

Re: Parse error: syntax error, unexpected $end

Posted: Sun Aug 02, 2009 2:36 pm
by koen.h
$_get['$newuser'];

What is this supposed to do?

Re: Parse error: syntax error, unexpected $end

Posted: Sun Aug 02, 2009 2:57 pm
by jackpf
Use mysql_error().

Re: Parse error: syntax error, unexpected $end

Posted: Sun Aug 02, 2009 3:01 pm
by cpetercarter
php will generate error messages which will probably explain the problem to you. But you have to set up your script properly to generate and read them. There is no point in putting " echo 'Success'" at the end if you don't first check that the operation has in fact been successful. You can do this by amending the mysql_query line to :

Code: Select all

$result = mysql_query("INSERT INTO userinfo (name) VALUES ($newuser)");
if (!$result) echo "Arrgh! Something's wrong : ".mysql_error();
 
This works because mysql_query returns a value of "false" if the operation fails.
A possible reason why the script is not working is that you are trying to enter an inadmissable string into the database. It is always a good idea to escape the string first, like this :

Code: Select all

$newuser = mysql_real_escape_string($newuser);
 
Commas in strings also cause problems, as MySQL is likely to treat them as marking the end of the query.

Re: Parse error: syntax error, unexpected $end

Posted: Sun Aug 02, 2009 3:36 pm
by Mark Baker
As koen.h has already pointed out:

Code: Select all

$newuser = $_get['$newuser'];
should probably be

Code: Select all

$newuser = $_GET['$newuser'];
Variables, including superglobals like $_GET, are case-sensitive

Re: Parse error: syntax error, unexpected $end

Posted: Sun Aug 02, 2009 10:53 pm
by crazymao
thanks for your replies.

cpetercarter, i have inserted the code you suggested to find out what the problem is and it gives my this: Column count doesn't match value count at row 1

i am not sure what it means, especially that my code doesnt include rows and columns. any help will be appreciated

Re: Parse error: syntax error, unexpected $end

Posted: Mon Aug 03, 2009 2:34 am
by cpetercarter
The first thing to do when you get an error message that you don't understand is to google it.

You do have rows and columns, even if you don't realise it.The "columns" in your table are the names of the various fields - things like "id", "name", "phone_number", "favourite_sport" etc. The "rows" are the individual records - things like "2", "Fred", "123 45 67 89", "table tennis". The error is telling you that the number of values in your INSERT query do not match the number of columns which you have specified.

This is on the face of it odd, because you have specified one column ('name') and one value ('$username'), so the numbers do match. My guess is that the error message is telling you either :
that the value is null or invalid. Follow Mark Baker's suggestion and replace $_get['username'] with $_GET['username'].
that you do not have any columns in the table. If you run the "SHOW TABLE" sql query, it should output the necessary info about the table