我有这个代码在我的stoc管理脚本中输入产品。 我需要一些帮助。 我想在我输入新产品时检查行鳕鱼(mysql中的codint)是否是唯一的,如果不是唯一警告我。
像opencat模型字段..
<?php
require('header.php');
include('functii/functions.php');
if(isset($_POST[submit]) && !empty($_POST[nume])){
$_POST[nume] = mysql_real_escape_string($_POST[nume]);
$_POST[stoc] = mysql_real_escape_string($_POST[stoc]);
$_POST[pretintrare] = mysql_real_escape_string($_POST[pretintrare]);
$_POST[pretrecomandat] = mysql_real_escape_string($_POST[pretrecomandat]);
$_POST[comentarii] = mysql_real_escape_string($_POST[comentarii]);
$_POST[descriere] = mysql_real_escape_string($_POST[descriere]);
$_POST[locatie] = mysql_real_escape_string($_POST[locatie]);
$_POST[stare] = mysql_real_escape_string($_POST[stare]);
$_POST[tip] = mysql_real_escape_string($_POST[tip]);
$sql = "INSERT INTO stoc (nume, tip, locatie, stoc, pretintrare, pretrecomandat, stare, comentarii, descriere, model1, model2, model3, model4, model5, model6, model7, model8, model9, model10, model11, model12, model13, model14, brand, img, img2, img3, img4, codint) VALUES ('$_POST[nume]', '$_POST[tip]', '$_POST[locatie]', '$_POST[stoc]', '$_POST[pretintrare]', '$_POST[pretrecomandat]', '$_POST[stare]', '$_POST[comentarii]', '$_POST[descriere]', '$_POST[model1]', '$_POST[model2]', '$_POST[model3]', '$_POST[model4]', '$_POST[model5]', '$_POST[model6]', '$_POST[model7]', '$_POST[model8]', '$_POST[model9]', '$_POST[model10]', '$_POST[model11]', '$_POST[model12]', '$_POST[model13]', '$_POST[model14]', '$_POST[brand]', '$_POST[img]', '$_POST[img2]', '$_POST[img3]', '$_POST[img4]', '$_POST[codint]' )";
$result=mysql_query($sql);
$last_id=mysql_insert_id();
die('<script type="text/javascript">window.location.href="stoc-id.php?id='.$last_id.'";</script>');
}
?>
<ul id="nav">
<li class="inactive"><a href="stoc.php?t=ecran">Ecrane</a></li>
<li class="inactive"><a href="stoc.php?t=tastatura">Tastaturi</a></li>
<li class="inactive"><a href="stoc.php?t=incarcator">Incarcatoare</a></li>
<li class="inactive"><a href="stoc.php?t=hdd">HDD</a></li>
<li class="inactive"><a href="stoc.php?t=placibaza">Placi de baza</a></li>
<li class="inactive"><a href="stoc.php?t=ram">RAM</a></li>
<li class="inactive"><a href="stoc.php?t=Cooler">Cooler</a></li>
<li class="inactive"><a href="stoc.php?t=balamale">Balamale</a></li>
<li class="inactive"><a href="stoc.php?t=laptop">Laptop</a></li>
<li class="inactive"><a href="stoc.php?t=accesoriil">Accesorii Laptop</a></li>
<li class="inactive"><a href="stoc.php?t=mufealimentare">Mufe Alimentare</a></li>
<li class="inactive"><a href="stoc.php?t=moduleel">Module Electronice</a></li>
<li class="inactive"><a href="stoc.php?t=carcasa">Carcasa laptop</a></li>
<li class="inactive"><a href="stoc.php?t=dez">Dezmembrari</a></li>
</ul>
<div id="content">
<h1>Adauga un produs pe stoc:</h1>
<form action="stoc-adauga.php" method="post">
<div id="tichetleft">
<p>Nume: <input style="margin-left: 40px;" type="text" name="nume"></p>
<p>Cod: <input style="margin-left: 52px;" type="text" name="codint"></p>
<p>Stoc: <input style="margin-left: 51px;" type="text" name="stoc"/></p>
<p>Pret recom: <input style="margin-left: -1px;" type="text" name="pretintrare"/></p>
</div>
<div id="tichetright">
<p>Locatie:
<input style="margin-left: 33px;" type="radio" id="1" name="locatie" value="0"><label for="1">Crangasi</label>
<input type="radio" name="locatie" id="2" value="1" checked="checked"><label for="2">Dristor</label>
</p>
<p style="margin-top: 27px;">Stare:
<input style="margin-left: 48px;" id="3" type="radio" name="stare" value="0" checked="checked"><label for="3">Nou</label>
<input type="radio" name="stare" id="4" value="1"><label for="4">Second-Hand</label>
</p>
<p style="margin-top: 23px;">Pret Distributie: <input style="margin-left: 13px;width:262px;" type="text" name="pretrecomandat"/></p>
</div>
<div class="clear"></div>
<div id="center">
<p class="stoc">Tip:
<input type="radio" id="5" name="tip" value="ecran" checked="checked"><label for="5">Ecran</label>
<input type="radio" id="6" name="tip" value="tastatura"><label for="6">Tastatura</label>
<input type="radio" id="7" name="tip" value="incarcator"><label for="7">Incarcator</label>
<input type="radio" id="8" name="tip" value="hdd"><label for="8">HDD</label>
<input type="radio" id="9" name="tip" value="placibaza"><label for="9">Placa de baza</label>
<input type="radio" id="10" name="tip" value="ram"><label for="10">RAM</label>
<input type="radio" id="12" name="tip" value="cooler"><label for="12">Cooler</label>
<input type="radio" id="13" name="tip" value="balamale"><label for="13">Balamale</label>
<input type="radio" id="11" name="tip" value="laptop"><label for="11">Laptop</label>
<input type="radio" id="14" name="tip" value="accesoriil"><label for="14">Accesorii Laptop</label>
<input type="radio" id="15" name="tip" value="mufealimentare"><label for="15">Mufe Alimentare</label>
<input type="radio" id="16" name="tip" value="moduleel"><label for="16">Module Electronice</label>
<input type="radio" id="17" name="tip" value="carcasa"><label for="17">Carcasa</label>
<input type="radio" id="18" name="tip" value="dez"><label for="18">Dezmembrari</label>
</p>
<p>Note int: <textarea style="margin-left: 45px;" type="text" name="comentarii"/></textarea></p>
<p>Descriere: <textarea style="margin-left: 45px;" type="text" name="descriere"/></textarea></p>
</div>
<div id="tichetleft">
<p>Model1: <input style="margin-left: 40px;" type="text" name="model1"></p>
<p>Model2: <input style="margin-left: 40px;" type="text" name="model2"></p>
<p>Model3: <input style="margin-left: 40px;" type="text" name="model3"></p>
<p>Model4: <input style="margin-left: 40px;" type="text" name="model4"></p>
<p>Model5: <input style="margin-left: 40px;" type="text" name="model5"></p>
<p>Model5: <input style="margin-left: 40px;" type="text" name="model11"></p>
<p>Model5: <input style="margin-left: 40px;" type="text" name="model12"></p>
<p>Brand: <input style="margin-left: 40px;" type="text" name="brand"></p>
<p>IMG3: <input style="margin-left: 40px;" type="text" name="img3"></p>
</div>
<div id="tichetright">
<p>Model6: <input style="margin-left: 40px;" type="text" name="model6"></p>
<p>Model7: <input style="margin-left: 40px;" type="text" name="model7"></p>
<p>Model8: <input style="margin-left: 40px;" type="text" name="model8"></p>
<p>Model9: <input style="margin-left: 40px;" type="text" name="model9"></p>
<p>Model0: <input style="margin-left: 40px;" type="text" name="model10"></p>
<p>Model0: <input style="margin-left: 40px;" type="text" name="model13"></p>
<p>Model0: <input style="margin-left: 40px;" type="text" name="model14"></p>
<p>IMG: <input style="margin-left: 40px;" type="text" name="img"></p>
<p>IMG2: <input style="margin-left: 40px;" type="text" name="img2"></p>
<p>IMG4: <input style="margin-left: 40px;" type="text" name="img4"></p>
</div>
<div id="center">
<input type="submit" style="padding-left: 25px;" name="submit" value="Adauga produsul"><img style="vertical-align: -10%;margin-left:-160px;" src="imagini/produs-icon.png" alt="Adauga tichet"></input>
</div>
</form>
</div> <!-- end content -->
<?php require('footer.php');?>
答案 0 :(得分:0)
要检查记录是否存在于DB中,
您首先获得指定codint的计数。对于下面给出的代码。
$checkStmt=mysqli_prepare($con, "SELECT COUNT(*) FROM stoc WHERE codint<>?");
if( $checkStmt ) {
$codint = mysqli_real_escape_string($con, $_POST[codint]);
mysqli_stmt_bind_param($checkStmt,"d",$codint);
mysqli_stmt_execute($checkStmt);
mysqli_stmt_bind_result($checkStmt,$txt_RecordCount);
mysqli_stmt_fetch($checkStmt);
mysqli_stmt_close($checkStmt);
$knownRecordCount=$txt_RecordCount;
}
if($txt_RecordCount>0){
// record id existing
}else{
// not existing. It is a fresh recored
}
请在您的代码中理解并实施。这会奏效。这完全适用于我的情况。