a 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
shehan31
Forum Commoner
Posts: 59
Joined: Sun Aug 29, 2010 5:24 am

a parse error

Post by shehan31 »

Deal all;
I have been having a parse error on line 8. This is a online shopping (cart) system.
Parse error: parse error in C:\wamp\www\pin project\cart.php on line 8

I am having problems to slove this and need some help.

Code: Select all

session_start();
$page = 'index.php';
mysql_connect('localhost','root','')or die (mysql_error('unable to connect to the database'));
mysql_select_db('cart')or die(mysql_error('error with the table'));
if (isset($_GET['add'])){
	$_SESSION['cart_'.$_GET['add']]+='1';
	$quantity = mysql_query('SELECT id, quantity FROM products WHERE id'= mysql_real_escape_string($_GET['add']));
	 while($quantity_row = mysql_fetch_assoc($quantity)){
	 		if ($quantity_row['quantity']!=$_SESSION['cart_'.$_GET['add']]){
				$_SESSION['cart_'.$_GET['add']]+='1';
				echo $_SESSION['cart_'];
			}
	 }
}
function products (){
	$get = mysql_query('SELECT id,name,description,price FROM products WHERE quantity>0 ORDER BY id DESC');
	  if (mysql_num_rows($get)==0){
	  	echo " there are no items to be dispalyed.";
	  }
	  else {
	  	while ($get_row = mysql_fetch_assoc($get)){
			echo '<p>'.$get_row['name'].'<br/>'.$get_row['description'].'<br/>'.$get_row['price'].'<a href="cart.php?add='.$get_row['id'].'">Add</a></p>';
		}
	  }
}
echo $_SESSION['cart_1'];

regards
shehan31
oscardog
Forum Contributor
Posts: 245
Joined: Thu Oct 23, 2008 4:43 pm

Re: a parse error

Post by oscardog »

Try this:

Code: Select all

<?php
session_start();
$page = 'index.php';
mysql_connect('localhost','root','')or die (mysql_error('unable to connect to the database'));
mysql_select_db('cart')or die(mysql_error('error with the table'));
if (isset($_GET['add'])){
        $_SESSION['cart_'.$_GET['add']]+='1';
        $quantity = mysql_query("SELECT id, quantity FROM products WHERE id='".mysql_real_escape_string($_GET['add']."'"));
         while($quantity_row = mysql_fetch_assoc($quantity)){
                        if ($quantity_row['quantity']!=$_SESSION['cart_'.$_GET['add']]){
                                $_SESSION['cart_'.$_GET['add']]+='1';
                                echo $_SESSION['cart_'];
                        }
         }
}
function products (){
        $get = mysql_query("SELECT id, name, description, price FROM products WHERE quantity > 0 ORDER BY id DESC");
          if (mysql_num_rows($get) == 0){
                echo "There are no items to be dispalyed.";
          }
          else {
                while ($get_row = mysql_fetch_assoc($get)){
                        echo '<p>'.$get_row['name'].'<br/>'.$get_row['description'].'<br/>'.$get_row['price'].'<a href="cart.php?add='.$get_row['id'].'">Add</a></p>';
                }
          }
}
echo $_SESSION['cart_1'];
?>
When writing queries it is best to encapsulate the actual query text using quotes (" ") rather than single quotes (' ') as you need to use single quotes around variables.

For example you wrote:

Code: Select all

$quantity = mysql_query('SELECT id, quantity FROM products WHERE id'= mysql_real_escape_string($_GET['add'])); 
It should be:

Code: Select all

$quantity = mysql_query("SELECT id, quantity FROM products WHERE id = '".mysql_real_escape_string($_GET['add']."'")); 
Notice in my code the way I have included variables compared to yours.
ModusPonens
Forum Newbie
Posts: 13
Joined: Sat Apr 09, 2011 11:57 am

Re: a parse error

Post by ModusPonens »

Another helpful tip on executing SQL queries. The first time through, always echo the string query on the line following the line you created the query on. It's easy to get confused between the literals and the variables and where you are with quotes, ticks, concatenations, semicolons, and parens.

If you echo the query string, it should be very apparent to you if you've constructed it properly.
shehan31
Forum Commoner
Posts: 59
Joined: Sun Aug 29, 2010 5:24 am

Re: a parse error

Post by shehan31 »

Hi;
This works and thank you very much. i am having another warning and a notice.
Notice: Undefined index: cart_3 in C:\wamp\www\pin project\cart.php on line 7

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\wamp\www\pin project\cart.php on line 10
I am confused with this cancatnated method.
Here are my Codes again.

Code: Select all

session_start();
$page = 'index.php';
mysql_connect('localhost','root','')or die (mysql_error('unable to connect to the database'));
mysql_select_db('cart')or die(mysql_error('error with the table'));
if (isset($_GET['add'])){
	$_SESSION['cart_'.$_GET['add']]+='1';
	
	$quantity = mysql_query("SELECT id, quantity FROM products WHERE id ='" .mysql_real_escape_string($_GET['add']."'"));
	 while($row=mysql_fetch_array($quantity)){
	 		if ($row['quantity']!=$_SESSION['cart_'.$_GET['add']]){
				$_SESSION['cart_'.$_GET['add']]+='1';
				//echo ($_SESSION['cart_1']);
			}
	 }
}

function products (){
	$get = mysql_query('SELECT id,name,description,price FROM products WHERE quantity>0 ORDER BY id DESC');
	  if (mysql_num_rows($get)==0){
	  	echo " there are no items to be dispalyed.";
	  }
	  else {
	  	while ($get_row = mysql_fetch_assoc($get)){
			echo '<p>'.$get_row['name'].'<br/>'.$get_row['description'].'<br/>'.$get_row['price'].'<a href="cart.php?add='.$get_row['id'].'">Add</a></p>';
		}
	  }
}
echo $_SESSION['cart_'];

Code: Select all

<?php require 'cart.php';?>

<html>
<head>
<body>
<?php products ()?>
</body>
</head>
</html>
regards
shehan31


oscardog wrote:Try this:

Code: Select all

<?php
session_start();
$page = 'index.php';
mysql_connect('localhost','root','')or die (mysql_error('unable to connect to the database'));
mysql_select_db('cart')or die(mysql_error('error with the table'));
if (isset($_GET['add'])){
        $_SESSION['cart_'.$_GET['add']]+='1';
        $quantity = mysql_query("SELECT id, quantity FROM products WHERE id='".mysql_real_escape_string($_GET['add']."'"));
         while($quantity_row = mysql_fetch_assoc($quantity)){
                        if ($quantity_row['quantity']!=$_SESSION['cart_'.$_GET['add']]){
                                $_SESSION['cart_'.$_GET['add']]+='1';
                                echo $_SESSION['cart_'];
                        }
         }
}
function products (){
        $get = mysql_query("SELECT id, name, description, price FROM products WHERE quantity > 0 ORDER BY id DESC");
          if (mysql_num_rows($get) == 0){
                echo "There are no items to be dispalyed.";
          }
          else {
                while ($get_row = mysql_fetch_assoc($get)){
                        echo '<p>'.$get_row['name'].'<br/>'.$get_row['description'].'<br/>'.$get_row['price'].'<a href="cart.php?add='.$get_row['id'].'">Add</a></p>';
                }
          }
}
echo $_SESSION['cart_1'];
?>
When writing queries it is best to encapsulate the actual query text using quotes (" ") rather than single quotes (' ') as you need to use single quotes around variables.

For example you wrote:

Code: Select all

$quantity = mysql_query('SELECT id, quantity FROM products WHERE id'= mysql_real_escape_string($_GET['add'])); 
It should be:

Code: Select all

$quantity = mysql_query("SELECT id, quantity FROM products WHERE id = '".mysql_real_escape_string($_GET['add']."'")); 
Notice in my code the way I have included variables compared to yours.
Peter Kelly
Forum Contributor
Posts: 143
Joined: Fri Jan 14, 2011 5:33 pm
Location: England
Contact:

Re: a parse error

Post by Peter Kelly »

for starters i dont think += is a valid expression if your trying to add 1 change line 6 to

Code: Select all

$_SESSION['cart_'.$_GET['add']]++;
as for the other error try run the sql query in phpmyadmin or something as that sounds like a mysql error rather than php.
ModusPonens
Forum Newbie
Posts: 13
Joined: Sat Apr 09, 2011 11:57 am

Re: a parse error

Post by ModusPonens »

$quantity = mysql_query("SELECT id, quantity FROM products WHERE id =' " .mysql_real_escape_string($_GET['add']." 'semicolon"));
First thing is...there's no semicolon in the sql query. You need to add one in where I wrote "semicolon."
oscardog
Forum Contributor
Posts: 245
Joined: Thu Oct 23, 2008 4:43 pm

Re: a parse error

Post by oscardog »

ModusPonens wrote:
$quantity = mysql_query("SELECT id, quantity FROM products WHERE id =' " .mysql_real_escape_string($_GET['add']." 'semicolon"));
First thing is...there's no semicolon in the sql query. You need to add one in where I wrote "semicolon."
You don't need a semi-colon. Even in the official PHP docs they don't use them.

As for your error, I am not sure. I don't really get what you're trying to do, and what you're doing seems somewhat cumbersome.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: a parse error

Post by McInfo »

Peter Kelly wrote:i dont think += is a valid expression
It is a valid operator. Find it in this list.

However, (shehan31) this is a rather precarious statement.

Code: Select all

$_SESSION['cart_'.$_GET['add']]+='1';
It is not safe to assume that $_GET['add'] holds valid data just because it is set, but if it happens to hold "3" (and not something unexpected like "ha, ha, got you!"), the statement could be reduced to this.

Code: Select all

$_SESSION['cart_3'] += '1';
Just as you tested $_GET['add'] with isset(), $_SESSION['cart_3'] must be tested. If it is not set, the += assignment results in an "undefined index" error because it tries to read from a variable that has not yet been assigned a value.

If $_SESSION['cart_3'] is a number, I'm not sure why a string is being added to it. It's not fatal in PHP to use a numeric string in place of a number, but it doesn't make as much sense as adding the number 1 or using the increment operator (++) as Peter suggested. If instead you're trying to append the character '1' to the end of whatever string is expected to be in $_SESSION['cart_3'], then use the concatenating assignment operator (.=).
shehan31
Forum Commoner
Posts: 59
Joined: Sun Aug 29, 2010 5:24 am

Re: a parse error

Post by shehan31 »

hello everyone;
nothing works and it gives me the same result.
mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\wamp\www\pin project\cart.php on line 10
.
Is there any other way of doing this. I am also bit confused with this session declaration and concatanation. Actually I am following a you tube viedo tutorial and I have done exactley what it says.
http://www.youtube.com/results?search_q ... t+2%2F7%29
Regards
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: a parse error

Post by McInfo »

Actually, the code you posted earlier is different from the tutorial code at the end of Part 3/7. Compare line-by-line and even character-by-character.

Your error comes from a misplaced parenthesis on this line which causes the apostrophe to be concatenated with $_GET['add'] instead of the query string, resulting in the apostrophe being escaped and thus invalidating the query. Also, the (int) type cast is missing.

Code: Select all

$quantity = mysql_query("SELECT id, quantity FROM products WHERE id ='" .mysql_real_escape_string($_GET['add']."'"));
shehan31
Forum Commoner
Posts: 59
Joined: Sun Aug 29, 2010 5:24 am

Re: a parse error

Post by shehan31 »

Hi McInfo;
nothing seems to be working at the moment. Without the integer part (int), it must work as I think.
Notice: Undefined index: cart_3 in C:\wamp\www\pin project\cart.php on line 7

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\wamp\www\pin project\cart.php on line 10

Notice: Undefined index: cart_ in C:\wamp\www\pin project\cart.php on line 29
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: a parse error

Post by McInfo »

The causes of those errors have been explained to you, but I will summarize.

The first error is partly the result of a problem that exists in the tutorial code. The author of the videos would have encountered the same error if he had enabled the display_errors and error_reporting configuration options. He had them disabled, which is improper in a development environment. You, correctly, have them enabled. The error can be overcome by improving on the tutorial code.

All three errors are a result of not transcribing the code exactly. At least two lines that were deleted in the "Part 3/7" video remain in your code. The misplaced parenthesis is the most likely cause of the second error, as I have already explained.
Post Reply