简体   繁体   English

INSERT INTO数据库不起作用

[英]INSERT INTO database won't work

I'm trying to enter data into a database (it's a reactions script) but I always get a error and it's driving my crazy. 我试图将数据输入数据库(这是一个反应脚本),但是我总是遇到错误,这使我发疯。

This is the code that i'm using: 这是我正在使用的代码:

    if (mysql_query("INSERT INTO gastenboek (naam, bericht, datum, ip, plantid) VALUES ('".trim(mysql_real_escape_string($_POST['naam']))."', '".trim(mysql_real_escape_string($_POST['bericht']))."', $datum_reactie, '".mysql_real_escape_string($_SERVER['REMOTE_ADDR'])."', $id)")) {
        echo '<span style="color:green; font-weight: bold"Je reactie is succesvol toegevoegd!</span>';
    } else {
        echo '<span style="color:green; font-weight: bold">Er is iets fout gegaan en je reactie is niet toegevoegd. Probeer het later opnieuw.</span>';
    }

That doesn't seem to work. 这似乎不起作用。 But when I display the data using the following code: 但是当我使用以下代码显示数据时:

    if (mysql_query("INSERT INTO gastenboek (naam, bericht, datum, ip, plantid) VALUES ('".trim(mysql_real_escape_string($_POST['naam']))."', '".trim(mysql_real_escape_string($_POST['bericht']))."', $datum_reactie, '".mysql_real_escape_string($_SERVER['REMOTE_ADDR'])."', $id)")) {
        echo '<span style="color:green; font-weight: bold"Je reactie is succesvol toegevoegd!</span>';
    } else {
        echo '<span style="color:green; font-weight: bold">Er is iets fout gegaan en je reactie is niet toegevoegd. Probeer het later opnieuw.</span>';
        $qry = "INSERT INTO gastenboek (naam, bericht, datum, ip, plantid) VALUES ('".trim(mysql_real_escape_string($_POST['naam']))."', '".trim(mysql_real_escape_string($_POST['bericht']))."', $datum_reactie, '".mysql_real_escape_string($_SERVER['REMOTE_ADDR'])."', $id)"; echo $qry;
    }

This is what I get: 这是我得到的: 在此处输入图片说明

So everything seems right? 所以一切似乎都正确吗?

This is what my database looks like: 这是我的数据库的样子: 在此处输入图片说明

Looks like you need quotes around the date value in the third column. 看起来您需要在第三列中的日期值周围加上引号。

You might want to use bind variables because it will make the quotes easier to work with and safer (from a SQL injection perspective). 您可能想使用绑定变量,因为它会使引号更易于使用且更安全(从SQL注入角度而言)。 Something like this: 像这样:

$stmt = mysqli_prepare($db, "INSERT INTO gastenboek (name, bericht, datum, ip, platid) VALUES (?, ?, ?, ?, ?)");

mysqli_stmt_bind_param ($stmt, 'sssss', $_POST['naam'], $_POST['bericht'], $datum_reactie, $_SERVER['REMOTE_ADDR'], $id);

mysqli_stmt_execute($stmt);

Try to quote your 'date' field. 尝试引用您的“日期”字段。 This might fix things for you. 这可能会为您解决问题。

You could use ( http://php.net/manual/en/function.mysql-error.php ): 您可以使用( http://php.net/manual/en/function.mysql-error.php ):

echo mysql_errno($link) . ": " . mysql_error($link) . "\n";

That returns the last error of mysql. 那将返回mysql的最后一个错误。 As said Lashane put '$datum_reactie' 正如拉尚所说的那样'$ datum_reactie'

Also, I have to say it: "don't use msysql_*, it's deprecated". 另外,我必须说:“不使用msysql_ *,已弃用”。

查询可能由于多种原因而失败-语法错误,不存在的表/列,权限不足等。您最好的选择是使用mysql_error()/ mysql_errno()在失败时打印错误(在else {}中)。 。

是的...因为所有都是varchar或文本,所以必须将所有值都用单引号引起来

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM