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. 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. It works if I am on my sql prompt and use this command
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:
[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
<?phptagerror_reporting(E_ALL); ini_set('display_errors', 1);see if it yields anything.mysqli_error($db);depending on the DB variable. Which API are you using? Plus, try$stmt = $db->prepareinstead of$stmt = $this->db->prepareit's hard to say what your DB connection is or if using a class.