Ralph
Ralph

Reputation: 53

Problems with "mysqli_insert_id"

need a little help with adding product to database. error reported on "mysqli_insert_id" line Here is my code:

// Add this product into the database now
    $sql = mysqli_query($myConnection,"INSERT INTO products (product_name, price, details, category, subcategory, date_added) 
        VALUES('$product_name','$price','$details','$category','$subcategory',now())") or die (mysql_error());
     $pid = mysqli_insert_id($sql);
    // Place image in the folder 
    $newname = "$pid.jpg";
    move_uploaded_file( $_FILES['fileField']['tmp_name'], "../inventory_images/$newname");
    header("location: inventory_list.php"); 
    exit();

Here is the Error it produces:

Warning: mysqli_insert_id() expects parameter 1 to be mysqli, boolean given in /.....line 45

"Funny thing is that it still inserts the text into database & it shows up on my site but I still get the Error if I add another product.

my table does have Auto_Increment set on id

Any help would be greatly appreciated. Ralph

Upvotes: 0

Views: 423

Answers (1)

Bill Karwin
Bill Karwin

Reputation: 562368

Check the documentation for mysqli_insert_id():

mixed mysqli_insert_id ( mysqli $link )

The argument to the function should be your $myConnection resource, not the $sql result from your INSERT statement.

When you run an INSERT statement, mysqli_query() returns TRUE on success or FALSE on failure. So you're passing the boolean value TRUE instead of a connection resource.

Upvotes: 3

Related Questions