SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    Jul 2009
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    update basket table qty field

    1.I have a small shopping cart that needs to update the basket when products are added. if the product is already in the basket, the basket 'qty' field must be updated with '1' increment.

    2.Here is what I have done(not working).
    Everytime I add a product, it adds the data as a new record in the basket table and the if statement im running with
    PHP Code:
    mysql_num_rows($result); 
    dont seem to work.


    PHP Code:
    <?php

    // Include files
    require_once('config.php');

    session_start(); 

    //If Session not set, generate random generic session
    if($_SESSION['username'] == '')
      { 
        
    header("Location: ../login.php");
      }
    elseif (
    $_GET['action'] == 'performance_add')
      {
      
    // extract data from pd_performance table
    mysql_select_db("shop");
        
        
    $id $_GET['id'];
        
    $result mysql_query("SELECT * FROM pd_performance WHERE pd_id = $id");
        
        
    $row mysql_fetch_array($result);
        
    $pd_name $row['pd_name'];
        
    $pd_desc $row['pd_desc'];
        
    $pd_price $row['pd_price'];
        
    // Check if product is in basket table. if not - add to basket, if yes - increment qty field with '1'
        
    $result mysql_query("SELECT * FROM basket WHERE pd_name = $pd_name");
        
    $num_rows mysql_num_rows($result);
        if (
    $num_rows == 0)
          {
            
    $sql "INSERT INTO basket(pd_name, pd_desc, pd_price, qty)
                VALUES('
    $pd_name','$pd_desc', '$pd_price', '1')";
          }
        else  
          {
            echo 
    "i need code to update the qty field to increment with 1";
          }  
          
              
        if (!
    mysql_query($sql))
              {
              die(
    'Error: ' mysql_error());
              }
            
    mysql_close($con);
            
    header("Location: ../performance_parts.php");      
        }

    else
        {
        echo 
    "no query";
        }
    ?>

  2. #2
    SitePoint Guru mmarif4u's Avatar
    Join Date
    Dec 2006
    Location
    /dev/swat
    Posts
    619
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $up mysql_query("update table set qty='qty+1' where id ='$id'"); 
    Is this what you need?

  3. #3
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,246
    Mentioned
    59 Post(s)
    Tagged
    3 Thread(s)
    although this method is not portable to other databases, you should really make use of mysql's splendid INSERT... ON DUPLICATE KEY UPDATE syntax

    no need for an extra SELECT query to determine if the product is already in the basket -- just insert it, and if it's already there, the ON DUPLICATE KEY portion will say UPDATE qty=qty+1
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  4. #4
    SitePoint Enthusiast
    Join Date
    Jul 2009
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by r937 View Post
    although this method is not portable to other databases, you should really make use of mysql's splendid INSERT... ON DUPLICATE KEY UPDATE syntax

    no need for an extra SELECT query to determine if the product is already in the basket -- just insert it, and if it's already there, the ON DUPLICATE KEY portion will say UPDATE qty=qty+1
    problem resolved

    I used ON DUPLICATE KEY , thanks!

  5. #5
    SitePoint Wizard PHPycho's Avatar
    Join Date
    Dec 2005
    Posts
    1,201
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by mmarif4u View Post
    PHP Code:
    $up mysql_query("update table set qty='qty+1' where id ='$id'"); 
    Is this what you need?
    I think this one is correct.
    PHP Code:
    $up mysql_query("update table set qty=qty+1 where id ='$id'"); 
    (without quotes in qty+1)

  6. #6
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,246
    Mentioned
    59 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by PHPycho View Post
    (without quotes in qty+1)
    you should do the same here --
    Code:
    where id = $id
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •