[英]php mysql how to use mysql_insert_id() to be able to insert into 2 table at the same time?
我在mysql數據庫中使用php進行一些插入。 我有2張桌子。
我的問題是,當我插入狀態表時,無論user_id是什么,user_id字段都會更改並取0。
那么如何解決這個問題呢?
<?php
//array for JSON response
$response = array();
// check for required fields
if(empty($_POST['user']) || empty($_POST['password'])){
$response["success"] = 0;
$response["message"] = "enter Both Fields";
// echoing JSON response
die (json_encode($response));
}
else if (isset($_POST['user']) && isset($_POST['password']) ) {
$user = $_POST['user'];
$password = $_POST['password'];
// include db connect class
require_once '/db_connect.php';
// connecting to db
$db = new DB_CONNECT();
// mysql inserting a new row
$sql = mysql_query("Select user, password from users where user='$user'")or die(mysql_error());
$count_query = mysql_num_rows($sql);
if($count_query >0){
$response["success"] = 1;
$response["message"] = "correct Informations";
// echoing JSON response
echo json_encode($response);
}
else{
$response["success"] = 0;
$response["message"] = "Wrong User Or Pass";
// echoing JSON response
echo json_encode($response);
}
}
else {
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";
// echoing JSON response
echo json_encode($response);
}
?>
<?php
/*
* Following code will create a new product row
* All product details are read from HTTP Post Request
*/
// array for JSON response
$response = array();
if(empty($_POST['status'])){
$response["success"] = 0;
$response["message"] = "You must Write something";
// echoing JSON response
die (json_encode($response));
}
// check for required fields
else if (isset($_POST['status'])) {
$status = $_POST['status'];
// include db connect class
require_once __DIR__ . '/db_connect.php';
// connecting to db
$db = new DB_CONNECT();
// mysql inserting a new row
$result = mysql_query("INSERT INTO status(status, user_id) VALUES('$status' , '$last_insert_id')") or die(mysql_error);
$last_insert_id = mysql_insert_id();
// check if row inserted or not
if ($result) {
// successfully inserted into database
$response["success"] = 1;
$response["message"] = "Your Status has been saved.";
// echoing JSON response
die (json_encode($response));
} else {
// failed to insert row
$response["success"] = 0;
$response["message"] = "Oops! An error occurred.";
// echoing JSON response
die (json_encode($response));
}
}
?>
我在Android上使用php,因此它們不是html格式
你不能 mysql_insert_id()
僅適用於最后執行的插入。 如果您要進行兩次插入,並在第二次插入之后調用insert_id(),則第一個ID將丟失。
沒有辦法解決這個問題。
您必須具有以下內容:
INSERT INTO foo ....
$fooid = mysql_insert_id();
INSERT INTO bar .... foo_id=$fooid
$barid = mysql_insert_id();
鑒於您的代碼實際上似乎被拆分為多個頁面,因此情況甚至更糟。 mysql_insert_id()僅適用於與數據庫的當前連接。 一旦第一個腳本退出,連接將關閉,insert_id丟失。
下一個腳本將獲得一個NEW連接,並具有其自己完全獨立的insert_id系統。
為了像這樣將多個頁面鏈接在一起,您必須在自己周圍檢索/傳遞插入ID,例如
第1頁:
INSERT ...
$_SESSION['page1_id'] = mysql_insert_id();
第2頁:
$last_id = $_SESSION['page1_id'];
INSERT ..... id=$last_id
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.