search
HomeBackend DevelopmentPHP Tutorial 遇见过重复插入数据的请进

遇见过重复插入数据的请进

Jun 13, 2016 pm 01:34 PM
gtltnbspquot

  遇见过重复插入数据的大虾请进!
问题是这样的,我用Php写了一个表单,然后在提交的处理页面执行一条insert语句,但是我到数据看的时候发现每次插入的都是两条记录,郁闷了一个下午了,希望遇见过这类问题的大侠能帮忙解决一下。我的提交表单的文件如下所示:

<?php session_start();?>
<html>
<head>
<title>图书馆管理系统</title>
<link href="http://www.php1.cn/"></head>
<script language="javascript">
function check(form){
if(form.barcode.value==""){
alert("请输入条形码1!");form.barcode.focus();return false;
}
if(form.bookName.value==""){
alert("请输入图书姓名!");form.bookName.focus();return false;
}
if(form.price.value==""){
alert("请输入图书定价!");form.price.focus();return false;
}
form.submit();
}
</script>
<body>
<table width="776" border="0" align="center" cellpadding="0" cellspacing="0" class="tableBorder">
  <tr>
  <td>
<?php include("navigation.php");?>
</td>
  </tr>
<td>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td valign="top" bgcolor="#FFFFFF"><table width="99%" height="510" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF" class="tableBorder_gray">
<tr>
<td height="510" align="center" valign="top" style="padding:5px;"><table width="98%" height="487" border="0" cellpadding="0" cellspacing="0">
<tr>
<td height="22" valign="top" class="word_orange">当前位置:图书档案管理 &gt; 添加图书信息 &gt;&gt;&gt;</td>
</tr>
<tr>
<td align="center" valign="top"><table width="100%" height="493" border="0" cellpadding="0" cellspacing="0">
<tr>
  <td align="center" valign="top">
<form name="form1" method="post" action="book_ok.php">
<table width="600" height="432" border="0" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF">
  <tr>
  <td width="173" align="center">条 形 码:</td>
  <td width="427" height="39">
  <input name="barcode" type="text" id="barcode"></td>
  </tr>
  <tr>
  <td align="center">图书名称:</td>
  <td height="39"><input name="bookName" type="text" id="bookName" size="50"> * </td>
  </tr>
  <tr>
  <td align="center">图书类型:</td>
  <td>
<select name="typeId" class="wenbenkuang" id="typeId">
<?php  
  include("Conn/conn.php");
$sql=mysql_query("select * from tb_booktype");
$info=mysql_fetch_array($sql);
do{
?> 
  <option value="<?php echo $info[id];?>"><?php echo $info[typename];?></option>
<?php }while($info=mysql_fetch_array($sql));?>  
  </select>
</td>
  </tr>
  <tr>
  <td align="center">作  者:</td>
  <td><input name="author" type="text" id="author" size="40"></td>
  </tr>
  <tr>
  <td align="center">译  者:</td>
  <td><input name="translator" type="text" id="translator" size="40"></td>
  </tr>
  <tr>
  <td align="center">出 版 社:</td>
  <td>
<select name="isbn" class="wenbenkuang">
<?php
$sql2=mysql_query("select * from tb_publishing");
$info2=mysql_fetch_array($sql2);
do{
?> 
  <option value="<?php echo $info2[ISBN];?>"><?php echo $info2[pubname];?></option>
<?php }while($info2=mysql_fetch_array($sql2));?>  
  </select>
</td>
  </tr>
  <tr>
  <td align="center">价  格:</td>
  <td><input name="price" type="text" id="price">(元)</td>
  </tr>
  <tr>
  <td align="center">页  码:</td>
  <td><input name="page" type="text" id="page"></td>
  </tr>
  <tr>
  <td align="center">书  架:</td>
  <td><select name="bookcaseid" class="wenbenkuang" id="bookcaseid">
<?php
$sql3=mysql_query("select * from tb_bookcase");
$info3=mysql_fetch_array($sql3);
do{
?> 
  <option value="<?php echo $info3[id];?>"><?php echo $info3[name];?></option>
<?php }while($info3=mysql_fetch_array($sql3));?>  
  </select>
  <input name="operator" type="hidden" id="operator" value="<?php echo $info3[name];?>"></td>
  </tr>
  <tr>
  <td align="center"> </td>
  <td><input name="Submit" type="submit" class="btn_grey" value="保存" onClick="return check(form1)"> 
<input name="Submit2" type="button" class="btn_grey" value="返回" onClick="history.back();"></td>
  </tr>
  </table>
</form>
</td>
  </tr>
</table></td>
  </tr>
  </table></td>
  </tr>
</table><?php include("copyright.php");?></td>
  </tr>
</table>
</td>
  </tr>
</table>
</body>
</html>
我的处理页面代码如下:<?php  
session_start();
include("conn/conn.php");
$operator=$_SESSION[admin_name];
$barcode=$_POST[barcode];
$bookName=$_POST[bookName];
$typeid=$_POST[typeId];
$author=$_POST[author];
$translator=$_POST[translator];
$isbn=$_POST[isbn];
$price=$_POST[price];
$page=$_POST[page];
$bookcase=$_POST[bookcaseid];
$inTime=date("Y-m-d");
$sql=mysql_query("insert into tb_bookinfo(barcode,bookName,typeid,author,translator,ISBN,price,page,bookcase,inTime,operator )values(&#39;$barcode&#39;,&#39;$bookName&#39;,&#39;$typeid&#39;,&#39;$author&#39;,&#39;$translator&#39;,&#39;$isbn&#39;,&#39;$price&#39;,&#39;$page&#39;,&#39;$bookcase&#39;,&#39;$inTime&#39;,&#39;$operator&#39;)");
header(&#39;location:book.php&#39;);   
echo "<script language=&#39;javascript&#39;>alert(&#39;图书信息添加成功!&#39;);</script>";
?>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">


------解决方案--------------------
我的处理页面代码如下:session_start(); 
------解决方案--------------------
onclick改为onsubmit ,以及删掉check()的form.submit(),check()只负责验证数据 
------解决方案--------------------
你在check方法里面多写了submit.验证成功后check()提交一次 本身的input type=submit 又提交一次,所以你数据是重复的。

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

How to make PHP applications fasterHow to make PHP applications fasterMay 12, 2025 am 12:12 AM

TomakePHPapplicationsfaster,followthesesteps:1)UseOpcodeCachinglikeOPcachetostoreprecompiledscriptbytecode.2)MinimizeDatabaseQueriesbyusingquerycachingandefficientindexing.3)LeveragePHP7 Featuresforbettercodeefficiency.4)ImplementCachingStrategiessuc

PHP Performance Optimization Checklist: Improve Speed NowPHP Performance Optimization Checklist: Improve Speed NowMay 12, 2025 am 12:07 AM

ToimprovePHPapplicationspeed,followthesesteps:1)EnableopcodecachingwithAPCutoreducescriptexecutiontime.2)ImplementdatabasequerycachingusingPDOtominimizedatabasehits.3)UseHTTP/2tomultiplexrequestsandreduceconnectionoverhead.4)Limitsessionusagebyclosin

PHP Dependency Injection: Improve Code TestabilityPHP Dependency Injection: Improve Code TestabilityMay 12, 2025 am 12:03 AM

Dependency injection (DI) significantly improves the testability of PHP code by explicitly transitive dependencies. 1) DI decoupling classes and specific implementations make testing and maintenance more flexible. 2) Among the three types, the constructor injects explicit expression dependencies to keep the state consistent. 3) Use DI containers to manage complex dependencies to improve code quality and development efficiency.

PHP Performance Optimization: Database Query OptimizationPHP Performance Optimization: Database Query OptimizationMay 12, 2025 am 12:02 AM

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),