douhuan1950 2017-10-03 09:15
浏览 28

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

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

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


        }
    ?>
  • 写回答

2条回答 默认 最新

  • dongping1922 2017-10-03 09:19
    关注

    Not entirely sure, but you should check you array like this for entries:

    if (count($data) > 0)
    
    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算