使用mysqli_insert_id将一个表中的最后一个ID插入另一个表中

时间:2014-11-04 01:53:05

标签: php mysql mysql-insert-id

我正在尝试使用mysqli_insert_id()从一个表中获取最后一个id值并将其插入另一个表中。以下代码为ID获取值0(但时间戳是正确的)。有任何想法吗?我认为有几种方法可以做到这一点,但我似乎无法使其发挥作用。

<?php
 $con=mysqli_connect("hostname","dbname","password","db");
 if (mysqli_connect_errno())
 {
 echo "Failed to connect to MySQL: " . mysqli_connect_error();
 }

$sql = "INSERT INTO `thelineexchange`.`lines` (`id` ,`line` ,`author` ,`email` ,`time`)
VALUES (NULL, '".$_POST['input-poem-text']."', '".$_POST['input-author']."',       '".$_POST['input-email']."',NOW( ));";

if (!mysqli_query($con,$sql))
 {
  die('Error: ' . mysqli_error($con));
 }

$id = mysqli_insert_id($con);

$sqlmsg = "INSERT INTO `linesid` (`id`, `time`) VALUES ('".$id."', NOW())";
mysqli_query($sqlmsg);
;

$url = 'yourline.php';
echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';  


 ?>

0 个答案:

没有答案
相关问题