数据库错误1452

时间:2017-02-06 06:53:52

标签: php mysql database codeigniter

我正在处理这个在两个表上有外键的项目。通过使用表单I尝试将新记录插入数据库。并且还有数据库中的图像路径,我通过表单插入图像路径。我使用codeigniter文件上传库。当我提交表单甚至外键字段时,数据库表的其他字段会更新。但图像路径未更新。当我提交表单时,它会显示此错误。

A Database Error Occurred

Error Number: 1452

Cannot add or update a child row: a foreign key constraint fails (`bfh`.`products`, CONSTRAINT `category_fk` FOREIGN KEY (`category_id`) REFERENCES `categories` (`id`))

INSERT INTO `products` (`img`) VALUES ('assets/img//sakya.PNG')

Filename: C:/xampp/htdocs/CI-skeleton/system/database/DB_driver.php

Line Number: 691

控制器

public function add_product()
    {
        $result = 0;
        $cat_id = $this->input->post("category_id");
        $type_id    = $this->input->post("type_id");
        $pname  = $this->input->post("p_name");
        $images = $this->input->post("images");
        $images  = $_FILES['images']['name'];
        $price  = $this->input->post("price");

        $this->load->model("Products_Model");
        $product_id = $this->Products_Model->add_product( $cat_id, $type_id, $pname, $price);

        if ($product_id != 0) {
        $result = $this->Products_Model->add_product_images($images);
        }


        if ($result && $_FILES['images']['name'][0] != "") {
        $this->load->model('Image_Upload');
        $result = $this->Image_Upload->upload("assets/img");
    }

        $this->session->set_flashdata('result', $result);
        redirect('Products');
    }

模型

public function add_product( $cat_id, $type_id, $pname, $price)
    {
        $result = $this->db->get_where("products", array('name' => $pname));
        if ($result->num_rows() != 0) {
            return 0; // record already exists
        } else {
            $data = array(
                    'category_id' => $cat_id,
                    'type_id' => $type_id,
                    'name' => $pname,
                    'price' => $price
            );

            if( !$this->db->insert('products', $data)) {
                return -1; // error
            }else{
                return $this->db->insert_id();
            }

            return 1; // success
        }
    }

public function add_product_images($images)
{
    $path = "assets/img/";

    foreach ($images as $image) {
        // if no images were given for the item
        if ($image == "") {
            return 1;
        }

        $data = array(
                'img' => $path."/".$image
        );

        if ( ! $this->db->insert('products', $data)) {
            return 0; // if something goes wrong
        }
    }

    return 1;
}

3 个答案:

答案 0 :(得分:1)

您应该在add_product_images()中代表insert使用“update”查询。 因为“插入”将添加产品的新记录,并且没有任何类别ID(外键),这就是显示此错误的原因。 所以尝试更新图像。

答案 1 :(得分:0)

enter image description here

你有两个具有相同名称的变量,这就是问题所在。 您需要具有不同的变量名称。 我希望这会对你有所帮助。

答案 2 :(得分:0)

此脚本存在一些问题

  1. 如果你写的模型,功能添加产品。如果查询失败,则返回-1,否则返回产品ID 但是如果产品在数据库中它返回0,那么在控制器中:你检查product_id!= 0然后插入图像。所以你不在乎产品是否存在于数据库中,你是否未能将其插入表格中
  2. 在add_product_images中检查是否($ image =="")。如果此声明为真,我不明白你如何得到那个图像数组($ images)是空的。您可以在foreach之前检查是否(sizeof($ images)== 0)检查图像数组的空白($ images)
相关问题