php - 在 PHP 函数中添加多行
问题描述
基本上我想将“价格”添加到数据表中。我怎样才能做到这一点?“价格”已添加到数据库中。
形式:
<form action="main/insertRow" method="post">
<label>Name:</label>
<input type="text" name="text" />
<label>Price:</label>
<input type="text" name="price" />
<input type="submit" value="Add" />
</form>
功能:
public function insertRow($data) {
$sth = $this -> db -> onlyExecute('INSERT INTO data (`text`) VALUES (:data)', array(':data' => $data));
if ($sth) {
echo true;
} else {
echo false;
}
}
解决方案
What is inside 'onlyExecute' function? Are you sure that your $data is not null in 'insertRow' function? If it's a PDO you can doing that:
$sth = $this->db->prepare('INSERT INTO data (`text`) VALUES (:data)');
$sth->execute([':data' => $data]);
or
$sth = $this->db->prepare('INSERT INTO data (`text`) VALUES (:data)');
$sth->bindValue(':data', $data);
$sth->execute();
Also make sure that your $data is a string, if it is an array you need to type $data['your_key']
推荐阅读
- reactjs - 服务器端的条件渲染
- scala - Spark Shell 允许重新声明相同的不可变变量
- python - 当属性位于根标记内时,如何将 XML 转换为 CSV?
- python-3.x - 具有多个元素的数组的真值是不明确的。在图像分类问题中使用 a.any() 或 a.all()
- excel - EXCEL 如何在另一个工作表的单行中连接多行的值?
- jira - 如何找到没有任何测试的测试集?
- php - 如何进行数据库检查并根据结果更新或插入(不断收到 HTTP 500 错误)
- javascript - 如何在javascript中验证对象
- visual-studio-code - 在 Visual Studio Code 中单击鼠标中键进行滚动?
- c# - 起订量设置覆盖