Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not inserting correct values into MySQL database

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!

like image 346
Eric Bannatyne Avatar asked Feb 21 '10 00:02

Eric Bannatyne


1 Answers

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();
like image 67
VolkerK Avatar answered Nov 02 '22 10:11

VolkerK