Help me look at the codes below. It has T_IF error and T_WHILE error. The errors are on line 7 and line 18. I hope anyone can teach me to repair it and the code function is still same.

<?php

require_once("db.php");

session_start()

if (!isset($_SESSION['cart'])) {     <---T_IF error
   $_SESSION['cart'] = array();  
}

$handle = mysql_connect("baccesso_web")
or die ("ERROR: Unable to open database!");

$query = "SELECT SKU, Name, Price FROM products";
$result = mysql_query($handle,$query)
or die ("ERROR: Cannot execute $query.");
mysql_error($handle)

if (mysql_num_rows($result)>0) {                         <--- T_IF error
   while ($row = mysql_fetch_object($result)) {          <--- T_WHILE error
    $sku = $row->Quantity
    $productInfo[$sku] = array()
    $productInfo[$sku]['Name'] = $row->Name;
    $productInfo[$sku]['Price'] = $row->Price;
    $productInfo[$sku]['Quantity'] = $row->Quantity;
    $productInfo[$sku]['Image'] = $row->Image;
   }
} else {
   die ("ERROR: Cannot retrieve product information");
}

mysql_close($handle);

switch($_POST['action']) {
   case 'add':
    foreach ($_POST['addQuantity'] as $sku => $quantity) {
       if (isset($quantity) && $quantity > 0) {
        $_SESSION['cart'][$sku] += $quantity;
       }
    }
    break;

   case 'update':
    foreach ($_POST['updateQuantity'] as $sku => $quantity) {
       if ($quantity == 0 && trim($quantity) != "") {
        unset($_SESSION['cart'][$sku]);
       }

       if ($quntity > 0) {
        $_SESSION['cart'][$sku]=$quantity;
       }
    }
    break;
   case 'reset':
    $_SESSION['cart'] = array();
   break;
}
?>

Recommended Answers

All 17 Replies

Missing semi-colon on line 5.

Line 15-17 should be this I guess:

$result = mysql_query($query, $handle) or die ("ERROR: Cannot execute $query: " . mysql_error($handle));

I doubt mysql_connect will work without a username specified.

The query won't run because you need to select a database first with mysql_select_db or specify the database in the query.

Have a look at the code snippet for MySQL.

You are missing a semicolon at the end of line 5, hence the T_IF error (if statement is not expected there if no semicolon):

session_start()

Same on line 17:

mysql_error($handle)

Thank you for both. The error solved. I have another question here.
Actually I am trying to build a shopping cart myself. I hope you can help me see my codes if I am able to build a shopping cart.

This is called "db.php"

    <?php

    mysql_connect("localhost", "baccesso_benny", "benny1998") or die(mysql_error());
    mysql_select_db("baccesso_web") or die(mysql_error());

    mysql_query("CREATE TABLE IF NOT EXISTS products(
    SKU VARCHAR(255)
    Name TEXT
    Price MONEY
    Quantity INT
    Image VARCHAR(80))")
     or die(mysql_error());  

    echo "Table Created!";

    ?>

This is called "cart.php" which is the 1st codes.

<?php

require_once("db.php");

session_start();

if (!isset($_SESSION['cart'])) {
   $_SESSION['cart'] = array();
}

$handle = mysql_connect("baccesso_web")
or die ("ERROR: Unable to open database!");

$query = "SELECT SKU, Name, Price FROM products";
$result = mysql_query($handle,$query)
or die ("ERROR: Cannot execute $query.");
mysql_error($handle);

if (mysql_num_rows($result)>0) {
   while ($row = mysql_fetch_object($result)) {
    $sku = $row->Quantity;
    $productInfo[$sku] = array();
    $productInfo[$sku]['Name'] = $row->Name;
    $productInfo[$sku]['Price'] = $row->Price;
    $productInfo[$sku]['Quantity'] = $row->Quantity;
    $productInfo[$sku]['Image'] = $row->Image;
   }
} else {
   die ("ERROR: Cannot retrieve product information");
}

mysql_close($handle);

switch($_POST['action']) {
   case 'add':
    foreach ($_POST['addQuantity'] as $sku => $quantity) {
       if (isset($quantity) && $quantity > 0) {
        $_SESSION['cart'][$sku] += $quantity;
       }
    }
    break;

   case 'update':
    foreach ($_POST['updateQuantity'] as $sku => $quantity) {
       if ($quantity == 0 && trim($quantity) != "") {
        unset($_SESSION['cart'][$sku]);
       }

       if ($quntity > 0) {
        $_SESSION['cart'][$sku]=$quantity;
       }
    }
    break;
   case 'reset':
    $_SESSION['cart'] = array();
   break;
}
?>

This is called "product.php"

<?php
require_once("cart.php");
require_once("db.php");
?>

<html>
<body>
<table border="0" width="100%" cell padding="10">
<tr>
<td valign="top">
   <u>Selected Products</u>
<?php
if (sizeof($_SESSION['cart']) > 0) {
?>
   <form action="<?=$_SERVER['PHP_SELF']?>" method="post">
   <ol>
<?php
   foreach ($productInfo as $sku => $quantity) {
    echo "<li><b>" . $productInfo[$sku]['name'] . "</b><br/>\n";
    echo $quantity . "units @ " . $productInfo[$sku]['price'] . " each<br/>\n";
    $subtotal = $quantity * $productInfo[$sku]['price'];
    $total += $subtotal;
    echo "Subtotal: " . number_format($subtotal, 2) . ",br/>\n";
    echo "New Quantity: <input type=text size=4 name=updateQuantity[$sku] />\n";
    echo "</li><p></p>\n";
   }
?>
   /ol>
   <b>Total: <?php echo number_format($total, 2); ?></b>
   <p></p>
   <input type="hidden" name="action" value="update">
   <input type="submit" value="Update Cart">
   </form>

   <form action="<?=$_SERVER['PHP_SELF']?>" method="post">
   <input type="hidden" name="action" value="reset">
   <input type="submit" value="Reset Cart">
   </form>

<?php
} else {
?>
   </p>
   No products selected.
<?php
}
?>

</body>
</html>
   </td>
</body>
</html>

What exactly is the question? Are you getting any errors?

The only strange thing I spotted is that you are building the $productInfo array using quantity as the key.

$productInfo[$sku] = array();

First: there is no Quantity field in the query and second: when you have two rows with the same quantity (the key) the existing value in the $productInfo will be overwritten.

The line of code below will produce html errors and contains two PHP errors:

 echo "New Quantity: <input type=text size=4 name=updateQuantity[$sku] />\n";

It should be:

 echo "New Quantity: <input type=\"text\" size=\"4\" name=\"{$updateQuantity[$sku]}\" />\n";

Note that I have used backslash to escape double quotes, added $ to updateQuantity array name and erapped $updateQuantity[$sku] in curly braces.

My question is I encountered this error.
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Name TEXT NOT NULL Price MONEY NOT NULL Quantity INT NOT NULL Image V' at line 3

<?php
$user="baccesso_benny";
$password="benny1998";
$database="baccesso_web";
mysql_connect("localhost", "$user", "$password") or die(mysql_error());
mysql_select_db("$database") or die(mysql_error());

$query="CREATE TABLE IF NOT EXISTS `products`(
`SKU` INT NOT NULL
`Name` TEXT NOT NULL
`Price` MONEY NOT NULL
`Quantity` INT NOT NULL
`Image` VARCHAR(80) NOT NULL)";
mysql_query($query)
or die(mysql_error());
echo "Table Created!";
mysql_close();
?>

What is the error ? The error log is:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'MONEY NOT NULL, Quantity INT NOT NULL, Image VARCHAR(80) NOT NULL)' at line 4

<?php
$user="baccesso_benny";
$password="benny1998";
$database="baccesso_web";
mysql_connect("localhost", "$user", "$password") or die(mysql_error());
mysql_select_db("$database") or die(mysql_error());

$query=("CREATE TABLE IF NOT EXISTS `products`(
`SKU` INT NOT NULL,
`Name` TEXT NOT NULL,
`Price` MONEY NOT NULL,
`Quantity` INT NOT NULL,
`Image` VARCHAR(80) NOT NULL)");
mysql_query($query)
or die(mysql_error());
echo "Table Created!";
mysql_close();
?>

Just eliminate the "Money" datatype.

Thank you for your helps. Here I need helps again.

Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in /home/baccesso/public_html/member/form.php on line 29

<?php

require_once("models/config.php");
if (!securePage($_SERVER['PHP_SELF'])){die();}

if(isUserLoggedIn()) { header("Location: account.php"); die(); }

require_once("models/header.php");

echo "
<body>
<div id='wrapper'>
<div id='top'><div id='logo'></div></div>
<div id='content'>
<h1>Form</h1>
<div id='left-nav'>";

include("left-nav.php");

echo "
</div>
<div id='main'>";

echo resultBlock($errors,$successes);

echo "
<div id='regbox'>
<form action="insert.php" method="post">
<p>
<label>Name:</label>
<input type='text' name='name' />
</p>
<p>
<label>Price:</label>
<input type='text' name='price' />
</p>
<p>
<label>Quantity;</label>
<input type='text' name='quantity' />
</p>
<label>Image:</label>
<input type='text' name='image' />
</p>
<input type="submit">
</form>
</div>
</div>
<div id='bottom'></div>
</div>
</body>
</html>";

?>

What's the error?

The reason are double quotes that you use in the middle of the string on line 28 above. If you want to use double quotes literaly in double quoted strings you have to escape them with backslashes like this <form action=\"insert.php\" method=\"post\">:

echo "
<div id='regbox'>
<form action=\"insert.php\" method=\"post\">
<p>
<label>Name:</label>
<input type='text' name='name' />
</p>
<p>
<label>Price:</label>
<input type='text' name='price' />
</p>
<p>
<label>Quantity;</label>
<input type='text' name='quantity' />
</p>
<label>Image:</label>
<input type='text' name='image' />
</p>
<input type=\"submit\">
</form>
</div>
</div>
<div id='bottom'></div>
</div>
</body>
</html>";

or use single quotes for all html attributes.

Or use heredoc syntax

echo <<<EOT
<div id='regbox'>
<form action=\"insert.php" method="post">
<p>
<label>Name:</label>
<input type='text' name='name' />
</p>
<p>
<label>Price:</label>
<input type='text' name='price' />
</p>
<p>
<label>Quantity;</label>
<input type='text' name='quantity' />
</p>
<label>Image:</label>
<input type='text' name='image' />
</p>
<input type="submit">
</form>
</div>
</div>
<div id='bottom'></div>
</div>
</body>
</html>";
EOT;

why can't I retrieve product information?

<?php
require_once("cart.php");
require_once("db.php");
?>

<html>
<body>
<?php
$query = mysql_query('SELECT Name, Price, Quantity, Image FROM products') 
 or die(mysql_error()); 
while($info = mysql_fetch_array( $query )) {
echo $info['name'] . " " . $info['price'] . " " . $info['quantity'] . " " . $info['image'] . "
";
}
?>
<tr>
<td valign="top">
   <u>Selected Products</u>
<?php
if (sizeof($_SESSION['cart']) > 0) {
?>
   <form action="<?=$_SERVER['PHP_SELF']?>" method="post">
   <ol>
<?php
   foreach ($productInfo as $sku => $quantity) {
    echo "<li><b>" . $productInfo[$sku]['name'] . "</b><br/>\n";
    echo $quantity . "units @ " . $productInfo[$sku]['price'] . " each<br/>\n";
    $subtotal = $quantity * $productInfo[$sku]['price'];
    $total += $subtotal;
    echo "Subtotal: " . number_format($subtotal, 2) . ",br/>\n";
    echo "New Quantity: <input type=\"text\" size=\"4\" name=\"{$updateQuantity[$sku]}\" />\n";
    echo "</li><p></p>\n";
   }
?>
   /ol>
   <b>Total: <?php echo number_format($total, 2); ?></b>
   <p></p>
   <input type="hidden" name="action" value="update">
   <input type="submit" value="Update Cart">
   </form>

   <form action="<?=$_SERVER['PHP_SELF']?>" method="post">
   <input type="hidden" name="action" value="reset">
   <input type="submit" value="Reset Cart">
   </form>

<?php
} else {
?>
   </p>
   No products selected.
<?php
}
?>

</body>
</html>
   </td>
</body>
</html>

Maybe you do not have short hand PHP tag enabled.

Above line 42:

<form action="<?=$_SERVER['PHP_SELF']?>" method="post">

Change to :

<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post">

Although, if you are new and starting fresh code, i would suggest you drop the MYSQL syntax usage and opt for either PDO with MySQL, or MySQLi.

Sometimes it is better to enclose column and table names in backticks to make sure they do not clash with mysql kkeyword:.

$query = mysql_query('SELECT `Name`, `Price`, `Quantity`, `Image` FROM `products`')
or die(mysql_error()); 

You can also try the query in phpmyadmin and see if you get any errors:

SELECT `Name`, `Price`, `Quantity`, `Image` FROM `products`

View the source in browser and check whether the html code for the form has been generated properly. In Firefox you right click and select View Page Source from the menu.

Maybe I'm the strange one, but I always put php tags at top and bottom, and echo out all of the html, in a block, and only use single quotes. That way I always know where I am. I even use single quotes as standard if I'm writing html, with doubes only when needed...... Works for me, but then i'm the only one reading/maintaining my code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.