2012-04-10 36 views
0

以下は、私が試したコードですが、スクリプトを正しく実行するために適切な修正を加えることはできません。LAST_INSERT_ID()でPHPを使用して複数のテーブルに挿入

$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

です。サイドノート:「ジミー・オブライエン」が注文するとどうなりますか? –

+3

またはリトルボビーテーブル? (http://xkcd.com/327/) –

+0

@ÁlvaroG.Vicarioハッカーが勝つ、それは何が起こるかです。あなたの入力を消毒することを忘れないでください! – Matthematics

答えて

3

あなたはmysql_query();

使用して2つの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

次の2つの別々のmysql_queryの呼び出しにそれを破るとmysql_insert_id機能を使用する必要があります。

$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()); 
0

はわずか2つのクエリを作成します

$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クエリ(ダミーコntent)は

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'); 
-1

あなただけの挿入のを分割するセミコロンが欠落:

$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

これはMySQLで動作しません –

+0

私はこれをダミーのmysql dbでテストしました。実際に動作します。 – michaelotoole

+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

関連する問題