MySQL不从一列插入数据

时间:2016-07-23 22:20:40

标签: php mysql

我正在尝试将数据插入数据库。这是我正在使用的代码。它会插入除图像(Pic)之外的所有数据。

public function insert($hey){

    try {

        $hey = $this->db->query("INSERT INTO addmember(Pic,Firstname,Lastname,Age,Gender,Phonenumber,Location,Member,Department) VALUES('".$hey['pic']."','".$hey['fname']."','".$hey['lname']."','".$hey['age']."','".$hey['gender']."','".$hey['phone']."','".$hey['loc']."','".$hey['group']."','".$hey['department']."')");

    } catch (PDOException $e) {

        echo $e->getMessage();
    }
}

1 个答案:

答案 0 :(得分:0)

您正在为数组建立索引$嘿错了。 ($ hey [" pic"]根据您的数组转储不正确)

这应该有效:

public function insert($hey){

    try {

        $hey = $this->db->query("INSERT INTO addmember(Pic,Firstname,Lastname,Age,Gender,Phonenumber,Location,Member,Department) VALUES('".$hey['Pic']/* this is where hte error was occuring */."','".$hey['fname']."','".$hey['lname']."','".$hey['age']."','".$hey['gender']."','".$hey['phone']."','".$hey['loc']."','".$hey['group']."','".$hey['department']."')");

    } catch (PDOException $e) {

        echo $e->getMessage();
    }
}

另外,为什么你没有约束输入?