Hi folks!
I got a little problem with my assignment.
The idea is to create online bookstore.
As far didn't have much troubles with the project, until now.
Problem occures when I'm trying to update/delete an entry to mySql.

if you take a look at line 93 - there is 3times equal mark, in this case update works nice, but while I'm trying to delete entry - typing 0
, than only quantity is removed form sql(equals to 0), but if you remove "=" than no metter what you type in, whole entry is been removed form sql - it should do this only after typing 0.

Hope you might help me,
many thanks in advance!

code goes here:

<?php
session_start();?>
<?php
if (isset($_REQUEST["update"])) // add was clicked
   {
    update_cart();
    display_output_page();
    }
       else
       {
          display_output_page();
        }
  function display_output_page()
   {
?>
<html>
<head><title>Displaying the book database table using PHP</title></head>
<h1>YOUR CATALOG</h1>
<p style ="margin-left: 42em"><a href = "books.php">[ Home</a> | <a href = "tisa.php"> Shopping Cart</a>
| <a href = "catalog.php"> Checkout ] </a></P>
<body>
<?php
$db_link = db_connect("project");
$self = $_SERVER['PHP_SELF'];
$quantity=$_REQUEST['quantity'];
$session_id = session_id();
$fields = mysql_list_fields("project", "books_new");
$num_columns = mysql_num_fields($fields);
$query = "SELECT cart.isbn, cart.price, cart.quantity, books_new.isbn, books_new.title, books_new.author, books_new.pub, books_new.year FROM cart,books_new WHERE cart.isbn=books_new.isbn";
//$query = "SELECT books_new.*, cart.* FROM books_new INNER JOIN cart ON books_news.isbn = cart.isbn";
$result    = mysql_query($query) or die("SQL query failed");
$row = mysql_num_rows($result);
$total = 0;
if($row == 0)
{
echo"<h1>Your Cart is Empty</h1>";
}
else
{
echo '<table border="1" cellpadding = "5" >', "\n";
            echo "<tr>\n";
                        echo "<th>Isbn</th>\n";
                        echo "<th>Title</th>\n";
                        echo "<th>Price</th>\n";
                        echo "<th>Author</th>\n";
                        echo "<th>Pub</th>\n";
                        echo "<th>Year</th>\n";
                        echo "<th>Quantity</th>\n";
                        echo "<th>Subtotal</th>\n";
                        echo "<th>Update</th>\n";
while ($row = mysql_fetch_assoc($result))
{
             $isbn        = $row['isbn'];
             $title      = $row['title'];
             $price        = $row['price'];
             $author        = $row['author'];
             $pub        = $row['pub'];
             $year        = $row['year'];
             $quantity    = $row['quantity'];
             $cost = $quantity*$price;
             $total= $cost + $total;
              echo "<tr>";
              echo "<td>".$row['isbn']."</td>";
              echo "<td>".$row['title']."</td>";
              echo "<td>".$row['price']."</td>";
              echo "<td>".$row['author']."</td>";
              echo "<td>".$row['pub']."</td>";
             echo "<td>".$row['year']."</td>";
?>
<form action="<?php echo $self ?>" method="POST">
     <td><input type = "text"     name = "quantity" style='background-color:yellow;'    value = "<?php echo $row['quantity'];?>"  size = "3" /></td>
        <input type="hidden"  name="isbn" value ="<?php echo $isbn?>">
        <input type="hidden"  name="price" value = "<?php echo $price?>">
        <?php echo "<td>".$row['price']*$row['quantity']."</td>";?>
    <td><input type='submit' name='update' value='update' style='background-color:#DEAAA2;'></td>
   </tr>
   </form>
<?php
}
echo "<tr><td><td><td><td><td><td><td>Total Price<td>$total</td></td></td></td></td></td></td></td></td>";
echo "</table>\n";
mysql_free_result($result);
mysql_close($db_link);
}
}
function update_cart()
{
    $db_link = db_connect("project");
    $session_id = session_id();
       $isbn = $_REQUEST['isbn'];
       $price = $_REQUEST['price'];
    $quantity=$_REQUEST['quantity'];
    if($quantity=== 0)
    {
    $query = "DELETE FROM cart WHERE isbn='$isbn' AND session_id= '$session_id'";
    //echo($query);
    $result = mysql_query($query) or ("<br>delete failed");
    }
else
{
$query1 = "UPDATE cart SET quantity = '$quantity' WHERE session_id = '$session_id' AND isbn='$isbn'";
$result1 = mysql_query($query1) or die("<br>Insertion failed");
}
}
function db_connect($db_name)
{
   $host_name = "localhost";
   $user_name = "root";
   $password = "usbw";
   $db_link = mysql_connect($host_name, $user_name, $password)
      or die("Could not connect to $host_name");
   mysql_select_db($db_name)
      or die("Could not select database $db_name");
   return $db_link;
}
?>
</form>
</body>
</html>

change it to (with only 2 = sign)

if($quantity==0 || trim($quantity)=="")
{
.
.
.
}

3 equals checks that the 2 variables have the same value AND type(strict)
2 equals just checks that the value matches(loose)

data coming from post and get vars come in a a string eg.

page.php?quantity=1

<?php
if($_GET['quantity'] == 1){
    echo 'loose match: integar 1<br/>';
}
if($_GET['quantity'] === 1){
    echo 'strict match: integar 1<br/>';
}
if($_GET['quantity'] == '1'){
    echo 'loose match: string "1"<br/>';
}
if($_GET['quantity'] === '1'){
    echo 'strict match: string "1"<br/>';
}
?>

Hi folks,
Thank you very much urtrivedi, indeed, you have fixed my code :)
Biiim - thank you for advise, have to say that didnt really know difference between 2 & 3 qual marks

Once agian, many thanks gusy!

Dont use the $_GET statement in Biiim statment, use $_POST. The only time you use GET statement, is if you are submitting your form using a url. in this case you would need to have the form method="GET", but your using method="POST"

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.