首页 > 解决方案 > 在 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;
    }
}

标签: phpmysql

解决方案


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']


推荐阅读