为什么我提交的数据,弄出来的时候会有重复

MILKZHOU 2009-11-27 12:21:25
问题:
为什么我提交的数据,弄出来的时候会有重复的呢?怎么解决呢?

先看一下正确的:


但现在却变成这样啦:


涉及到的数据库如下:


涉及的代码如下:

<table width="600" border="0">
<tr>
<td><?php include_once("result_top.php");?></td>
</tr>
</table>
<form action="result_add.php" method="post">
<table width="600" border="1">
<tr>
<td>学生姓名</td>
<td>学号</td>
<td>性别</td>
<td>专业</td>
<td>班级</td>
</tr>

<tr>
<?php
include_once("conn.php");
$name = $_POST ['name'];
$sql="select * from db_stu_info where name='$name'";
$query = mysql_query($sql) or die("执行sql语句失败".mysql_error());
if(isset($name))
{
$num = mysql_num_rows($query);
if ($num > 0) {
while($row = mysql_fetch_array($query))
{
?>
<!--在input中,增加value的作用就是想在提交的数据库的时候可以提交得到-->
<td><input name="name" type="hidden" value="<?php echo $row['name'];?>" /><?php echo $row['name'] ?></td>
<td><input name="num" type="hidden" value="<?php echo $row['num'];?>" ><?php echo $row['num']; ?></td>
<td><?php echo $row['sex']; ?></td>
<td><?php echo $row['department']; ?></td>
<td><?php echo $row['class1']; ?></td>
<?php
}
}
else{
echo "<script>alert('没有此学生信息,请重新填写!'); history.back();</script>";
}
}
?>
</tr>
</table>
<table border="1" width="600">
<tr>
<td>科目</td>
<td>成绩</td>
</tr>
<?php
$sql="select b.subject,b.id,db_stu_result.result from db_stu_info a, db_subject b, db_stu_result where a.department=b.department and a.name='$name' order by b.id";
//SELECT subject,id FROM db_subject WHERE department = (SELECT department FROM db_stu_info WHERE name = '$name')比上式慢
//作用是:找两个表中先找出department字段一样的,然后再找name字段与POST过来的一样的,找出来的结果用subject来显示
$query=mysql_query($sql) or die("执行sql语句失败".mysql_error());
$num = mysql_affected_rows ();
if ($num > 0) {
while($row = mysql_fetch_array($query))
{
$subject_count=$subject_count.$row["subject"].",";//把所有的科目都赋值给$subject_count
$subject_id_count=$subject_id_count.$row["id"].",";//把所有的ID号都赋值给$subject_id_count
$subject_tag=$row["id"];//把ID号赋值给$subject_tag,调用这个就是在调用ID号
?>
<tr>
<td width="100"><input name="subject_<?php echo $subject_tag?>" value="<?php echo $row["subject"]?>" type="hidden" ><?php echo $row['subject']?></td>
<td><input type="text" name="result_<?php echo $subject_tag?>" value="<?php echo $row["result"]?>" /></td>
</tr>
<?php
}
?>
<input name="subject_id" value="<?php echo $subject_id_count?>" type="hidden" ><!--ID-->
<input name="subject" value="<?php echo $subject_count?>" type="hidden" ><!--科目-->
<?php
// echo $subject_count;
// echo $subject_id_count."<br>";
// echo $subject_tag;

}
?>
<tr>
<td border="0" colspan="2"><input type="submit" name="sub" value="提交"/>  <input type="reset" name="reset" value="重写"></td>
</tr>
</table>
</form>



...全文
100 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
MILKZHOU 2009-11-27
  • 打赏
  • 举报
回复
我觉得应该是那个SQL有问题吧


select b.subject,b.id,db_stu_result.result from db_stu_info a, db_subject b, db_stu_result where a.department=b.department and a.name='$name' order by b.id
MILKZHOU 2009-11-27
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 siramizu 的回复:]
看看数据库中是否有重复数据
[/Quote]

没有,我已经删掉了,有重复的话
Siramizu 2009-11-27
  • 打赏
  • 举报
回复
看看数据库中是否有重复数据
MILKZHOU 2009-11-27
  • 打赏
  • 举报
回复
这些内容,主要是当我需要修改成绩的时候用的,所以大家可以暂时不要理它


$subject_count=$subject_count.$row["subject"].",";//把所有的科目都赋值给$subject_count
$subject_id_count=$subject_id_count.$row["id"].",";//把所有的ID号都赋值给$subject_id_count
$subject_tag=$row["id"];//把ID号赋值给$subject_tag,调用这个就是在调用ID号

huang020 2009-11-27
  • 打赏
  • 举报
回复
$sql="select b.subject,b.id,db_stu_result.result from db_stu_info a, db_subject b, db_stu_result where a.department=b.department and a.name='$name' order by b.id";

换成这句试试

$sql="select b.subject,b.id,db_stu_result.result from db_stu_info a, db_subject b, db_stu_result c where a.department=b.department and c.subject= b.subject and a.name='$name' order by b.id";

你查的是三个表中的约束结果,可你的语句只对两个表做了约束
死胖子 2009-11-27
  • 打赏
  • 举报
回复
$sql="select b.subject,b.id,db_stu_result.result from db_stu_info a, db_subject b, db_stu_result where a.department=b.department and a.XXX=db_stu_result.XXX and a.name='$name' order by b.id";
//其中XXX是两个表之前的关联字段,如果你两个表中没有关联字段的话,也不能使用NAME做关联字段(应该新增一字段,比如学号),因为容易重名,另外 建议也不要使用(a.name='$name')来作为条件判断,应该使用学校这样的唯一字段来进行判断
t240034137 2009-11-27
  • 打赏
  • 举报
回复
你把你的数据库语句 在数据库中查一下! 看看什么结果!
MILKZHOU 2009-11-27
  • 打赏
  • 举报
回复
结果是这个:

SELECT c.name, b.subject, b.id, c.result
FROM db_stu_info a, db_subject b, db_stu_result c
WHERE a.department = b.department
AND b.subject = c.subject
AND a.name = "朱砂"
ORDER BY id
LIMIT 0 , 30
MILKZHOU 2009-11-27
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 jnkc 的回复:]
另外,用姓名查询可能会遇到重名问题,应该用学号比较好。
[/Quote]

这个我知道,我只是测试
MILKZHOU 2009-11-27
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 jnkc 的回复:]
应该就是查成绩的sql有问题,db_stu_result缺乏条件约束,试试看:
SQL codeselect b.subject,b.id,(select r.resultfrom db_stu_result rwhere r.num.a.numand r.subject=b.subject)as resultfrom db_stu_info a, db_subject bwhere a.name='$name'and b.department=a.departmentorderby b.id
[/Quote]


不行,有错误
江南昆虫 2009-11-27
  • 打赏
  • 举报
回复
另外,用姓名查询可能会遇到重名问题,应该用学号比较好。
江南昆虫 2009-11-27
  • 打赏
  • 举报
回复
应该就是查成绩的sql有问题,db_stu_result缺乏条件约束,试试看:
select b.subject,b.id,(select r.result from db_stu_result r where r.num.a.num and r.subject=b.subject) as result from db_stu_info a, db_subject b where a.name='$name' and b.department=a.department order by b.id

21,891

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧