如何在子表oophp

时间:2016-10-18 10:26:10

标签: php mysql

更新:我有两个表用户请求用户表有列 id用户名,密码和城镇我可以成功地在用户中插入数据.requests表有 id,user_id,product_name,proposed_price和request_description ,其中user_id是从users表引用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);
   }
  ?>

1 个答案:

答案 0 :(得分:-1)

显然,问题是您有foreign key指向发出请求的用户,当您尝试inserttable时,您会收到foreign key以来的异常1}}不可为空。有两种可能的解决方案:您可以找到正确的用户并在insert语句中将其id用作user_id,或者将foreign key列修改为可为空,这样您就可以支持无用户请求。 / p>

所以,第一个解决方案是:

  • user_id传递给User_request function
  • 通过添加User_request
  • 来修改$user_id的参数列表
  • 修改您的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;
    }
}
相关问题