使用Ajax使用php将记录插入mysql数据库

时间:2016-12-24 18:16:08

标签: php ajax mysqli

我正在尝试使用模态添加记录。我使用了以下代码,但我的记录没有添加到数据库中..帮助我通过pls

<link rel="stylesheet" type="text/css" href="/Project/Style/bootstrap.css"/>
     <script src="Style/jQuery/jquery-1.11.1.min.js" type="text/javascript"></script>
     </head>
    <body>

    <div id="container">
    <?php
    include('header.php');
    require_once('Connections/conn.php');

    $sql2= "SELECT * from type where cid='1' ";
    $results =mysqli_query($conn,$sql2);


    ?>

    <div id="main">

    <div class="container" >
    <div class="row">
    <div class="form-group col-md-3">

    <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal">
    ++Add Farm Product
    </button>
    </div>
    <div class="input-group col-md-4 col-sm-offset-2">
                        <span class="input-group-addon">Search</span>
                        <input type="text" class="form-control" name="search-text" id="search-text" placeholder="Search By Product Name">
                    </div>
                    </div>

    <div id="result"></div>
    <!-- Modal -->
    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
      <div class="modal-dialog">
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            <h4 class="modal-title" id="myModalLabel">Add Farm Product</h4>
          </div>
          <div class="modal-body">

    <form>
      <div class="row">
           <div class="col-md-6 padding-top-10">
          <label for="proname" class="control-label">Product Name:</label>
      <input type="text" class="form-control" id="proname" placeholder="Product Name" name="proname" />
 </div>
 <div class="col-md-5 padding-top-10">
  <label for="pro-type" class="control-label">Product Type:</label>
   <select class="form-control" id="pro_type" name="pro_type" >
   <option>Select Product type...</option>
    <?php foreach($results as $result): ?>
    <option value= " <?php echo $result['tid'];  ?>" > <?php echo $result['type_name']; ?>
</option>
  <?php endforeach ;?>              
 </select>
 </div>
 </div><br/>

    <div class="row">                       
  <div class="col-md-4 padding-top-10">
  <label for="qty" class="control-label">Quantity:</label>
  <input type="text" class="form-control" id="qty" placeholder="Quantity"  name="qty" />                                                  
   </div>
   <div class="col-md-4 padding-top-10">
   <label for="price" class="control-label">Unit Price :</label>
   <input type="text" class="form-control" id="price" placeholder="Unit Price" name="price" />                                                                         
</div>
 <div class="col-md-4 padding-top-10">
 <label for="pdate" class="control-label"> Production Date :</label>
 <input type="date" class="form-control" id="pdate" placeholder="Production Date" name="pdate" />
  </div>
</div>                           
    </form>
    </div>
   <div class="modal-footer">
 <button type="button" class="btn btn-default" data-dismiss="modal" >Close</button>
  <button type="button" id="add"  name="add"  class="btn btn-primary">Add Product</button>
          </div>
        </div>
      </div>
    </div>  
    </div>
    </div>
    </div>

    </body>
    </html>
    <script>


    // haddling the post for adding record
    $('#add').click(function(){

        var proname = $('#proname').val();
        var pro_type = $('#pro_type').val();
        var qty = $('#qty').val();
        var price = $('#price').val();
        var pdate = $('#pdate').val();

        var datas="proname="+proname+"&pro_type="+pro_type+"&qty="+qty+"&price="+price+"&pdate"+pdate;


    $.ajax({
           type: "POST",
           url: "addpro_exec.php",
           data: datas,
           success:function(data){

        },
         error: function(){

                     }
        });
        });
    </script>

处理发布数据的代码如下 //addpro_exec.php文件

<?php
    require_once('Connections/conn.php');
    if(isset($_POST['add'])){

    $proname=$_POST['proname'];
    $pro_type= $_POST['pro_type'];
    //$typname=$_POST['type_name'];
    $qty=   $_POST['qty'];
    $price= $_POST['price'];
    $pdate= $_POST['pdate'];
    $asdate = \date('F');
        $sql="INSERT into product VALUES('','$proname','','$qty','$price','$pdate','','$pro_type','1') ON DUPLICATE KEY UPDATE qty = qty+$qty";
        $sql2= "INSERT into year_estm VALUES('','$proname','$asdate','$qty','') ON DUPLICATE KEY  update   Production_input=Production_input+$qty ";
    $AddProduction=  mysqli_query($conn, $sql2) or die('Cannot add to production'. mysqli_error($conn));
    $RecAdd= mysqli_query($conn,$sql) or die('Cannot add Product' . mysqli_error());
    if($RecAdd){
        header('location:viewfarm.php');
    }

    }

    else{

    }

    ?>

1 个答案:

答案 0 :(得分:1)

在javascript中尝试以下行:

var datas="add=1&proname="+proname+"&pro_type="+pro_type+"&qty="+qty+"&price="+price+"&pdate="+pdate;

这应该解决问题。

警告:在插入数据库之前,请务必过滤并正确引用用户输入,以避免SQL注入攻击。使用准备好的陈述来帮助实现这一目标。

http://php.net/manual/en/mysqli.quickstart.prepared-statements.php