Php 如何在子表中使用外键插入
更新:我有两个表users和requests users表有id username、password和town列,我可以在users中成功插入数据。requests表有id、user\u id、product\u name、provided\u price和request\u description,其中user\u id是引用users表中id的外键,问题在于,将用户id作为外键的请求表中的插入数据失败。我的桌子上什么也没有 此函数应用于插入: 以下代码调用上述功能:Php 如何在子表中使用外键插入,php,mysql,Php,Mysql,更新:我有两个表users和requests users表有id username、password和town列,我可以在users中成功插入数据。requests表有id、user\u id、product\u name、provided\u price和request\u description,其中user\u id是引用users表中id的外键,问题在于,将用户id作为外键的请求表中的插入数据失败。我的桌子上什么也没有 此函数应用于插入: 以下代码调用上述功能: 显然,问题是您有一个外键
显然,问题是您有一个外键指向发出请求的用户,当您尝试插入到该表时,您会得到一个异常,因为该外键不可为null。有两种可能的解决方案:要么找到合适的用户并在insert语句中将其id用作user_id,要么将外键列修改为可空,以便支持无用户请求 因此,第一个解决方案是: 将用户\u id传递给用户\u请求函数 通过在其中添加$User\U id来修改User\U请求的参数列表 修改insert语句并添加$user\u id作为第四个参数 第二种解决方案是修改表,以便用户能够允许空值。我相信你需要第一个解决方案 编辑: 我相信这句话:
$user = $db-User_request($product_name, $proposed_price, $request_description);
是
另外,您还有一个值:$user[username]
我不确定用户表的名称,但会假定它被称为users,并且有一个id和一个username字段,因此查询如下:
select id, username from users where username = ?
将返回相关数据,您可以实现一个名为getUserByUsername的函数,该函数将采用用户名,在我们的例子中为$user[username],并返回用户注释,这最好从$\u会话读取,但我不知道您有什么。从这里,您可以读取用户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);
另一个功能将更改为:
//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;
}
}
错误消息?能否添加表结构?另外,哪个表是A,哪个表是B?如果在requests表中有一个外键通过user\u id字段指向users表,那么为什么不在requests表中插入记录时填充user\u id字段呢?@NaijaProgrammer,表A有:id、用户名、密码和town表B:id、user\u id、product\u name、proposed\u price,请求_description@Shadow,我需要的是不要让user_id为空,而是参考users id获取它的值。我想向downvoter询问downvote的原因。这是个好主意,先生,但我真正渴望的是在表B请求的外键用户id字段中获得表A的主键id,并将用户id传递给用户请求函数。但由于用户id指向文本字段,我得到错误,即缺少必需的参数。很抱歉,他们否决了投票,但您提供了很好的帮助me@user3518835,我已编辑了我的答案。希望它现在更有帮助。
select id, username from users where username = ?
//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);
//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;
}
}