-2

I have this code to enter product in my stoc administration script. I need some help. I want when i enter new product to check if row cod (codint in mysql ) is unique and if is not unique alert me.

Like opencat model field ..

<?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');?>
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Velicu Cristian
  • 27
  • 1
  • 10
  • 5
    Every time you use [the `mysql_`](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) database extension in new code **[a Kitten is strangled somewhere in the world](http://2.bp.blogspot.com/-zCT6jizimfI/UjJ5UTb_BeI/AAAAAAAACgg/AS6XCd6aNdg/s1600/luna_getting_strangled.jpg)** it is deprecated and has been for years and is gone for ever in PHP7. If you are just learning PHP, spend your energies learning the `PDO` or `mysqli` database extensions and prepared statements. [Start here](http://php.net/manual/en/book.pdo.php) – RiggsFolly Jan 23 '17 at 09:41
  • 3
    Your script is at risk of [SQL Injection Attack](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) Have a look at what happened to [Little Bobby Tables](http://bobby-tables.com/) Even [if you are escaping inputs, its not safe!](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) Use [prepared parameterized statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) – RiggsFolly Jan 23 '17 at 09:42
  • 3
    Please read [What topics can I ask about](http://stackoverflow.com/help/on-topic) and [How to ask a good question](http://stackoverflow.com/help/how-to-ask) and [the perfect question](http://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/) and how to create a [Minimal, Complete and Verifiable example](http://stackoverflow.com/help/mcve) SO is **not a free Coding or Code Conversion or Debugging or Tutorial or Library Finding service** ___We fix your attempts, we do not write your code___ – RiggsFolly Jan 23 '17 at 09:43

1 Answers1

0

To check the record is existing in DB or not,

You first get the count of the specified codint. For that code like below given.

$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
    }

Please understand and implement in your code. This will work. This is perfectly works in my case.

Pranav MS
  • 2,235
  • 2
  • 23
  • 50
  • Before the insertion you should perform this and the count value is zero then insert otherwise perform some other things. – Pranav MS Jan 24 '17 at 02:53