2012-04-10 55 views
0

下面是我尝试过的代码,但我似乎无法做出正确的调整来让脚本正常运行。使用PHP与LAST_INSERT_ID插入多个表格

$sql = 'INSERT INTO clients (first_name, last_name, email) VALUES('.$_POST['first_name'].', '.$_POST['last_name'].', '.$_POST['email'].') 
INSERT INTO client_data (client_id, phone, zip, note) VALUES(LAST_INSERT_ID(), '.$_POST['phone'].', '.$_POST['zip'].', '.$_POST['message'].')'; 

mysql_query($sql) or die (mysql_error()); 

任何帮助,非常感谢。

+3

附注:“Jimmy O'Brian”发出命令时会发生什么? – 2012-04-10 15:06:53

+3

或者小鲍比桌子? (http://xkcd.com/327/) – 2012-04-10 15:08:05

+0

@ÁlvaroG.Vicario黑客赢了,这就是发生了什么事。记得清理你的输入! – Matthematics 2012-04-10 15:08:50

回答

3

你不能mysql_query();

使用这样执行两个SQL语句:

$sql = 'INSERT INTO clients (first_name, last_name, email) VALUES('.$_POST['first_name'].', '.$_POST['last_name'].', '.$_POST['email'].')'; 
mysql_query($sql); 

$clientId = mysql_insert_id(); 

$sql = 'INSERT INTO client_data (client_id, phone, zip, note) VALUES('.$clientId.', '.$_POST['phone'].', '.$_POST['zip'].', '.$_POST['message'].')'; 

mysql_query($sql) or die (mysql_error()); 

阅读了关于SQL注入以及如何预防它们。

0

你应该把它分解成两个独立的mysql_query调用和使用mysql_insert_id功能:

$firstName = mysql_real_escape_string($_POST["first_name"]); 
$lastName = mysql_real_escape_string($_POST["last_name"]); 
$email = mysql_real_escape_string($_POST["email"]); 
$phone = mysql_real_escape_string($_POST["phone"]); 
$zip = mysql_real_escape_string($_POST["zip"]); 
$message = mysql_real_escape_string($_POST["message"]); 

mysql_query("INSERT INTO clients (first_name, last_name, email) VALUES ('{$firstName}', '{$lastName}', '{$email}')") or die(mysql_error()); 
mysql_query("INSERT INTO client_data (client_id, phone, zip, note) VALUES ('". mysql_insert_id() ."', '{$phone}', '{$zip}', '{$message}')") or die(mysql_error()); 
0

只是要2个查询:

$sql_insert_clients = "INSERT INTO clients (first_name, last_name, email) VALUES(".$_POST['first_name'].", ".$_POST['last_name'].", ".$_POST['email'].")"; 
mysql_query($sql_insert_clients) or die (mysql_error()); 

$sql_insert_client_data = "INSERT INTO client_data (client_id, phone, zip, note) VALUES(".mysql_insert_id().", ".$_POST['phone'].", ".$_POST['zip'].", ".$_POST['message'].")"; 
mysql_query($sql_insert_client_data) or die (mysql_error()); 
-1

你只是缺少分号分割插入的

$sql = 'INSERT INTO clients (first_name, last_name, email) 
     VALUES('.$_POST['first_name'].', '.$_POST['last_name'].', '.$_POST['email'].')'."; ".' INSERT INTO client_data (client_id, phone, zip, note) VALUES(LAST_INSERT_ID(), '.$_POST['phone'].', '.$_POST['zip'].', '.$_POST['message'].')'; 

mysqli_multi_query($sql) or die (mysql_error()); 

它应该运行的SQL查询(使用dummy co内容)是

INSERT INTO clients (first_name, last_name, email) VALUES('test', 'surname', email); INSERT INTO client_data (client_id, phone, zip, note) VALUES(LAST_INSERT_ID(), 'phone', 'zip', 'message'); 
+0

这不会在MySQL – 2012-04-10 15:19:19

+0

工作我刚刚在一个虚拟的MySQL数据库中测试了它..它确实工作。 – michaelotoole 2012-04-10 15:21:34

+0

[mysqli_multi_query](http://php.net/manual/en/mysqli.multi-query.php)有可能,但不能与[mysql_query](http://php.net/manual/en/function .mysql-query.php)。 – Travesty3 2012-04-10 15:21:45