搜尋
首頁後端開發php教程 遇见过重复插入数据的请进

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

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 又提交一次,所以你数据是重复的。

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
如何使PHP應用程序更快如何使PHP應用程序更快May 12, 2025 am 12:12 AM

tomakephpapplicationsfaster,關注台詞:1)useopcodeCachingLikeLikeLikeLikeLikePachetoStorePreciledScompiledScriptbyTecode.2)MinimimiedAtabaseSqueriSegrieSqueriSegeriSybysequeryCachingandeffeftExting.3)Leveragephp7 leveragephp7 leveragephp7 leveragephpphp7功能forbettercodeefficy.4)

PHP性能優化清單:立即提高速度PHP性能優化清單:立即提高速度May 12, 2025 am 12:07 AM

到ImprovephPapplicationspeed,關注台詞:1)啟用opcodeCachingwithapCutoredUcescriptexecutiontime.2)實現databasequerycachingingusingpdotominiminimizedatabasehits.3)usehttp/2tomultiplexrequlexrequestsandreduceconnection.4 limitesclection.4.4

PHP依賴注入:提高代碼可檢驗性PHP依賴注入:提高代碼可檢驗性May 12, 2025 am 12:03 AM

依赖注入(DI)通过显式传递依赖关系,显著提升了PHP代码的可测试性。1)DI解耦类与具体实现,使测试和维护更灵活。2)三种类型中,构造函数注入明确表达依赖,保持状态一致。3)使用DI容器管理复杂依赖,提升代码质量和开发效率。

PHP性能優化:數據庫查詢優化PHP性能優化:數據庫查詢優化May 12, 2025 am 12:02 AM

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

簡單指南:帶有PHP腳本的電子郵件發送簡單指南:帶有PHP腳本的電子郵件發送May 12, 2025 am 12:02 AM

phpisusedforsenderemailsduetoitsbuilt-inmail()函數andsupportivelibrariesLikePhpMailerAndSwiftMailer.1)usethemail()functionForbasiceMails,butithasimails.2)butithasimail.2)

PHP性能:識別和修復瓶頸PHP性能:識別和修復瓶頸May 11, 2025 am 12:13 AM

PHP性能瓶颈可以通过以下步骤解决:1)使用Xdebug或Blackfire进行性能分析,找出问题所在;2)优化数据库查询并使用缓存,如APCu;3)使用array_filter等高效函数优化数组操作;4)配置OPcache进行字节码缓存;5)优化前端,如减少HTTP请求和优化图片;6)持续监控和优化性能。通过这些方法,可以显著提升PHP应用的性能。

PHP的依賴注入:快速摘要PHP的依賴注入:快速摘要May 11, 2025 am 12:09 AM

依賴性注射(DI)InphpisadesignPatternthatManages和ReducesClassDeptions,增強量強制性,可驗證性和MATIALWINABIOS.ItallowSpasspassingDepentenciesLikEdenciesLikedAbaseConnectionStoclasseconnectionStoclasseSasasasasareTers,interitationAseTestingEaseTestingEaseTestingEaseTestingEasingAndScalability。

提高PHP性能:緩存策略和技術提高PHP性能:緩存策略和技術May 11, 2025 am 12:08 AM

cachingimprovesphpermenceByStorcyResultSofComputationsorqucrouctationsorquctationsorquickretrieval,reducingServerLoadAndenHancingResponsetimes.feftectivestrategiesinclude:1)opcodecaching,whereStoresCompiledSinmememorytssinmemorytoskipcompliation; 2)datacaching datacachingsingMemccachingmcachingmcachings

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

MantisBT

MantisBT

Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

SublimeText3 英文版

SublimeText3 英文版

推薦:為Win版本,支援程式碼提示!

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

EditPlus 中文破解版

EditPlus 中文破解版

體積小,語法高亮,不支援程式碼提示功能