2016-12-24 109 views
-1

我正在嘗試使用模式添加記錄。我已經使用下面的代碼,但我的記錄不會被添加到數據庫中..幫我通過請使用Ajax將記錄插入到mysql數據庫中

<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{ 

    } 

    ?> 
+0

這很難回答,因爲您沒有顯示您嘗試調試的內容。有沒有錯誤?你知道AJAX調用是否有效嗎?你有沒有檢查你的控制檯?如果你不使用AJAX直接發佈到url addpro_exec.php會發生什麼呢?它會工作嗎? –

+0

看看這一行,'if(isset($ _ POST ['add'])){...'。 '$ _POST ['add']'沒有設置,那是因爲你沒有在你的AJAX請求中發送任何東西。此外,缺少'='在這裏,'...「&pdate」+ pdate;' –

+0

沒有ajax,它工作得很好,..我第一次學習ajax @cale_b – Othman

回答

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

+0

非常感謝。..它已經正常工作@ mim.ms – Othman

+0

強調使用預準備語句 - 這對防止攻擊非常重要! –

相關問題