这段代码有什么问题

时间:2011-04-15 12:21:35

标签: php mysql mamp

class MyClass {

    private $db;

    // Constructor 
    function __construct() {
        $this->db = new mysqli('localhost', 'root', 'root', 'Test_db');
        $this->db->autocommit(FALSE);
    }

    // Destructor 
    function __destruct() {
        $this->db->close();
    }

    // Main method 
    function MyFun() {

        // Check for required parameters
        if (isset($_POST["name"]) && isset($_POST["username"]) && isset($_POST["password"]) && isset($_POST["email"])) {
        echo "Before \n";
        $name = $_POST["name"];
        $username = $_POST["username"];
        $password = $_POST["password"];
        $email = $_POST["email"];
        $activation = 0;
        echo "After \n";
            // tracking 
            $stmt = $this->db->prepare("INSERT INTO users (name, username, password, email,activation) VALUES (?, ?, ?, ?, ?)");
            $stmt->bind_param("is", $name, $username, $password, $email, $activation); //Line 95
            $stmt->execute();
            $stmt->close();

        }

输出:

无效请求

MAMP控制台:

[15-Apr-2011 15:09:10] PHP Warning:  mysqli_stmt::bind_param() [<a href='function.mysqli-stmt-bind-param'>function.mysqli-stmt-bind-param</a>]: Number of elements in type definition string doesn't match number of bind variables in /Applications/MAMP/htdocs/Test/reg.php on line 95

数字相同,但我不知道为什么会出现此错误

3 个答案:

答案 0 :(得分:4)

$stmt->bind_param("is", $name, $username, $password, $email, $activation);

你的“定义”字符串(“是”)只包含两个定义,整数和字符串...你应该有5个。

$stmt->bind_param("sssss", $name, $username, $password, $email, $activation);

...例如......

答案 1 :(得分:2)

您的查询中只有五个?占位符,但您尝试将六个值添加到查询中。     $ stmt-&gt; bind_param(“是”,$ name,$ username,$ password,$ email,$ activation);

  1. “是”
  2. $名称
  3. $用户名
  4. $密码
  5. $电子邮件
  6. $活化
  7. 您提供的格式仅包含2个定义,但必须包含5才能匹配您的查询。试试"sssss"

答案 2 :(得分:1)

“是”是第六个变量,我建议你删除它或在语句中添加字段名称:

$stmt->bind_param("is", $name, $username, $password, $email, $activation);

从bind_param中删除:

$stmt = $this->db->prepare("INSERT INTO users (name, username, password, email,activation) VALUES (?, ?, ?, ?, ?)");
$stmt->bind_param($name, $username, $password, $email, $activation);

或添加到字段名称:

$stmt = $this->db->prepare("INSERT INTO users (**is**, name, username, password, email,activation) VALUES (?, ?, ?, ?, ?)");
$stmt->bind_param("**is**", $name, $username, $password, $email, $activation);

或     $ stmt = $ this-&gt; db-&gt; prepare(“INSERT INTO users(姓名,用户名,密码,
)     电子邮件,激活)VALUES(?,?,?,?,?)“);     $ stmt-&gt; bind_param(“issss”,$ name,$ username,$ password,$ email,$ activation);