如何在子表php中插入外键

时间:2016-10-20 09:26:36

标签: php mysql

我有两个表 users requests 用户表包含以下列:idusernamepasswordtown。我可以成功地在users中插入数据。 requests表包含:iduser_idproduct_nameproposed_pricerequest_description,其中user_id是引用的外键来自id表的users。问题是在requests表中插入数据失败,其中user_id作为外键。我收到一个错误:

  

未定义变量:user_id和mysqli_stmt :: bind_param():变量数与预备语句中的参数数量不匹配:$ qry-> bind_param(“i”,$ user_id);

此功能应该用于插入:

public function  User_request ($product_name, $proposed_price, $request_description) {

     $qry = $this->conn->prepare("SELECT id FROM users WHERE id = '$user_id' ");
     $qry->bind_param("i", $id);
     $result= $qry->execute();
     $user_id = $qry->num_rows();
     $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("sss",$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);
   }
  ?>

2 个答案:

答案 0 :(得分:1)

相应地重写User_request()函数:

public function  User_request ($product_name, $proposed_price, $request_description) {

    $qry = $this->conn->prepare("SELECT id FROM users WHERE id = ? ");
    $qry->bind_param("i", $user_id);
    $result= $qry->execute();
    $user_id = $qry->num_rows();
    $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;
        }
    }
} 

注意:
您应该在准备语句中使用占位符?$this->conn->prepare("SELECT id FROM users WHERE id = '$user_id' ");代替'$user_id',因为您有{{1} }}。
$qry->bind_param("i", $user_id)中的类似错误,将$stmt->bind_param("sss",$user_id, $product_name, $proposed_price, $request_description);替换为"sss"

答案 1 :(得分:0)

您正在寻找&#39; $ user_id&#39;的字符串文字。这里。

$qry = $this->conn->prepare("SELECT id FROM users WHERE id = '$user_id' ");

这可能不是你想要的。您还使用了在查询中找不到的命名占位符。我建议查看PDO文档。