发布到数据库
所以我有这样的代码,它接受用户插入的消息/帖子,并将其发布到数据库,然后显示一个单独的页面。我已经让展示公园工作正常,它只是试图插入数据库,这就是问题所在
这段代码...
<?php
mysql_connect("localhost", "root", "");
mysql_select_db("test");
$time = time();
mysql_query "INSERT INTO threads (title, message, author, dated);"
VALUES (NULL,'$_POST[title]','$_POST[message]','$_POST[author]','0','$time');
echo "Thread Posted.<br><a href='Untitled9.php'>Return</a>";
?>
不会将信息发布到数据库中!
这是为什么?如何解决?
id int(11) No None AUTO_INCREMENT
title varchar(255) latin1_swedish_ci No None
message text latin1_swedish_ci No None
author varchar(255) latin1_swedish_ci No None
replies int(11) No None
posted varchar(255) latin1_swedish_ci No None
votes_up int(11) No 0
votes_down int(11) No 0
So i have so code that takes a message/post users insert and its meant to post it to a database and this then displays and a seperate page. Ive got the displaying park working fine its just trying to insert to database which is the problem
This code...
<?php
mysql_connect("localhost", "root", "");
mysql_select_db("test");
$time = time();
mysql_query "INSERT INTO threads (title, message, author, dated);"
VALUES (NULL,'$_POST[title]','$_POST[message]','$_POST[author]','0','$time');
echo "Thread Posted.<br><a href='Untitled9.php'>Return</a>";
?>
wont post the infomation into the database!
Why is this and how can it be resolved?
id int(11) No None AUTO_INCREMENT
title varchar(255) latin1_swedish_ci No None
message text latin1_swedish_ci No None
author varchar(255) latin1_swedish_ci No None
replies int(11) No None
posted varchar(255) latin1_swedish_ci No None
votes_up int(11) No 0
votes_down int(11) No 0
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
更新:
应该发布而不是注明日期。
这是你的问题:
将其更改为:
我看到你也有空值,这让我相信你使用带有自动增量的ID,如果是这种情况,你也需要提供这个。示例:
编辑:此处
请注意,直接从发布数据插入值是不安全的,会让您容易受到各种攻击。
Update:
Should be posted not dated.
Heres your problem:
Change it to:
I see you have null values also, this makes me believe your using an ID with an auto increment, if this is the case, you need to supply this also. Example:
Edit: Here
Note inserting values straight from post data is unsafe and leaves you open to various attacks.
您尝试添加到新行的值多于指定的值。
您要设置 4 个值
,并且要分配 6 个值。
这是不可能的
同时验证 $_POST 数据 = 阅读此 从不信任用户输入。
并阅读手册 PHP & MYSQL
The values you are trying to add to the new row are more that the assigned values .
that are 4 values you want to set
and you are assigning 6 values.
which is not possible
Also validate $_POST data = read this Never trust user input.
And read the manual PHP & MYSQL
分号正在结束您的 sql 语句。你的询问还没有结束。您仍然需要指定要插入的值。
The semicolon was ending your sql statment. Your query wasn't finished. You still needed to specify the values you wanted to insert.
您提前结束了字符串。应该是:
此外,您的代码很可能成为 SQL 注入的目标。您应该使用 MySQLi 类和 PreparedStatement 插入您的帖子。
You ended the String to early. Should be:
Also, your code is very likely to become a target of SQL-Injections. You should use the MySQLi-class and a PreparedStatement to insert your posts.
问题数量:
以下是经过测试的代码:
Number of issues :
Here's tested code: