[英]How to insert with a foreign key in the child table oophp
UPDATE : I have two tables users And requests users table has columns id username,password and town i can insert data in users successfully .requests table has id ,user_id ,product_name, proposed_price and request_description , where user_id is a foreign key referencing id from users table, the problem is that insert data fails in requests table which has user_id as a foreign key . 更新:我有两个表的用户,并请求用户表具有列id用户名,密码和镇,我可以成功地在用户中插入数据。 请求表具有ID,user_id,product_name,proposal_price和request_description ,其中user_id是引用用户ID的外键问题在于,在以user_id作为外键的请求表中,插入数据失败。 i get nothing in requests table
我在请求表中什么也没得到
This function is supposed to be used in insertion: 该函数应该在插入中使用:
public function User_request ($product_name, $proposed_price, $request_description) {
$qry = $this->conn->prepare("SELECT id FROM users WHERE id = ? ");
$qry->bind_param("i", id);
$result= $qry->execute();
$user_id = $qry->fetch();
$qry->close();
if($user_id > 0){
$stmt = $this->conn->prepare("INSERT INTO requests (user_id, product_name, proposed_price, request_description) VALUES(?, ?, ?, ?)");
$stmt->bind_param("isss",$user_id, $product_name, $proposed_price, $request_description);
$result = $stmt->execute();
$stmt->close();
// check for successful store
if ($result) {
$stmt = $this->conn->prepare("SELECT * FROM requests WHERE request_description = ?");
$stmt->bind_param("s", $request_description);
$stmt->execute();
$user = $stmt->get_result()->fetch_assoc();
$stmt->close();
return $user;
} else {
return false;
}
}
}
And below codes call above function: 下面的代码调用上面的函数:
<?php
include './DbHandler.php';
$db = new DBHandler();
// json response array
$response = array("error" => FALSE);
if ( isset($_POST['product_name']) && isset($_POST['proposed_price']) && isset($_POST['request_description']) ) {
// receiving the post params
$product_name = $_POST['product_name'];
$proposed_price =$_POST['proposed_price'];
$request_description =$_POST['request_description'];
// create a new request
$user = $db-User_request($product_name, $proposed_price, $request_description);
if ($user) {
// user stored successfully
$response["error"] = FALSE;
$response["user"]["username"] = $user["username"];
$response["user"]["proposed_price"] = $user["proposed_price"];
$response["user"]["request_description"] = $user["request_description"];
echo json_encode($response);
} else {
// user failed to store
$response["error"] = TRUE;
$response["error_msg"] = "oops error occured!";
echo json_encode($response);
}
}
else {
$response["error"] = TRUE;
$response["error_msg"] = "Required parameters are missing!";
echo json_encode($response);
}
?>
Clearly, the problem is that you have a foreign key
pointing to the user issuing the request and when you try to insert
to that table
, you get an exception since the foreign key
is not nullable. 显然,问题在于您有一个指向发出请求的用户的
foreign key
,并且当您尝试insert
该table
,由于该foreign key
不可为空,因此会出现异常。 There are two possible solutions: you either find the proper user and use its id as user_id
in the insert statement, or you modify the foreign key
column to be nullable, so you support user-less requests. 有两种可能的解决方案:您可以找到合适的用户,然后在插入语句中将其ID用作
user_id
,或者将foreign key
列修改为可为空,以便支持无用户请求。
So, the first solution is to: 因此,第一个解决方案是:
user_id
to User_request
function
user_id
传递给User_request
function
User_request
's parameter list by adding $user_id
there $user_id
修改User_request
的参数列表 insert
statement and add $user_id
as the fourth parameter insert
语句,并添加$user_id
作为第四个参数 The second solution is to alter
the table
so that user_id
to allow null
s. 第二种解决方案是
alter
table
,以使user_id
允许为null
。 I believe you need the first solution. 我相信您需要第一个解决方案。
EDIT: 编辑:
I believe this line: 我相信这一行:
$user = $db-User_request($product_name, $proposed_price, $request_description);
is 是
$user = $db->User_request($product_name, $proposed_price, $request_description);
Also, you have this value: $user["username"]
另外,您具有此值:
$user["username"]
I am not sure about the name of the user table, but will assume it is called users
and has an id
and a username
field, so a query like: 我不确定用户表的名称,但会假设它被称为
users
并具有一个id
和一个username
名字段,因此查询如下:
select id, username from users where username = ?
would return the relevant data and you could implement a function called getUserByUsername
which will take a username
, which will be $user["username"]
in our case and return the user (note, that this is better read from $_SESSION
, but I do not know what you have there). 将返回相关数据,您可以实现一个名为
getUserByUsername
的函数,该函数将使用一个username
,在本例中为$user["username"]
并返回该用户(请注意,最好从$_SESSION
读取,但是我不知道那里有什么)。 From here you can read user_id
and you will be able to pass it as: 从这里您可以读取
user_id
并且可以将其传递为:
//I left the name user here, but you essentially gather a request record
$user = $db->User_request($product_name, $proposed_price, $request_description, $user_id);
and the other function
would change to: 而另一个
function
将变为:
//I left the name user here, but you essentially gather a request record
public function User_request ($product_name, $proposed_price, $request_description, $user_id) {
//Notice the fourth ? and user_id at the end of the liist
$stmt = $this->conn->prepare("INSERT INTO requests ( product_name, proposed_price, request_description, user_id) VALUES(?, ?, ?, ?)");
//and you need the effective value as well
$stmt->bind_param("sss", $product_name, $proposed_price, $request_description, $user_id);
$result = $stmt->execute();
$stmt->close();
// check for successful store
if ($result) {
$stmt = $this->conn->prepare("SELECT * FROM requests WHERE request_description = ?");
$stmt->bind_param("s", $request_description);
$stmt->execute();
$user = $stmt->get_result()->fetch_assoc();
$stmt->close();
return $user;
} else {
return false;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.