简体   繁体   English

相同的条形码意外插入我的数据库

[英]The same barcode is unexpectedly inserting into my database

This is a code for inserting the barcode into a database but if the same barcode exists in the database, I don't need this to be inserted same again, so I coded like this but still, the same barcode is inserting in my database. 这是用于将条形码插入数据库的代码,但是如果数据库中存在相同的条形码,则不需要再次插入相同的条形码,因此我进行了这样的编码,但仍然在数据库中插入了相同的条形码。 What the error in this code? 这段代码有什么错误?

I am using mysqli and Bootstrap to show the errors in the page, the page is executed from another page called product.php via ajax and it is showing in another page called showuser.php as it gets updated in a table which should give the output 我正在使用mysqli和Bootstrap在页面中显示错误,该页面是通过ajax从另一个名为product.php的页面执行的,它在另一个名为showuser.php的页面中显示,因为它在要提供输出的表中进行了更新

<?php
        include('conn.php');

        if(isset($_POST['add'])){
            $proname=$_POST['product'];
            $bar=$_POST['barcode'];
            $war=$_POST['warranty'];
            $gar=$_POST['guarranty'];
            $amount=$_POST['amount'];
            $id = $_POST['cusid'];
            $date = $_POST['date'];

            $check = "SELECT * FROM warranty_update WHERE battery_serial = '$bar'";
            $rs = mysqli_query($con,$check);
    $data = mysqli_fetch_array($rs, MYSQLI_NUM);
    if($data[0] > 1) {

    ?>
        <br>
    <br>
    <br>
    <br>
    <br>
    <br>
    <br>
    <br>
    <br>


        <h3 align="center"><i class="   glyphicon glyphicon-warning-sign"></i><br>
    <br>
      Sorry Barcode Already Been Inserted !<br><br>

    Please Go Back</h3>
    <?php } else {

         $datesplit     = explode("-",$date);
         $warranty      = $datesplit[0];
         $guarante      = $datesplit[0];


         $day    = $datesplit[1];
         $month  = $datesplit[2];

          $fdate =  strtotime($date);
          $Gm=strtotime("+$gar Months",$fdate);
          $wm=strtotime("+$war Months",$Gm);
            $m1=date("Y-m-d", $Gm) ;
            $m2 =date("Y-m-d", $wm) ;

         $today =  date("Y/m/d");


          $expiry_warranty = $m2;
          $expiry_guarante = $m1;

        //mysql_query("INSERT INTO `cus_reg`(`reg_id`, `name`, `mobile`, `battery_serial`, `model`, `date`, `reg_by`, `warranty_expiry`, `waranty_span`,`guarantee_expiry`, `g_span`) VALUES (NULL,'$name','$mobile','$battery_serial','$vehicle_num','$date','$regby','$expiry_warranty', '$wmonth', '$expiry_guarante', '$gmonth')")==true or die(mysql_error);


            //mysql_query("INSERT INTO `warranty_update`(`wa_id`, `cus_reg_id`, `new_barcode`, `date`) VALUES (NULL,LAST_INSERT_ID(),'$battery_serial','$today')");

    mysqli_query($conn,"INSERT INTO `warranty_update`(`wa_id`, `reg_id`, `battery_serial`, `model`, `date`, `warranty_expiry`, `waranty_span`, `guarantee_expiry`, `g_span`, `amount`) VALUES (NULL,'$id','$bar','$proname','$today','$expiry_warranty','$war','$expiry_guarante','$gar','$amount')");  
    mysqli_query($conn,"INSERT INTO `replacements`(`r_id`, `reg_id`, `wa_id`, `new_barcode`, `date`) VALUES ('NULL','$id',LAST_INSERT_ID(),'$bar','$today')");  
       ?>


       <!--header("refresh:1;url=home.php");-->

         <h3 align="center"><i class="  glyphicon glyphicon-warning-sign"></i><br>
    <br>
      Data Successfully Inserted !  


    <?php }


        }
    ?>

不确定,但是您应该像这样检查数组以获取条目:

if (count($data) > 0)

The best way to do this is defining a unique constraint on barcode via SQL. 最好的方法是通过SQL在条形码上定义唯一约束。 Anyway I think that you are valuating the condition in an erroneous mode. 无论如何,我认为您是在错误模式下评估条件。 $data is an array, so you should check his size with: sizeof. $ data是一个数组,因此您应该使用:sizeof检查其大小。

so: if(sizeof($data) > 1) { .... } 因此: if(sizeof($data) > 1) { .... }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM