1

私は SELECT LAST_INSERT_ID() を使用しようとしています。フォームを使用してユーザー入力値を取得しています。最初の挿入では、次の挿入のために最後に挿入されたIDを取得する必要があります...最後に選択されたIDを取得して2番目の挿入ステートメントに渡す方法がわかりません

コードを更新しましたが、テーブルに投稿する ID をまだ取得できません

include("config.inc.php");
$link = mysql_connect($db_host,$db_user,$db_pass);
if(!$link) die ('Could not connect to database: '.mysql_error());
mysql_select_db($db_name,$link);
$query = "INSERT into `".$db_table."` (producer_id,series_id,lang_id,title_name,title_public_access) VALUES ('" . $_POST['producer_id'] . "','" . $_POST['series_id'] . "','" . $_POST['lang_id'] . "','" . $_POST['title_name'] . "','" . $_POST['title_public_access'] . "')";

$last_id = mysql_insert_id();

$query = "INSERT into `".$db_table2."` (seg_id, file_video_UNC,file_video_URL) VALUES ('" . '$last_id' . "','" . $_POST['file_video_UNC'] . "','" . $_POST['file_video_URL'] . "')";

mysql_query($query);
mysql_close($link);
4

3 に答える 3

2

そのためのmysql_insert_id()という関数があります。

... first query here ...
$last_id = mysql_insert_id();
$sql = "INSERT INTO $db_table SET 
    file_video = " . $_POST['file_video_UNC'].",
    file_video_URL = " . $_POST['file_video_URL'] . ",
    insert_id_of_first_query = $last_id";
...

更新されたコードはクエリをデータベースに送信しません-結果として no INSERT、したがって noLAST_INSERT_ID

$query = "INSERT into ".$db_table." 
    (producer_id,series_id,lang_id,title_name,title_public_access) VALUES
    ('" . $_POST['producer_id'] . "','" 
        . $_POST['series_id'] . "','" 
        . $_POST['lang_id'] . "','" . $_POST['title_name'] . "','" 
        . $_POST['title_public_access'] . "')";

mysql_query($query); /* YOU FORGOT THIS PART */
$last_id = mysql_insert_id();
于 2011-01-13T00:28:50.400 に答える
1

PHP の行でクエリを単独で文字列にダンプすることはできません。2 番目のクエリ内で LAST_INSERT_ID() を使用するか、API でこれをラップするPHP の mysql_insert_id()関数を使用することをお勧めします。

于 2011-01-13T00:29:54.363 に答える
0

行で:

$query = "INSERT into `".$db_table2."` (seg_id, file_video_UNC,file_video_URL) VALUES ('" . '$last_id' . "','" . $_POST['file_video_UNC'] . "','" . $_POST['file_video_URL'] . "')";

VALUES ('" . '$last_id' . "',変数を一重引用符なしで囲む必要があると思いVALUES ('" . $last_id . "',ます。

于 2013-04-12T10:52:20.813 に答える