我无法使用Php将表1的内容插入到sql的表2中?

时间:2017-05-11 06:45:48

标签: php mysql

$conn = mysqli_connect($dbhost,$dbuser,$dbpass, $dbname);
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}


for($i=0;$i<=feof($getdata);$i++)
{
if (filter_var($data[$i][1], FILTER_VALIDATE_EMAIL)){
echo $data[$i][1];
$email=$data[$i][1];

$name=$data[$i][0];
$sql ="INSERT INTO promo_user (name,email) VALUES ('$name','$email')";
mysqli_query($conn,$sql);
$sql .="INSERT INTO promo_type (uid) select uid from promo_user";
mysqli_query($conn,$sql);
mysqli_close($conn);
} 

这是我的代码在这里我首先将一些内容(名称和电子邮件)插入到表promo_user中,然后尝试将uid从promo_user插入到表promo_type但这不起作用,内容将转到表promo_user而不是promo_type 请帮忙

1 个答案:

答案 0 :(得分:0)

插入后获取插入ID

$sql ="INSERT INTO promo_user (name,email) VALUES ('$name','$email')";
mysqli_query($conn,$sql);
$uid = mysqli_insert_id($conn);
$sql ="INSERT INTO promo_type set uid =$uid";
mysqli_query($conn,$sql);
mysqli_close($conn);