Saturday, 24 August 2013

PHP prepared statement fails to add to mysql table; Same INSERT query via CLI mysql works. Frustration ensues

PHP prepared statement fails to add to mysql table; Same INSERT query via
CLI mysql works. Frustration ensues

This is so noobish, it's driving me absolutely mad. I've been working with
mysql for about a week now so I'm reliant upon looking up error messages
and reading man pages.
I have a table imaginatively called ... 'table'. The first field is a
record ID field (INT), the next field is the date (DATETIME) and then a
user id field (INT). Everything else is a unsigned TINYINT.
I made sure that all the fields lined up properly. Making sure that I
wasn't trying to force a DATE into an INT field. Also made sure there was
a ; at the end of my query.
Here's the php, and it generates no error:
function saveEvaluation($query) {
$conn = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
or die ('mysqli failure');
echo "<p>Attempting query: {$query}</p>";
if ($stmt = $conn->prepare($query))
$stmt->execute();
echo "error: {$mysqli->error}";
}
The query that's passed is a simple string.
INSERT INTO table VALUES (NULL, "2013-08-24 22:08:30", 0, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1);
If entered directly via mysql> it adds to the table, otherwise when the
php executes, nothing is added to the table and seeing no error.
mysql> INSERT INTO table VALUES (NULL, "2013-08-24 22:08:33", 0, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1);
Query OK, 1 row affected (0.01 sec)
mysql> SELECT * FROM table;
... table too long to add but the only entry added was from mysql> use.
Now, I have tried to bindparam() as well by simply changing the query to
$query = "INSERT INTO table VALUES (NULL, {$date}, 0, ..., 1);";
then using a
$stmt ->bindparam('s', $date);
$stmt ->execute();
That fails to add to the table and also fails silently. I have to be
missing something basic but I can't see what. Everything I've read tells
me these should be working.
The one thing I'm not sure how to verify is whether or not my DB_USER has
the rights to alter/insert on a table but I'm not sure if I should expet
that mysql would tell me that via error. I guess I'll be looking into this
while awaiting suggestions.

No comments:

Post a Comment