gpt4 book ai didi

php - 需要帮助查找错误,数据库中没有添加任何内容

转载 作者:行者123 更新时间:2023-11-29 06:04:42 25 4
gpt4 key购买 nike

我即将创建一个简单的代码来添加新闻...(我的小项目)当我单击“提交”时,我会收到成功文本,并重定向回页面,但没有添加数据,如果我将表单留空,则不会出现错误消息。

...mysql_real_escape_string 会帮我解决一些麻烦吗?

<?php 
include('connect_db.php');
if(isset($_POST['submit']))
{
$title = mysql_real_escape_string(htmlspecialchars($_POST['title']));
$text = mysql_real_escape_string(htmlspecialchars($_POST['text']));

if ($title == '' || $text == '')
{

// generate error message
$error = 'ERROR: Please fill in all required fields!';
}

$result = mysql_query("INSERT INTO news ('id', 'date', 'title', 'text')
VALUES ('NULL', NOW(),'$title','$text')",$conn);

echo "<b>Thank you!<br>You'll be redirected in (4) secs...";
echo "<meta http-equiv=Refresh content=4;url=add.php>";

} else {

echo "<form method='post' action='add.php'>
<legend>Add news</legend>
<label>Title</label>
<input type='text' name='title'>
<label>Text</label>
<textarea rows='5' name='text'></textarea>
<br />
<button type='submit' name='submit' class='btn'>Submit</button>
</form>";
}?>

最佳答案

NULL 是一个关键字,不应该用引号引起来。

同样,字段名称应括在反引号 ` 中,而不是单引号 ' 中,并且您还应将表名称括在其中以保持一致性。

此外,看起来id已设置为AUTO_INCRMENT,因此您无需将其设置为NULL。如果dateTIMESTAMP(它应该是),那么您还可以设置DEFAULT CURRENT_TIMESTAMP并将其从查询中删除。

$result = mysql_query("INSERT INTO `news` (`title`,`text`) VALUES ('$title','$text')");

关于php - 需要帮助查找错误,数据库中没有添加任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12098415/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com