将数据从php插入数据库mysql表

时间:2015-10-22 22:54:44

标签: php mysql sql-insert

我在将数据从php插入数据库时​​遇到问题。

档案是: Patient.php

表名是: patient_tbl

内表是:头像教育

这里是插入代码: 以下代码没有问题,'education'的值将被插入到表

$sql = "INSERT INTO " . $this->_table;
$sql .= "education,";
$params = array(
  urlencode($patient->getEducation()),
);
return $this->exec($sql, $params);
}

但现在这是我的问题, “阿凡达”不插入表格

错误消息为:“无法准备查询。”

$sql = "INSERT INTO " . $this->_table;
$sql .= "education, avatar, ";
$params = array(
  urlencode($patient->getEducation()),
  urlencode($patient->getAvatar()),
);
return $this->exec($sql, $params);
}

我的问题是什么?我无法找到解决方案,。

我非常感谢你的帮助。

谢谢

1 个答案:

答案 0 :(得分:1)

基本语法sql查询'INSERT'是

INSERT [INTO] tbl_name [(col_name,...)]
        VALUES (expression,...),(...)

如果使用某些参数,则需要添加括号:

$sql = "INSERT INTO " . $this->_table;
$sql .= "(education, avatar) VALUES (?, ?)";
$params = array(
  urlencode($patient->getEducation()),
  urlencode($patient->getAvatar()),
);
return $this->exec($sql, $params);
}

然后写一下$ this-> exec?

相关问题