<?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);
}
?>
错误讯息:
mysql_insert_id() expects parameter 1 to be resource, boolean given
当我没有传递参数时,我收到此错误消息:
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
解决方案代码:
<?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);
}
?>
答案 0 :(得分:12)
传递给mysql_insert_id
的参数是数据库连接的资源。你正在为它提供MySQL查询的结果。只有mysql_insert_id()
本身应该工作,除非您打开多个数据库连接。
答案 1 :(得分:6)
答案 2 :(得分:1)
您没有检查mysqli_query
的结果,您应该这样做:
if (($bidData = mysqli_query($dbc, $query) !== true) {
printf("Error: %s in query %s\n", $mysqli->error,$query);
}
答案 3 :(得分:1)
有点迟了,但我在php手册中看到,界面如下:
int mysql_insert_id ([ resource $link_identifier ] );
int mysqli_insert_id ( mysqli $link )
。所以,我认为:
mysqli_insert_id($link)
; mysqli_insert_id
函数。