【问题标题】:Codeigniter: The problem is in the if else. it only work only on IF it won't go to else ifCodeigniter:问题出在 if else 中。它仅适用于 IF 它不会去 else if
【发布时间】:2021-12-21 00:06:52
【问题描述】:

先生们,你们好。请帮助我,我遇到了这个问题。我有 2 本书信息表,现在我的目标是允许图书管理员插入到数据库中,即使只有 1 个表单具有价值,无论图书管理员选择哪种形式 [!实际上它可以插入但只有左侧形成右侧它有错误

更新: 当我检查它不会去 else if

由于某种原因我不能放截图 错误: "错误号:1452

无法添加或更新子行:外键约束失败(lnulibrary.tbl_borrowed_books, CONSTRAINT tbl_borrowed_books_ibfk_1 FOREIGN KEY (book_id) REFERENCES tbl_books (book_id))

插入tbl_borrowed_booksbook_idprofile_idreturn_date)值(''、'1'、'2021-11-11')

文件名:D:/new xampp/htdocs/library/system/database/DB_driver.php

行号:691"

 my model
 public function borrowers()
  {

 $sum =  date('Y-m-d', strtotime("+3 days"));
  $isbn1= $this->input->post('serial1',true);
   $isbn2= $this->input->post('serial2',true);

  if(empty($isbn2))
  {
   $data1 = array(
  'book_id'     => $this->input->post('book_id1',true),
  'profile_id'  => $this->input->post('userid',true),
  'return_date' => $sum

    );
      $this->db->insert('tbl_borrowed_books', $data1);

      }
      if(empty($isbn1))
      {
       $data2 = array(
      'book_id'     => $this->input->post('book_id2',true),
      'profile_id'  => $this->input->post('userid',true),
      'return_date' => $sum


      );
      $this->db->insert('tbl_borrowed_books', $data2);
        }



    /* i will use this if 2 forms have value */
/* $data1 = array(
    'book_id'     => $this->input->post('book_id1',true),
   'profile_id'  => $this->input->post('userid',true),
   'return_date' => $sum

   );
   $this->db->insert('tbl_borrowed_books', $data1);

   $data2 = array(
  'profile_id'  => $this->input->post('userid',true),
  'book_id'     => $this->input->post('book_id2',true),
  'return_date' => $sum

          );
        $this->db->insert('tbl_borrowed_books', $data2);*/

       }

【问题讨论】:

  • tbl_borrowed_books.book_id 是否应该是整数? (我有点希望它是一个整数)如果它是自动递增的,要么从 INSERT 语法中省略该列,要么从插入到父表的行中获取 $this->db->insert_id()。我对这个建议没什么信心,因为英文 /minimal reproducible example 不是很清楚。

标签: php codeigniter insert


【解决方案1】:

您需要传递正确的 bookid,即存在于您的数据库中的 bookid, 这不是codeigniter的错误而是数据库的错误,您可以删除外键并检查调试,但我强烈建议您使用外键

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-19
    • 2021-08-26
    相关资源
    最近更新 更多