简体   繁体   中英

mysql_insert_id() not working

<?php

$userid = $_SESSION['user_id'];
$price = $ad['price'];
$owner = $ad['owner']; //owner of advertisement

//make a bid for this advertisement
$query = "INSERT INTO bids (id, ad, bidder, bid, bidwhen, owner, quantity)
         VALUES (NULL, '$adid', '$userid','$price', now(), '$owner', 1)";
$bidData = mysqli_query($dbc, $query);

$message = $_POST['message']; //message to owner

if ($message != "") { //if message box is not empty insert comment
    $title = $ad['title'];
    $bidid = mysql_insert_id($bidData); //Line 123 get last id of bid insert and put it into message query for reference

    $query = "INSERT INTO messages (sentto, sentfrom, sentat, message, title, bid)
    VALUES ('$owner', '$userid', now(), '$message', '$title', '$bidid')";
    $messageData = mysqli_query($dbc, $query);
}

?>

Error message:

mysql_insert_id() expects parameter 1 to be resource, boolean given

When i dont pass a parameter i get this error message:

Warning: mysql_insert_id() [function.mysql-insert-id]: Access denied for user 'ODBC'@'localhost' (using password: NO) in C:\Users\Jonny\Desktop\projects\xampp\htdocs\phpprojects\lets\ad.php on line 123

Warning: mysql_insert_id() [function.mysql-insert-id]: A link to the server could not be established in C:\Users\Jonny\Desktop\projects\xampp\htdocs\phpprojects\lets\ad.php on line 123

Solution Code:

<?php

$userid = $_SESSION['user_id'];
$price = $ad['price'];
$owner = $ad['owner']; //owner of advertisement

mysqli_close($dbc);
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
if (!$dbc) {
    die('Could not connect: ' . mysqli_error());
}
mysqli_select_db($dbc, 'databasename');

//make a bid for this advertisement
$query = "INSERT INTO bids (id, ad, bidder, bid, bidwhen, owner, quantity)
         VALUES (NULL, '$adid', '$userid','$price', now(), '$owner', 1)";
$bidData = mysqli_query($dbc, $query);

$message = $_POST['message']; //message to owner

if ($message != "") { //if message box is not empty insert comment
    $title = $ad['title'];
    $bidid = mysql_insert_id($dbc); //get last id of bid insert and put it into message query for reference

    $query = "INSERT INTO messages (sentto, sentfrom, sentat, message, title, bid)
    VALUES ('$owner', '$userid', now(), '$message', '$title', '$bidid')";
    $messageData = mysqli_query($dbc, $query);
}
?>

The argument passed to mysql_insert_id is the resource of a database connection. You're feeding it the result of a MySQL query. Just mysql_insert_id() by itself should work unless you're opening multiple database connections.

http://us3.php.net/mysql_insert_id

  1. What pygorex1 said. You don't want to pass the result of a query to *insert_id()
  2. Why are you using mysql i _query(), but mysql_insert_id()? If you're using mysqli, use myqli_insert_id .

It's kind of late, but I see in php manual, the interface are below:

  1. int mysql_insert_id ([ resource $link_identifier ] );
  2. int mysqli_insert_id ( mysqli $link ) .

So, I think:

  1. You have to use mysqli_insert_id($link) if you are mysqli style;
  2. You should always pass the link identifier to mysqli_insert_id function.

You are not checking the result of mysqli_query , you should be doing it like:

if (($bidData = mysqli_query($dbc, $query) !== true) {
       printf("Error: %s in query %s\n", $mysqli->error,$query);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM