开发者

Not inserting correct values into MySQL database

开发者 https://www.devze.com 2022-12-20 23:25 出处:网络
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 o

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 databa开发者_运维知识库se, 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!


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();


Try this:

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

精彩评论

暂无评论...
验证码 换一张
取 消