Results 1 to 3 of 3

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

Hybrid View

  1. #1
    Join Date
    Mar 2013

    using Prepared Statement's to Insert data into a Database


    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->fetch()) {
    "Sorry. That product already exists.";
                } else {
    $qry2 = ("INSERT INTO products (name, short_desc, long_desc, category, price, shipping, qty)
    $productName', '$productShortDesc', '$productLongDesc', '$productCategory', '$productPrice','$productShipping', '$productQTY'");
                    if (
    $stmt $this->conn->prepare($qry2)) {
    //Add item to DB
    //Place image in folder
    $newname "$pid.jpg";
    move_uploaded_file($FILES['fileField']['tmp_name'], "../product_images/$newname");
                    } else {
    "Error adding new product, Please check all details and try again.";

  2. #2
    Join Date
    Aug 2004
    Do something in the final else block to log (or just output for now) $stmt->error so you can find out what went wrong.
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  3. #3
    Join Date
    Feb 2012
    bud, you are missing a bracket.

    $qry2 = (
    "INSERT INTO products (name, short_desc, long_desc, category, price, shipping, qty)
    $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:

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