www.webdeveloper.com
Results 1 to 3 of 3

Thread: using Prepared Statement's to Insert data into a Database

  1. #1
    Join Date
    Mar 2013
    Posts
    1

    using Prepared Statement's to Insert data into a Database

    HI,

    I am building a PHP and Mysqli based shopping cart for my UNI project. I have been using prepared statements for everything so far and they work great.

    However I have hit my first problem. I cannot seem to insert data into the database using a prepared statement.

    I have written a function that first checks to see if a product already exists. this works well and if that product does not exist it should run the prepared stmt and insert the data. However it is skipping over the insert part and going straight to the 'else' section of the 'if' stating that a product could not be uploaded.

    Here is the function that is not working. As before the first part works well, just have a problem when it actually comes to add the product.

    PHP Code:
    function addProduct($productName$productPrice$productCategory$productShortDesc$productLongDesc$productShipping$productQTY) {
            
    //Check if item already exists
            
    $qry "Select id FROM products WHERE name = ? LIMIT 1";
            if (
    $stmt $this->conn->prepare($qry)) {
                
                
    $stmt->bind_param('s'$productName);
                
    $stmt->execute();
                
    $stmt->bind_result($p_id);
                
                if(
    $stmt->fetch()) {
                    
                    echo 
    "Sorry. That product already exists.";
                    exit();    
                    
                } else {
                    
    $qry2 = ("INSERT INTO products (name, short_desc, long_desc, category, price, shipping, qty)
                    VALUES('
    $productName', '$productShortDesc', '$productLongDesc', '$productCategory', '$productPrice','$productShipping', '$productQTY'");
                    
                    if (
    $stmt $this->conn->prepare($qry2)) {
                    
    //Add item to DB
                                
                        
    $stmt->execute();
                        
                        
    $stmt->insert_id;
                        
    //Place image in folder
                        
    $newname "$pid.jpg";
                        
    move_uploaded_file($FILES['fileField']['tmp_name'], "../product_images/$newname");
                        
                    } else {
                        
                        echo 
    "Error adding new product, Please check all details and try again.";
                        
                    }
                        
                }
                
            }
        
        } 
    Regards
    Chris

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    18,925
    Do something in the final else block to log (or just output for now) $stmt->error so you can find out what went wrong.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Feb 2012
    Location
    youTUBE
    Posts
    234
    bud, you are missing a bracket.

    $qry2 = (
    "INSERT INTO products (name, short_desc, long_desc, category, price, shipping, qty)
    VALUES('
    $productName', '$productShortDesc', '$productLongDesc', '$productCategory', '$productPrice','$productShipping', '$productQTY'");
    Knowledge is that which can be shown to be the case, and Intelligence is the method one uses to deploy the demonstration of what is the case, everything else is Information.

    For a stronger Text reading try:
    http://www.wyc3.com/diction.php

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles