Reputation: 3024
<?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);
}
?>
Upvotes: 2
Views: 17529
Reputation: 62884
Upvotes: 7
Reputation: 11
It's kind of late, but I see in php manual, the interface are below:
int mysql_insert_id ([ resource $link_identifier ] );
int mysqli_insert_id ( mysqli $link )
.So, I think:
mysqli_insert_id($link)
if you are mysqli style;mysqli_insert_id
function.Upvotes: 1
Reputation: 454960
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);
}
Upvotes: 1
Reputation: 38298
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.
https://www.php.net/mysql_insert_id
Upvotes: 13