【问题标题】:mysqli function doesn't insert valuesmysqli 函数不插入值
【发布时间】:2016-07-10 11:07:14
【问题描述】:

我正在尝试通过 mysqli 以面向对象的方式连接到数据库。我有一些错误,并解决了它们,但现在我只能解决这个问题。我的代码在这里,所有名称(数据库名称、用户、密码、主机和表名称)都是正确的(实际上是复制和粘贴),但查询仍然返回 0。

<?php


class DbConnection
{
    public $link;

    public function __construct()
    {
        $this->link = new mysqli("localhost","root","","todo");
        if (mysqli_connect_errno()) {
            printf("Connect failed: %s\n", mysqli_connect_error());
            exit();
        }
    }

    function RegisterUsers($username, $password, $ip, $name, $email)
    {
        $stmt = $this->link->prepare("INSERT INTO users (Username, `Password`, ip, Name, Email) VALUES (?,?,?,?)");
        $stmt->bind_param("sssss", $username, $password, $ip, $name, $email);
        $stmt->execute();

        $stmt->store_result();
        $count = $stmt->num_rows;
        return $count;
    }

}

$dbConn = new DbConnection();
echo $dbConn->RegisterUsers("a","a","a","a", "a");


?>

编辑:使用这段代码,我得到一个

在布尔值上调用成员函数 bind_param()

错误。

【问题讨论】:

  • 您是否遇到任何错误?或使用 printf("Error: %s.\n", $stmt->error);打印错误
  • 是的,我收到一个错误,没有电子邮件值。现在我收到Call to a member function bind_param() on boolean 错误。我更新了代码@Naisapurushotham
  • 将另一个占位符添加到bind_param。它应该看起来像这样bind_param("sssss",$username, $password, $ip, $name, $email)。每个参数一个占位符,? 标记相同,您需要其中五个
  • 那个?,不记得他们了...解决了!非常感谢! @AlexAndrei 你能把它贴在答案上吗?

标签: php mysql mysqli


【解决方案1】:

Passwordname 在 mysql 中是 keywords。如果要将其用作列名,则必须将其放在反引号中以将其转义

    $stmt = $this->link->prepare("INSERT INTO users (Username, `Password`, ip, `Name`) VALUES (?,?,?,?)");

【讨论】:

  • @user6463207 执行语句后检查错误:php.net/manual/de/mysqli.error.php
  • 请在我的帖子中查看我的编辑和 cmets 以解决我的实际问题,谢谢
  • @user6463207 现在您有 5 个列名,values 中只有 4 个占位符。请不要更改问题,将所有更改添加为问题的更新
  • 问题已更改,代码已更新。现在我有了带有 5 个字符串值和 5 个变量的 bind_param()。
猜你喜欢
  • 2014-12-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-30
  • 2012-08-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多