php检索插入的最后一条记录的id并将其插入另一个表中
我试图检索表“authors”中插入的最后一条记录的id,并将检索到的id插入表“articles”中;这是我的密码:php检索插入的最后一条记录的id并将其插入另一个表中,php,lastinsertid,Php,Lastinsertid,我试图检索表“authors”中插入的最后一条记录的id,并将检索到的id插入表“articles”中;这是我的密码: $title = mysql_real_escape_string($_POST[title]); $body = mysql_real_escape_string($_POST[body]); $category = mysql_real_escape_string($_POST[category]); $insert_author="INSERT INTO authors
$title = mysql_real_escape_string($_POST[title]);
$body = mysql_real_escape_string($_POST[body]);
$category = mysql_real_escape_string($_POST[category]);
$insert_author="INSERT INTO authors (name) VALUES ('$title')";
$select_author= mysql_query("SELECT LAST_INSERT_ID()");
$authId = mysql_fetch_array($select_author);
$query="INSERT INTO articles (body, date, category, auth_id) VALUES ('$body', '$date_now', '$category', '$authId')";
这不管用
有什么想法吗
提前谢谢
毛罗到底什么不起作用。
$authId
是否包含预期值?你把它扔了检查吗
除此之外,您还忘了第二个
mysql\u query()
什么东西不起作用。$authId
是否包含预期值?你把它扔了检查吗
除此之外,您忘记了第二个
mysql\u query()
您可以使用
$insert_author="INSERT INTO authors (name) VALUES ('$title')";
mysql_query($insert_author) or die(mysql_error());
$authId = mysql_insert_id();
$query="INSERT INTO articles (body, date, category, auth_id) VALUES
('$body', '$date_now', '$category', '$authId')";
注意您缺少用于插入查询的
mysql\u query
函数,我也在上面的代码中添加了该函数。您可以使用获取最后一个插入id
$insert_author="INSERT INTO authors (name) VALUES ('$title')";
mysql_query($insert_author) or die(mysql_error());
$authId = mysql_insert_id();
$query="INSERT INTO articles (body, date, category, auth_id) VALUES
('$body', '$date_now', '$category', '$authId')";
注意您缺少用于插入查询的
mysql\u query
函数,我在上面的代码中也添加了该函数。请尝试。请尝试。正如名字所说,mysql\u fetch\u数组($select\u author)
返回一个数组,因此需要获取该数组中的特定值以插入到另一个表中。顾名思义,mysql\u fetch\u数组($select\u author)
返回一个数组,因此需要获取该数组中的特定值以插入到另一个表中。信息不足。你做了什么调试?确切地说,什么不起作用?“它不起作用”是信息不足。你做了什么调试?到底是什么不起作用?