Shabarish Shetty
Shabarish Shetty

Reputation: 132

PHP Update the value of text input in database when a button is pressed

I will explain what i have implemented so far :- This is the table in database ie adhar_card . See the image below for structure:- (Adhard_card_id is the primary key )

enter image description here

When we click on the verify button , in adhar_card table the field "status " will change to 2 . Similary when we click on reject button the status will change to 5. The only part that is missing in my code is functionlity for input field (expiry date) . User should be able to enter the expiry date and when they press the verify button.. the status should change to 2 in DB(Adharcard)(Already implimented) and fill the input value to "expiry_Date" field in DB(Adharcard) (not implemented). How can i solve this problem .? This code might cause sql injection or this code might be vulnerable , but since we are using this code only for localhost .. i think its not an issue and we will look at the vulnerability when we upload it to server.For now i want the input value to be updated in DB.Status column is getting updated but input column is not getting updated .

Here is the Html part (Contains input text field and verify button ) : -

 <div class="form-group">
   <div class="input-group">
      <div class="form-line">
         <input type="text" class="form-control date" id="expiry_date" placeholder="Add Aadhar Number ">
      </div>
   </div>
   <button id="adhar_card_button" type="button"   class="btn btn-primary btn-lg text-center">sdasd</button>
</div>

Here is the activate function ( Where it calls the ajax request when button is pressed ) :- ( When we alert the expiry_date am getting the value which is entered in input box)

function activate(tablename,idName,idValue,expiry_date){
var expiry_date = $("#expiry_date").val();
if(expiry_date != null && expiry_date != ""){
$.ajax({    
type: 'POST',  
url: 'verify_single_doc.php',   
dataType: 'JSON' ,
data: {"tablename":tablename,"idName":idName,"idValue":idValue,expiry_date:"expiry_date"},  
success: function(response)  
{ 
if(response.error == false){
//alert('in');
alert(expiry_date);
$("#"+response.tablename+"_button").html('Reject.');
$("#"+response.tablename+"_button").attr("class","btn btn-danger");
$("#"+response.tablename+"_button").attr("onclick","deactivate("+response.tablename+","+response.idName+","+response.idValue+")");
}
}
});  
}
}

And PHP code to update the values ( Here its updating the status to 2 but not updating the expiry date value ):-

<?php

require('connect.php');
$res = array();
if( $_REQUEST['tablename'] != null  &&
    $_REQUEST['idName'] != null  &&
    $_REQUEST['idValue'] != null &&
    $_REQUEST['expiry_date'] !=null 

){

        $tablename = $_REQUEST['tablename'] ;
        $idName = $_REQUEST['idName'] ;
        $idValue = $_REQUEST['idValue'] ;
        $expiry_date = $_REQUEST['expiry_date'];    

        if (mysqli_query($conn,"UPDATE $tablename SET status = 2 , expiry_date = $expiry_date WHERE $idName = $idValue" ) ){

                $res['error'] = false;
                $res['message'] = "Verified.";
                $res['tablename'] = $tablename;
                $res['idName'] = $idName;
                $res['idValue'] = $idValue;
                $res['expiry_date'] = $expiry_date;
        }else{
            $res['error'] = true;
            $res['message'] = "Try again later.";
        } 
}else{
$res['error'] = true;
$res['message'] = "Fields are missing.";
}

echo json_encode($res);


?>

Upvotes: 0

Views: 103

Answers (2)

Mohamed-Yousef
Mohamed-Yousef

Reputation: 24001

While you'r code already work .. you've just a problem with expiry_date in the next line use "expiry_date":expiry_date instead of expiry_date:"expiry_date"

data: {"tablename":tablename,"idName":idName,"idValue":idValue,"expiry_date":expiry_date},

Something else I need to mention here .. I don't believe that ajax success function can read true or false from php .. so you may need to use $res['error'] = "false"; and on js if(response.error == "false"){ same thing with true

Upvotes: 1

B. Desai
B. Desai

Reputation: 16436

As expiry date will be date field, You need to update value enclosed with ''. So change your query like below then try to update date.

mysqli_query($conn,"UPDATE $tablename SET status = 2 , expiry_date = '$expiry_date' WHERE $idName = $idValue" );

Also in your jS expiry date goes wrong. Change your data to

data: {"tablename":tablename,"idName":idName,"idValue":idValue,"expiry_date":expiry_date},

Upvotes: 1

Related Questions