簡體   English   中英

如何在子表oophp中使用外鍵插入

[英]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 ,並且當您嘗試inserttable ,由於該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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM