views:

133

answers:

3

I'm having some trouble using MySQLi to insert values into a database. I don't get any errors, but the values being inserted are not correct at all. One of the TEXT fields is always empty, and the other one always has the value "ý". The INT field always contains the value 50396416. I am using utf8_general_ci.

CREATE TABLE events (
    id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY ,
    title TEXT NOT NULL ,
    content TEXT NOT NULL ,
    date INT UNSIGNED NOT NULL
);

Here's what I have to insert the values:

function insertEvent($title, $content, $date) {

    $stmt = $this->db->prepare('INSERT INTO events (title, content, date) VALUES (?, ?, ?)');

    $stmt->bind_param('ssi', $title, $content, $date);
    $stmt->execute();

    $stmt->close();
}

It seems pretty simple, so I don't know what the problem could be. If you have any advice, I'd love to hear it!

I don't know if this could be a problem with my code, or with the database, because everything did work correctly on one server, but not on another.

Update

Actually I just noticed, through phpinfo(), that the servers seem to be using different MySQL versions. Could that possibly be causing the problem?

I'm also pretty sure that the data I'm using is correct. I'm getting the values from a form using $_POST. For example, if, for the "title" field, I enter "asdf":

$stmt->bind_param('ssi', $title, $content, $date);
$title = $_POST['title'];
echo $title; // echoes "asdf"

It looks like it was just an incompatibility with the MySql version (4.something) on the server. I got them to update it, and it seems to work fine now. Thanks everyone, for helping!

A: 

"It seems pretty simple, so I don't know what the problem could be."

do you have a similar example that works? another piece of code using mysqli that you can compare it to

Fire Crow
The exact same thing is working, but only on one server that I'm using. The difference (that I notice) is the MySQL version. Will I need to update to the latest version? (might be difficult to get the host to do that)
aldld
This should have been a comment, not an answer.
Pekka
oops meant to leave a comment on main question.
prodigitalson
+1  A: 

Either use a debugger (e.g. xdebug + netbeans as frontend) or add some more debug output.

echo "<pre>Debug: $stmt->bind_param('ssi', $title, $content, $date);\n</pre>";
$stmt->bind_param('ssi', $title, $content, $date);
$stmt->execute();
VolkerK
A: 

try this:

$stmt = $this->db->prepare("INSERT INTO events (title, content, date) VALUES ('?', '?', '?')");
Amad