Parse Error

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cveselka
Forum Newbie
Posts: 8
Joined: Wed Apr 19, 2006 1:06 pm

Parse Error

Post 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]
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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
cveselka
Forum Newbie
Posts: 8
Joined: Wed Apr 19, 2006 1:06 pm

Post by cveselka »

OK that worked but now I'm getting unable to select database
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post 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)
cveselka
Forum Newbie
Posts: 8
Joined: Wed Apr 19, 2006 1:06 pm

Post by cveselka »

ok getting unable to slect database
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post 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)
User avatar
ambivalent
Forum Contributor
Posts: 173
Joined: Thu Apr 14, 2005 8:58 pm
Location: Toronto, ON

Post by ambivalent »

Code: Select all

$DBNAME != $DBName
Make the variable name consistent.
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

Yep, you are right. I didn't notice that.
cveselka
Forum Newbie
Posts: 8
Joined: Wed Apr 19, 2006 1:06 pm

Post 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?
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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.
Post Reply