这是执行插入
<?php
    $conn=mysql_connect("localhost","root","123456") or die("数据库服务器连接错误".mysql_error());
    mysql_select_db("db_database18",$conn) or die("数据库访问错误".mysql_error());
    mysql_query("set names gb2312");
$title=$_POST[txt_title];
$content=$_POST[txt_content];
$createtime=date("Y-m-d H:i:s");
$sql=mysql_query("insert into tb_affiche(title,content,createtime)values('$title','$content','$createtime')");
echo "<script>alert('公告信息添加成功!');window.location.href='page_affiche.php';</script>";
mysql_free_result($sql);
mysql_close($conn);
?>
这是表单信息:
 <form name="form1" method="post" action="check_add_affiche.php">
                              <table width="520" height="212"  border="0" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF">
                                <tr>
                                  <td width="87" align="center">公告主题:</td>
                                  <td width="433" height="31"><input name="txt_title" type="text" id="txt_title" size="40">
                    * </td>
                                </tr>
                                <tr>
                                  <td height="124" align="center">公告内容:</td>
                                  <td><textarea name="txt_content" cols="50" rows="8" id="txt_content"></textarea></td>
                                </tr>
                                <tr>
                                  <td height="40" colspan="2" align="center"><input name="Submit" type="submit" class="btn_grey" value="保存" onClick="return check(form1);">
&nbsp;                                    <input type="reset" name="Submit2" value="重置"></td>
                                </tr>
                              </table>
                          </form>
为什么每次提交都会一下插入两条重复数据???
怎么解决之。。

解决方案 »

  1.   

    mysql_free_result($sql);
    这个是多余的
      

  2.   

    onClick="return check(form1);
    check的js方法是怎么定义的呢?最好是onsubmit  = 
      

  3.   

    你加个判断就好了。if(isset($_POST['Submit'])){
        $conn=mysql_connect("localhost","root","123456") or die("数据库服务器连接错误".mysql_error());
        mysql_select_db("db_database18",$conn) or die("数据库访问错误".mysql_error());
        mysql_query("set names gb2312");
        $title=$_POST[txt_title];
        $content=$_POST[txt_content];
        $createtime=date("Y-m-d H:i:s");
        $sql=mysql_query("insert into tb_affiche(title,content,createtime)values('$title','$content','$createtime')");
        echo "<script>alert('公告信息添加成功!');window.location.href='page_affiche.php';</script>";
        mysql_free_result($sql);
        mysql_close($conn);
    }
      

  4.   

    你代码有问题,把mysql_free_result($sql);去掉
    <?php
        $conn=mysql_connect("localhost","root","123456") or die("数据库服务器连接错误".mysql_error());
        mysql_select_db("db_database18",$conn) or die("数据库访问错误".mysql_error());
        mysql_query("set names gb2312");
        $title=$_POST[txt_title];
        $content=$_POST[txt_content];
        $createtime=date("Y-m-d H:i:s");
        $sql=mysql_query("insert into tb_affiche(title,content,createtime)values('$title','$content','$createtime')");
        echo "<script>alert('公告信息添加成功!');window.location.href='page_affiche.php';</script>";
       mysql_free_result($sql);// 你这句又不是查询语句,用这个函数干什么,去掉它吧
        mysql_close($conn);//干脆这个也不要了,当然留在还挺好,符合教科书精神
    ?>
      

  5.   

    Click="return check(form1);" 这个去掉我估计就ok了