0

Hi I have an insert and an update statement. The update works fine, I have two fields, one category_of_taxom and number_of_taxom. The update allows either one to be changed and it will update the record.

However the insert which is pretty much the same, if it is not already existing creates the record. Will work if I input values for both options, but if only one is inputted it does not submit anything? The code is quite complicated, with all the variable names, but Im thinking I maybe need another if statement or something? to check if just one is inputted? or is this a PDO thing that all parameters need filling?

   $stmt6 = $conn ->prepare("UPDATE record_tbl SET category_of_taxom =?, number_of_taxom =? WHERE sheet_id = ? AND line = 6");
   $stmt6->bindParam(1, $category_of_taxom66);
   $stmt6->bindParam(2, $number_of_taxom66);
   $stmt6->bindParam(3, $sheet_id);
      $category_of_taxom66 = $_POST['categorySelect6fromDB'];
      $number_of_taxom66 = $_POST['number_of_taxom6'];
   $stmt6->execute();       
       echo "Saved!";
}
    else 
            {       
        if (isset($_POST['categorySelect6fromDB'])) {
         $category_of_taxom66 = $_POST['categorySelect6fromDB'];
         $param_cat = PDO::PARAM_INT;
 }
  else {
         $category_of_taxom66 = NULL;
         $param_cat = PDO::PARAM_NULL;
 }
    if (isset($_POST['number_of_taxom6'])) {
         $number_of_taxom66 = $_POST['number_of_taxom6'];
         $param_num = PDO::PARAM_INT;
 }
else {
         $number_of_taxom66 = NULL;
        $param_num = PDO::PARAM_NULL;
 }
 $stmt66 = $conn ->prepare("INSERT INTO record_tbl (line, taxom_id, category_of_taxom, number_of_taxom, sheet_id) VALUES (6,6,?,?,?)");
            $stmt66->bindParam(1, $category_of_taxom66, $param_cat);
            $stmt66->bindParam(2, $number_of_taxom66, $param_num);
            $stmt66->bindParam(3, $sheet_id);
          $stmt66->execute();
            echo "New Record Inserted!";        
   }
            }   
4

1 回答 1

1

我在考虑几种情况,也许稍后会编辑答案,如果这里不是所需的,但既然作者说:

抱歉:工作表 ID 始终设置,但是 $category_of_taxom66 和 $number_of_taxom66 有时可能会或可能不会设置。我有一个高于一切的 if 语句。这会检查,就好像没有设置任何内容一样,它会跳过更新或插入。但是,如果设置了其中之一,则需要添加。

似乎插入应该发生,但是没有设置的值应该发生什么,它通常应该变成NULL?

$stmt66 = $conn ->prepare("INSERT INTO record_tbl (line, taxom_id, category_of_taxom, number_of_taxom, sheet_id) VALUES (6,6,?,?,?)");
         $stmt66->bindParam(1, $category_of_taxom66);
         $stmt66->bindParam(2, $number_of_taxom66);
         $stmt66->bindParam(3, $sheet_id);
         $category_of_taxom66 = isset($_POST['categorySelect6fromDB']) ? $_POST['categorySelect6fromDB'] : NULL;
         $number_of_taxom66 = isset($_POST['number_of_taxom6']) ? $_POST['number_of_taxom6'] : NULL;
         $stmt66->execute();

我认为 $category_of_taxom66 的值将是用户输入,如果存在的话,或者 NULL 如果不存在。


另一种变化:

if (isset($_POST['categorySelect6fromDB'])) {
    $category_of_taxom66 = $_POST['categorySelect6fromDB'];
    $param_cat = PDO::PARAM_INT;
}
else {
    $category_of_taxom66 = NULL;
    $param_cat = PDO::PARAM_NULL;
}
if (isset($_POST['number_of_taxom66'])) {
    $number_of_taxom66 = $_POST['number_of_taxom66'];
    $param_num = PDO::PARAM_INT;
}
else {
    $number_of_taxom66 = NULL;
    $param_num = PDO::PARAM_NULL;
}
$stmt66 = $conn ->prepare("INSERT INTO record_tbl (line, taxom_id, category_of_taxom, number_of_taxom, sheet_id) VALUES (6,6,?,?,?)");
        $stmt66->bindParam(1, $category_of_taxom66, $param_cat);
        $stmt66->bindParam(2, $number_of_taxom66, $param_num);
        $stmt66->bindParam(3, $sheet_id);
        $stmt66->execute();

因此,如果 category_taxom 由用户填充“100”,但 number_taxom 不是,它应该产生:

        $stmt66->bindParam(1, 100, PDO::PARAM_INT);
        $stmt66->bindParam(2, NULL, PDO::PARAM_NULL);

另一种方法是在第一个示例中使用“0”而不是“NULL”:

         $category_of_taxom66 = isset($_POST['categorySelect6fromDB']) ? $_POST['categorySelect6fromDB'] : 0;
         $number_of_taxom66 = isset($_POST['number_of_taxom6']) ? $_POST['number_of_taxom6'] : 0;
于 2013-08-09T13:17:08.827 回答