title: Ajax datatable Update with PHP MySql
body: <p>I want to update data using Ajax and below is the sample code. The SweetAlert get displaced that it has been updated but it doesn't take effect in the database non thus it gives an error.</p>
  <h3>Ajax Code</h3>
  <p>This is the Ajax Script for the form submission when the submit button is clicked.</p>
  <pre><code>&lt;script&gt;
                 $('#submit').on('click', function(event){
                  event.preventDefault();
                  var firstname = $('#firstname').val();
                  var othername = $('#othername').val();
                  var gender = $('#gender').val();
                  var id_type = $('#id_type').val();
                  var id_number = $('#id_number').val();
                  var issue_date = $('#issue_date').val();
                  var business_place = $('#business_place').val();
                  var food_type = $('#food_type').val();
                  var screened = $('#screened').val();
                  var sub_metro = $('#sub_metro').val();
                  var telephone = $('#telephone').val();
                  var get_date = $('#get_date').val();
                  var chit_number = $('#chit_number').val();
                  var remarks = $('#remarks').val();
                  var user_id = $('#user_id').val();
                  var vendor_id = $('#vendor_id').val();
  
  if (firstname!=&quot;&quot; &amp;&amp;othername!=&quot;&quot; &amp;&amp; food_type!=&quot;&quot;){
              $.ajax({
                  url: &quot;action/vendor_update.php&quot;,
                  type: &quot;POST&quot;,
                  data: {
                      firstname:firstname, othername:othername, gender:gender, 
  id_type:id_type, id_number:id_number, issue_date:issue_date,    
  business_place:business_place, food_type:food_type, screened:screened, 
  sub_metro:sub_metro, telephone:telephone, get_date:get_date,
                      chit_number:chit_number, remarks:remarks, user_id:user_id, vendor_id:vendor_id,
                  },
                  cache: false,
                  success: function(data){
                      if(data.statusCode=200){
                          $('#dataForm').find('input:text').val('');
                              alert('Updated');
                              })      
                       }
                      else if (data.statusCode=201)
                       {
                              alert('Cannot Update');
                       }
                  }
              });
          
          }else{
              alert('All fields are mandatory');
             }
      });
  </code></pre>
  <h3>PHP Code ---&gt; action/vendor_update.php</h3>
  <p>This code is for the php server side for data insertion into the database.</p>
  <pre><code>&lt;?
          session_start();
          // include('../includes/session.php');
          include('./includes/connection.php');
          
          $query = $dbh-&gt;$prepare(&quot;UPDATE vendors SET Firstname=:firstname, Othername=:othername, Telephone=:telephone, Gender=:gender, IdType=:id_type, IdNumber=:id_number, IdDate=:issue_date, BusinessPlace=:business_place, FoodType=:food_type, Notes=:remarks, ScreenStatus=:screened, ChitNumber=:chit_number, UpdatedBy=:user_id WHERE VendorId=:vendor_id&quot;);
  
          $query-&gt;execute([
                  $firstname = $_POST['firstname'];
                  $othername = $_POST['othername'];
                  $telephone = $_POST['telephone'];
                  $gender = $_POST['gender'];
                  $id_type = $_POST['id_type'];
                  $id_number = $_POST['id_number'];
                  $issue_date = $_POST['issue_date'];
                  $business_place = $_POST['business_place'];
                  $food_type = $_POST['food_type'];
                  $remarks = $_POST['remarks'];
                  $screened = $_POST['screened'];
                  $chit_number = $_POST['chit_number'];
                  $user_id =  $_POST['user_id'];
                  $vendor_id = $_POST['vendor_id'];
            ]);
                  
          // $query-&gt;execute($_POST);
  
  if ($query-&gt;execute()){
     echo json_encode(array(&quot;statusCode&quot;=&gt;200));
     
  } else {
     echo json_encode(array(&quot;statusCode&quot;=&gt;500));
  }
  
                  
  
  ?&gt;
  </code></pre>