将空值插入索引数据库时出现问题

时间:2011-02-28 21:40:49

标签: php mysql

我希望有人能够帮助我,因为我很困难。

我正在尝试将一些表单数据插入到我的MySQL数据库中。当我设置索引时,数据库不会填充说不能更新db子行的外键约束失败,但是,如果我删除外键它工作正常。

我正在尝试链接2个索引字段,这些字段只是从另一个表中获取自动递增的ID。我认为错误是由于索引字段没有值而引起的。请有人帮忙吗??? :)

==更新==

嗨,谢谢你的回复。

我在发布问题之前删除了外键。

我会尝试解释一下。当我在MySQL中向数据库添加外键时,我可以更改主键字段,相应的索引字段将自动更改,从而为我提供相同的值,这正是我想要的。

问题是当我尝试使用我的PHP代码插入值时,我得到了无法更新子错误。我没有为unique_id字段选择一个值,因为我希望它继承主键的值。这是我的代码尽可能简化。

                                                               <?php  //this calls the class productupload and instantiates the function insert_form to insert values into the db.
$product = new productUpload();
$product->insert_form();?> //This calls the insert_form function and inserts info into tables
                                                                                   <?php
class productUpload extends DatabaseObject{

protected static $table_name="itm_details";
protected static $db_fields =array('id','unique_id','itm_cat','itm_make','itm_model','itm_desc','itm_cond','itm_date_from','itm_date_to','itm_add_date');      


        public $id;
        public $unique_id;
        public $itm_cat;
        public $itm_make;
        public $itm_model;
        public $itm_desc;
        public $itm_cond;
        public $itm_date_from;
        public $itm_date_to;   
        public $itm_add_date;

继承了抓取表单提交数据的函数insert_form。注意我没有为$ unique_id

定义一个值
public function insert_form(){  
global $database;  
 //set the object attributes to the form the parameters
        if(isset($_POST['submit'])) {
        $result = array(
        $this->itm_cat =(!empty($_POST['itm_cat'])) ? trim($_POST['itm_cat']) : NULL,
        $this->itm_make =(!empty($_POST['itm_make'])) ? trim($_POST['itm_make']) : NULL,       
        $this->itm_model = (!empty($_POST['itm_model'])) ? trim($_POST['itm_model']) : NULL,                                   
        $this->itm_desc =(!empty($_POST['itm_desc'])) ? trim($_POST['itm_desc']) : NULL,
        $this->itm_cond =(!empty($_POST['itm_cond'])) ? trim($_POST['itm_cond']) : NULL,
        $this->itm_date_from =(!empty($_POST['itm_date_from'])) ? trim($_POST['itm_date_from']) : NULL,
        $this->itm_date_to =(!empty($_POST['itm_date_to'])) ? trim($_POST['itm_date_to']) : NULL,
        $this->itm_add_date = date("Y-m-d H:m:s"));
//check date is numeric
        //if(is_numeric($_POST['itm_date_from'])) {
        //$this->itm_date_from = $_POST['itm_date_from'];
//}    
//check date is numeric                        
        //if(is_numeric($_POST['itm_date_to'])) {
        //$this->itm_date_to = $_POST['itm_date_to'];
        //}
        if($result){
        $result = $this->create();
        }
        }
        }

//

这里是上面提到的create函数,它清理已发布的数据并将其插入到数据库中。

public function create() {
        global $database;
        $attributes = $this->sanitised_attributes();

        $sql = "INSERT INTO ".self::$table_name."(";
        $sql .= join(", ", array_keys($attributes));
        $sql .= ") VALUES ('";
        $sql .= join("', '", array_values($attributes));
        $sql .= "')";
        if($database->query($sql)) {
        $this->id = $database->insert_id();
        return true;
        } else {       
        return false;
        }

}
?>

2 个答案:

答案 0 :(得分:0)

我假设关键约束是这样的。

ALTER TABLE itm_pic_detail ADD FOREIGN KEY (`unique_id`) REFERENCES itm_pic_detail(`itm_pic_id`) ON DELETE CASCADE ON UPDATE RESTRICT;

如果我理解正确,您无法添加约束本身,这表示您已经存在违反此新规则的冲突记录。运行以下查询以识别它们并首先更正它们。

SELECT id FROM itm_details WHERE unique_id NOT IN (SELECT itm_pic_id FROM itm_pic_detail);

答案 1 :(得分:0)

您是否检查过您的脚本正在使用的用户是否有权更新这两个表?

还尝试回显$ sql并检查它是否可以手动运行,你可能有一个简单但很难发现的错误。

此外,你可以通过删除所有那些杂乱的修剪来改进你的脚本,而不是这样做......

function ParseInt($value) {
  $value = (int)$value;
  return ($value <= 0) ? 'NULL' : $value;
}

$this->itm_cat = ParseInt($_POST['itm_cat']);

如果您从未计划过ID&lt; = 0,则自动增量字段始终从1开始。

您还可以清理$ db_fields并自动化它。

/* get the columns from the table */
function __construct($db, $table) {
  $this->db       = $db;
  $this->table    = $table;
  $this->dbfields = array();

  $rs = mysqli_query($db, 'SHOW COLUMNS FROM `' . $this->table . '`');
  while($row = mysqli_fetch_assoc($rs))
    $this->dbfields[] = $row['Field'];
}

将它放在你的父类中,然后你可以更进一步,并将它添加到你的父类,这样你就可以检查列名。

function __set($member, $value) {
  if (!in_array($member, $this->dbfields))
    throw new Exception('The table ' . $this->table . ' does not have a column named ' . $member);
  $this->$member = $value;
}

function __get($member) {
  if (!in_array($member, $this->dbfields))
    throw new Exception('The table ' . $this->table . ' does not have a column named ' . $member);
  return $this->$member;
}
相关问题