Page 1 of 1

Parse Error

Posted: Wed Apr 19, 2006 1:11 pm
by cveselka
hawleyjr | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I have a form that redirects to a php page to inser data into mysql db.  Whenever I try to run I get the following error:

Parse error: parse error, unexpected T_VARIABLE in users.php on line 10


Below is the code.  I'm not sure what I am missing

Code: Select all

<?php

$DBHOST = 'localhost';
$DBUSER = '*******';
$DBPASS = '*****';
$DBNAME = 'Database here';

$dbconnect = mysql_connect($DBHOST,$DBUSER,$DBPASS) or die('Err:Unable to connect to database');

@mysql_select_db('$DBName') or die('Unable to select database '$DBName');

$sqlquery = INSERT INTO Users VALUES('$last_name');

$results = mysql_query($sqlquery);

mysql_close();

?>
hawleyjr | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Wed Apr 19, 2006 1:35 pm
by John Cartwright
you have 2 different errors

Code: Select all

@mysql_select_db('$DBName') or die('Unable to select database '$DBName');
should be

Code: Select all

@mysql_select_db('$DBName') or die('Unable to select database '.$DBName);
You wern't escaping your singles quotes.. use the concentating operator "." to escape the quote

Next,

Code: Select all

$sqlquery = INSERT INTO Users VALUES('$last_name');


should be

Code: Select all

$sqlquery = 'INSERT INTO Users VALUES(\''.$last_name.'\')';
You didn't even quote the query, but remember you must escape the single quotes to parse variables.

Won't hurt to read the manual on strings http://ca.php.net/manual/en/language.types.string.php

Posted: Wed Apr 19, 2006 2:28 pm
by cveselka
OK that worked but now I'm getting unable to select database

Posted: Wed Apr 19, 2006 2:39 pm
by Oren
Replace:

Code: Select all

@mysql_select_db('$DBName') or die('Unable to select database '.$DBName);
With:

Code: Select all

@mysql_select_db($DBName) or die('Unable to select database '.$DBName);
(changed: '$DBName' to: $DBName)

Posted: Wed Apr 19, 2006 2:59 pm
by cveselka
ok getting unable to slect database

Posted: Wed Apr 19, 2006 4:00 pm
by Oren
I hope that in the real code you don't have it like this:

Code: Select all

$DBNAME = 'Database here';
(Unless it is actually your database name)

Posted: Wed Apr 19, 2006 4:11 pm
by ambivalent

Code: Select all

$DBNAME != $DBName
Make the variable name consistent.

Posted: Wed Apr 19, 2006 4:13 pm
by Oren
Yep, you are right. I didn't notice that.

Posted: Wed Apr 19, 2006 4:13 pm
by cveselka
Found it. My DB name has a capital letter in the name. Now when I submit the form it goes to a blank page. How can I make it show me that it inserted into the db?

Posted: Thu Apr 20, 2006 1:09 pm
by timvw
You haven't bothered to read the manual... http://www.php.net/mysql_query
Return Values
For SELECT, SHOW, DESCRIBE or EXPLAIN statements, mysql_query() returns a resource on success, or FALSE on error.
For other type of SQL statements, UPDATE, DELETE, DROP, etc, mysql_query() returns TRUE on success or FALSE on error.