我有以下代码:MySQL-为什么LAST_INSERT_ID()不适合我?
public function createNewGuide($userID,$guideName)
{
$sql =" INSERT INTO myTable(name, updated)
VALUES ('$guideName', 'NOW()')";
//Process query
$this->query($sql); // This inserts the new row
$this->query('LAST_INSERT_ID()'); // This throws an error
return $this->query_result;
}
我的查询功能看起来是这样的:
private function query($sql)
{
$this->query_result = mysql_query($sql, $this->conn)
or die("Unable to query local database <b>". mysql_error()."</b><br>$sql");
}
我得到以下错误:
MySQL Database Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LAST_INSERT_ID()'
我GOOGLE了,看着相似的问题,但没有找到答案:(
我还没有尝试过日e PHP function mysql_insert_id(),因为我真的想用SQL来做这件事。
您应该考虑使用查询参数,而不是直接将值插入到SQL字符串中。您的代码可能存在SQL注入漏洞。 – 2010-01-09 12:45:23
我知道 - 使用MySQLi的权利?我已经尝试过了,但我无法查询查询。我只能得到错误:(所以我尽我所能地以其他方式处理它。 – Steven 2010-01-09 13:13:45
您在显示数据库错误消息的方式中也存在XSS漏洞。http://en.wikipedia.org/wiki/Cross-site_scripting – 2010-01-09 13:42:05