[英]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);
}
?>
错误信息:
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);
}
?>
传递给mysql_insert_id
的参数是数据库连接的资源。 您正在向它提供MySQL查询的结果。 除非您要打开多个数据库连接,否则仅mysql_insert_id()
本身应该可以工作。
有点晚了,但是我在php手册中看到,界面如下:
int mysql_insert_id ([ resource $link_identifier ] );
int mysqli_insert_id ( mysqli $link )
。 所以我认为:
mysqli_insert_id($link)
; mysqli_insert_id
函数。 您没有检查mysqli_query
的结果,应该这样做:
if (($bidData = mysqli_query($dbc, $query) !== true) {
printf("Error: %s in query %s\n", $mysqli->error,$query);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.