Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 表单插入所有用户的数据重复时间_Php_Mysql_Insert_Records - Fatal编程技术网

Php 表单插入所有用户的数据重复时间

Php 表单插入所有用户的数据重复时间,php,mysql,insert,records,Php,Mysql,Insert,Records,我不知道为什么,但是每当一个用户在我正在工作的网站上发表文章时,它都会多次向数据库发表文章,网站上的每个成员都有一个条目(目前为3个条目) 这是我的密码 添加主题php $category=$_POST['category']; $sub_category=$_POST['sub_category']; $topic_data=$_POST['topic_data']; $posted=date("h:i:s d/m/Y"); //create date time $sql = "INSERT

我不知道为什么,但是每当一个用户在我正在工作的网站上发表文章时,它都会多次向数据库发表文章,网站上的每个成员都有一个条目(目前为3个条目)

这是我的密码

添加主题php

$category=$_POST['category'];
$sub_category=$_POST['sub_category'];
$topic_data=$_POST['topic_data'];
$posted=date("h:i:s d/m/Y"); //create date time

$sql = "INSERT INTO `topics`(category, sub_category, topic_data, posted_by, posted)VALUES('$category', '$sub_category', '$topic_data', '".$_SESSION['user_id']."', '$posted')";
$result=mysql_query($sql);

if($result){
header("Location: topics.php?category=$category&sub_category=$sub_category");
exit();
}
$sql = "SELECT users.user_id, users.username, users.profile, topics.topic_id, topics.category, topics.sub_category, 
topics.topic_data, topics.posted_by, topics.posted, topics.view, topics.reply 
FROM users, topics WHERE topics.category = '" . $_GET['category'] . "' AND  topics.sub_category = '" . $_GET['sub_category'] . "' ORDER BY topics.posted DESC";

$result = mysql_query($sql);

while($rows = mysql_fetch_array($result)){
Topics.php

$category=$_POST['category'];
$sub_category=$_POST['sub_category'];
$topic_data=$_POST['topic_data'];
$posted=date("h:i:s d/m/Y"); //create date time

$sql = "INSERT INTO `topics`(category, sub_category, topic_data, posted_by, posted)VALUES('$category', '$sub_category', '$topic_data', '".$_SESSION['user_id']."', '$posted')";
$result=mysql_query($sql);

if($result){
header("Location: topics.php?category=$category&sub_category=$sub_category");
exit();
}
$sql = "SELECT users.user_id, users.username, users.profile, topics.topic_id, topics.category, topics.sub_category, 
topics.topic_data, topics.posted_by, topics.posted, topics.view, topics.reply 
FROM users, topics WHERE topics.category = '" . $_GET['category'] . "' AND  topics.sub_category = '" . $_GET['sub_category'] . "' ORDER BY topics.posted DESC";

$result = mysql_query($sql);

while($rows = mysql_fetch_array($result)){

我认为
add_topic.php
不会插入多行。您可以通过直接查看表来验证这一点

我相信您的问题是您在
Topics.php
中的查询

$sql = "SELECT users.user_id, users.username, users.profile, topics.topic_id,
               topics.category, topics.sub_category, topics.topic_data, 
               topics.posted_by, topics.posted, topics.view, topics.reply 
        FROM users, topics 
        WHERE topics.category = '" . $_GET['category'] . "' 
        AND  topics.sub_category = '" . $_GET['sub_category'] . "' 
        ORDER BY topics.posted DESC";
您正在对
用户
主题
进行连接,但您没有定义它们之间的链接。因此,它为每个用户创建了一行,用于每个主题帖子

您可以将其视为此SQLFiddle中的第一个示例结果集-

您要做的是将
用户左键加入主题
的topics.posted\u by=users.user\u id

这是第二个示例结果集,位于-

因此,您的查询现在将是(转义
$\u GET
,以防止SQL注入)-


注意-您不应该在新代码中使用
mysql.*
函数。从php v.5.5开始折旧。您应该将代码更新为
mysqli.*
PDO

这张表格是怎么贴的?这可能就是问题所在,在您的表中实际上有3篇文章,或者它只是在Topics.php中显示3篇文章?是为每个用户发布一次,并且每个用户的用户id不同,还是为每个用户和相同的用户id每次发布一次?听起来调用
add_topic.php
的方式有问题。
add_topic.php
中的代码是否处于循环中?谢谢,再次检查后,您是对的,它没有复制记录,只是以这种方式显示,我目前正在通过教程等进行学习,mysql是最容易找到的,但我计划在接近完成时转换到mysqli。