如何插入两张表


How Insert two table

我已经写了这样的代码:

$password = md5($password);
            mysql_query("INSERT INTO `users` (username,password,email,pnum,hintque,hintans) VALUES('$username','$password','$email','$cnum','$hintque','$hintans')");
            mysql_query("INSERT INTO `personal` (userid,fname,lname,address,dob,country,state,city,poscode) VALUES(LAST_INSERT_ID(),'$fname','$lname','$address','$dob','$country','$state','$city'.'$zip')");
            header("location: ../register.php?feedback=Registration Complete. You May Now Login");

但这只是sql第一次成功插入数据。第二次失败。我希望两个查询同时触发。

第二个查询可能会失败,因为您有语法错误(请参阅最后一个值,.应该是,):

mysql_query("INSERT INTO `personal` (userid,fname,lname,address,dob,country,state,city,poscode) VALUES(LAST_INSERT_ID(),'$fname','$lname','$address','$dob','$country','$state','$city','$zip')");

此外,请避免使用mysql_*。该函数族现在已被弃用,您应该使用更新的函数,如MySQLi或PDO。

mysql_query("INSERT INTO `personal` (userid,fname,lname,
             address,dob,country,
             state,city,poscode) VALUES (LAST_INSERT_ID(),'$fname','$lname',
        '$address','$dob','$country',
        '$state','$city','$zip')"); 

问题:

'$city'.'$zip''$city','$zip'

注意:现在不推荐使用mysql_*函数。尝试使用mysqli

在一个脚本中不可能同时发生两件事。几乎同时可能。但通常这并不是真正的目标。相反,看看所有严肃的数据库管理系统提供的一个名为transactions的结构。它允许您将一系列sql语句组合为一个成功或失败的单元。

您也可以尝试mysqli_multi_query