[英]How to insert with a foreign key in the child table oophp
更新:我有兩個表的用戶,並請求用戶表具有列id用戶名,密碼和鎮,我可以成功地在用戶中插入數據。 請求表具有ID,user_id,product_name,proposal_price和request_description ,其中user_id是引用用戶ID的外鍵問題在於,在以user_id作為外鍵的請求表中,插入數據失敗。 我在請求表中什么也沒得到
該函數應該在插入中使用:
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;
}
}
}
下面的代碼調用上面的函數:
<?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);
}
?>
顯然,問題在於您有一個指向發出請求的用戶的foreign key
,並且當您嘗試insert
該table
,由於該foreign key
不可為空,因此會出現異常。 有兩種可能的解決方案:您可以找到合適的用戶,然后在插入語句中將其ID用作user_id
,或者將foreign key
列修改為可為空,以便支持無用戶請求。
因此,第一個解決方案是:
user_id
傳遞給User_request
function
$user_id
修改User_request
的參數列表 insert
語句,並添加$user_id
作為第四個參數 第二種解決方案是alter
table
,以使user_id
允許為null
。 我相信您需要第一個解決方案。
編輯:
我相信這一行:
$user = $db-User_request($product_name, $proposed_price, $request_description);
是
$user = $db->User_request($product_name, $proposed_price, $request_description);
另外,您具有此值: $user["username"]
我不確定用戶表的名稱,但會假設它被稱為users
並具有一個id
和一個username
名字段,因此查詢如下:
select id, username from users where username = ?
將返回相關數據,您可以實現一個名為getUserByUsername
的函數,該函數將使用一個username
,在本例中為$user["username"]
並返回該用戶(請注意,最好從$_SESSION
讀取,但是我不知道那里有什么)。 從這里您可以讀取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);
而另一個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.