所以我有一个查询,将一些信息插入表中。我的问题是我正在使用'default'条目,它会在表格中插入一个自动递增的数字和其他信息。
我正在使用mysqli_query()将信息插入表中。我的问题是,在搜索和搜索之后,每个人似乎都在使用“PDO”或其他与我正在使用的代码不兼容的东西。
我可以使用mysqli_insert_id查找刚刚提交的auto_incremented数字吗?如果是这样怎么用呢?这是我当前插入信息的代码......
mysqli_query($sql, "INSERT INTO tableposts(id, name, location) VALUES (DEFAULT, '$name', '$location')") or die(mysqli_error($sql));
所以我需要找出那个id。它应该是自动递增的。有什么帮助吗?
答案 0 :(得分:2)
试试这个例子:
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query = "INSERT INTO tableposts(id, name, location) VALUES (DEFAULT, '$name', '$location')";
$mysqli->query($query);
printf ("New Record has id %d.\n", $mysqli->insert_id);
/* close connection */
$mysqli->close();
答案 1 :(得分:1)
您可以按如下方式插入最后一个:
mysqli_insert_id($sql)
假设$ sql是你的连接链接。
来源:http://www.w3schools.com/php/func_mysqli_insert_id.asp,请查看此内容以获得清晰的理解