Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP检查是否唯一_Php_Mysql_Unique Id - Fatal编程技术网

PHP检查是否唯一

PHP检查是否唯一,php,mysql,unique-id,Php,Mysql,Unique Id,我有这个代码在我的stoc管理脚本中输入产品。 我需要一些帮助。 当我输入新产品时,我想检查行cod(mysql中的codint)是否唯一,如果不唯一,请通知我 像opencat模型字段 <?php require('header.php'); include('functii/functions.php'); if(isset($_POST[submit]) && !empty($_POST[nume])){ $_POST[nume] = mysql_real

我有这个代码在我的stoc管理脚本中输入产品。 我需要一些帮助。 当我输入新产品时,我想检查行cod(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');?>

检查记录是否存在于数据库中

首先获取指定的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
    }
$checkStmt=mysqli_prepare($con,“从stoc WHERE codint?”)中选择计数(*);
如果($checkStmt){
$codint=mysqli\u real\u escape\u字符串($con,$\u 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\u RecordCount;
}
如果($txt_RecordCount>0){
//记录id存在
}否则{
//不存在。这是一个新记录
}

请理解并在代码中实现。这会奏效的。这在我的情况下非常有效。

每次在新代码中使用数据库扩展时,它都会被弃用,并且在PHP7中已经存在多年,并且永远消失。如果你只是在学习PHP,那么就把精力花在学习
PDO
mysqli
数据库扩展和准备好的语句上。您的脚本可能会出现以下风险:查看发生了什么,甚至使用请阅读,以及如何创建一个非免费编码、代码转换、调试、教程或库查找服务,我们将修复您的尝试,我们不会在插入之前编写代码,您应该执行此操作,并且计数值为零,然后插入,否则执行其他操作。