0

i am trying to insert to a table and get the ID of the new row.

$mysqli = new mysqli("localhost", "dbuser", "dbpass", "dbname");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

$query = "INSERT INTO table (tbl1,tbl2,tbl3,tbl4,tbl5) VALUES ('".$tbll."','".$tbl2."','".$tbl3."','0.00','".$tbl5."'"; 
$mysqli->query($query);
$id_log = $mysqli->insert_id;
  • id_log is returning 0 no error anywhere in logs
  • on same file i am doing SELECT & UPDATE and all work perfectly
8
  • 2
    Add closing bracket on VALUES at the end ). Commented Dec 8, 2013 at 18:30
  • 1
    @Rolice, Not a bracket.. Parenthesis ;) Commented Dec 8, 2013 at 18:33
  • As a side note, you're using mysqli, but not prepared statements. As a result, you are still open to SQL injection because you insert php variables directly. Granted, you could be doing things in code you haven't included, but if this is all your MySQL code, you should start using prepared statements and parametrized queries. Commented Dec 8, 2013 at 18:33
  • LOL! i wasted so much time on it..but i learned many new things on the way :D Commented Dec 8, 2013 at 18:42
  • 1
    I agree, @ShankarDamodaran, let it be parenthesis :) Commented Dec 8, 2013 at 21:03

1 Answer 1

3

You missed a parenthesis here...

$query = "INSERT INTO table (tbl1,tbl2,tbl3,tbl4,tbl5) VALUES ('".$tbll."','".$tbl2."','".$tbl3."','0.00','".$tbl5."')";
                                                           ----------------------------------------------------------^
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.