简体   繁体   English

PHP插入mysql问题

[英]PHP Inserting into mysql issue

I am having trouble when I run the following command: 运行以下命令时遇到麻烦:

$stmt = $this->db->prepare("INSERT INTO checked_in (attendee_id) VALUES (?)");
$stmt->bind_param("i", $id);
$stmt->execute();
$stmt->close();

This is part of a bigger page but this is the part that isn't working. 这是更大页面的一部分,但是这是行不通的。 I first pull the $id from the SQL database so I know I have a connection. 我首先从SQL数据库中提取$ id,所以我知道已经建立了连接。 I just can't seem to get this update part to work. 我似乎无法使此更新部分正常工作。

mysql > CREATE TABLE checked_in ( 
        id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
        attendee_id int NOT NULL,
        check_in_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP);

This is the code I am using to build the table. 这是我用来构建表的代码。 Am I doing something wrong? 难道我做错了什么? I think my problem is that it can talk to the database but I don't know if I set up the table wrong where it is expecting the id and the timestamp to be manually entered. 我认为我的问题是它可以与数据库对话,但是我不知道我在期望手动输入ID和时间戳的地方是否设置了错误的表。 It works if I am on my sql prompt and use this command 如果我在sql提示符下并使用此命令,它将起作用

mysql> INSERT INTO checked_in (attendee_id) VALUES (1);

Here is the main part of the program (in the process of adapting the tutorial to my own): 这是程序的主要部分(在使教程适应我自己的过程中):

function checkIn() {

    // Check for required parameters
    if (isset($_POST["phone"]))  {

        // Put parameters into local variables
        $phone = $_POST['phone'];

        // Look up code in database
        $user_id = 0;
        $stmt = $this->db->prepare('SELECT id, first, last, email FROM attendees WHERE phone=?');
        $stmt->bind_param("s", $phone);
        $stmt->execute();
        $stmt->bind_result($id, $first, $last, $email);
        while ($stmt->fetch()) {
            break;
        }
        $stmt->close();

        // Bail if number doesn't exist
        if ($id <= 0) {
            sendResponse(400, 'Invalid number');
            return false;
        }   

        // Check to see if this device already redeemed 
        $stmt = $this->db->prepare('SELECT id FROM checked_in WHERE attendee_id=?');
        $stmt->bind_param("i", $id);
        $stmt->execute();
        $stmt->bind_result($checkedInID);
        while ($stmt->fetch()) {
            break;
        }
        $stmt->close();

        // Bail if number already checked in
        if ($checkedInID > 0) {
            sendResponse(403, 'Number already checked in');
            return false;
        }

        // Add tracking of redemption
        $stmt = $this->db->prepare("INSERT INTO checked_in (attendee_id) VALUES (?)");
        $stmt->bind_param("i", $id);
        $stmt->execute();

        // Return unlock code, encoded with JSON
        $result = array(
            "checked_in" => "true",
        );
        sendResponse(200, json_encode($result));
        return true;
    }
    sendResponse(400, 'Invalid request');
    return false;
        }
}

Ok so I checked my apache error logs and this is what it is saying: 好的,所以我检查了我的apache错误日志,这就是它的意思:

[Fri Jul 25 15:06:41.996107 2014] [:error] [pid 16824] [client 127.0.0.1:52699] PHP Notice:  Undefined variable: db in /var/www/html/index.php on line 129
[Fri Jul 25 15:06:41.996171 2014] [:error] [pid 16824] [client 127.0.0.1:52699] PHP Fatal error:  Call to a member function prepare() on a non-object in /var/www/html/index.php on line 129

If you bind your parameters while using a question mark in your queries, then you shouldn't use i as parameter. 如果在查询中使用问号时绑定了参数,则不应使用i作为参数。 Use a numeric parameter instead. 请改用数字参数。

Try 尝试

$stmt->bind_param(1, $id);

From them manual : 从他们手册

For a prepared statement using named placeholders, this will be a parameter name of the form :name . 对于使用命名占位符的准备好的语句,这将是形式为:name的参数:name For a prepared statement using question mark placeholders, this will be the 1-indexed position of the parameter. 对于使用问号占位符的准备好的语句,这将是参数的1索引位置。

That said, please describe what $id is assigned to in this function. 也就是说,请描述在此函数中分配的$id

Try this: 尝试这个:

$stmt = $this->db->prepare("INSERT INTO checked_in (attendee_id) VALUES (?)");
$stmt->execute( array( $id ) );

should work. 应该管用。

If you want to check the error, you should do: 如果要检查错误,则应该执行以下操作:

$stmt = $this->db->prepare("INSERT INTO checked_in (attendee_id) VALUES (?)");
$check = $stmt->execute( array( $id ) );

if ( $check ) {
    echo 'Nice work!';
} else {
    $error = $stmt->errorInfo();
    echo $error[2];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM