[英]php mysql how to use mysql_insert_id() to be able to insert into 2 table at the same time?
I am using php with mysql database to do some insert. 我在mysql数据库中使用php进行一些插入。 I have 2 tables.
我有2张桌子。
my problem is that when i insert into status table the user_id field do change and take 0 no matter what is the user_id. 我的问题是,当我插入状态表时,无论user_id是什么,user_id字段都会更改并取0。
so how to fix this problem ??? 那么如何解决这个问题呢?
<?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));
}
}
?>
i am using php with android so their is no html form 我在Android上使用php,因此它们不是html格式
You can't. 你不能
mysql_insert_id()
only applies to the LAST insert performed. mysql_insert_id()
仅适用于最后执行的插入。 If you're doing two inserts, and call insert_id() after the second one, the first ID is lost. 如果您要进行两次插入,并在第二次插入之后调用insert_id(),则第一个ID将丢失。
There is no way around this. 没有办法解决这个问题。
You must have something like: 您必须具有以下内容:
INSERT INTO foo ....
$fooid = mysql_insert_id();
INSERT INTO bar .... foo_id=$fooid
$barid = mysql_insert_id();
Given that your code actually seems to be split into multiple pages, it's even worse. 鉴于您的代码实际上似乎被拆分为多个页面,因此情况甚至更糟。 mysql_insert_id() only applies to the CURRENT connection to the database.
mysql_insert_id()仅适用于与数据库的当前连接。 Once your first script exits, the connection is closed and the insert_id is lost.
一旦第一个脚本退出,连接将关闭,insert_id丢失。
The next script will get a NEW connection, and have its own completely separate insert_id system going. 下一个脚本将获得一个NEW连接,并具有其自己完全独立的insert_id系统。
For chaining multiple pages together like this, you'll have to retrieve/pass the insert ID around yourself, eg 为了像这样将多个页面链接在一起,您必须在自己周围检索/传递插入ID,例如
page1: 第1页:
INSERT ...
$_SESSION['page1_id'] = mysql_insert_id();
page2: 第2页:
$last_id = $_SESSION['page1_id'];
INSERT ..... id=$last_id
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.