PHP按下按钮时更新数据库中文本输入的值

时间:2018-03-24 05:10:12

标签: javascript php jquery html ajax

我将解释到目前为止我已实现的内容: - 这是数据库中的表,即adhar_card。有关结构,请参见下图: - (Adhard_card_id是主键)

enter image description here

当我们点击验证按钮时,在adhar_card表中的字段" status"将改为2。类似的当我们点击拒绝按钮时,状态将变为5.我的代码中唯一缺少的部分是输入字段的功能(有效期)。用户应该能够输入到期日期以及当他们按下验证按钮时...状态应该在DB(Adharcard)中变为2(已经被充实)并将输入值填入" expiry_Date" DB(Adharcard)中的字段(未实现)。我怎么解决这个问题 。?此代码可能会导致sql注入或此代码可能容易受到攻击,但由于我们仅将此代码用于localhost ..我认为这不是问题,我们将在将其上传到服务器时查看漏洞。现在我想要要在DB.Status列中更新的输入值正在更新,但输入列未更新。

以下是 Html 部分(包含输入文字字段和验证按钮): -

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

这是activate函数(当按下按钮时它调用ajax请求的位置): - (当我们警告expiry_date获取输入框中输入的值时)

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+")");
}
}
});  
}
}

PHP 代码更新值(此处将状态更新为2但未更新到期日期值): -

<?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);


?>

2 个答案:

答案 0 :(得分:1)

由于到期日期为日期字段,您需要更新0x0000附带的值。因此,请更改您的查询,如下所示,然后尝试更新日期。

''

同样在你的jS到期日出错了。将您的数据更改为

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

答案 1 :(得分:1)

虽然您的代码已经有效但您在下一行expiry_date只是一个问题,请使用"expiry_date":expiry_date代替expiry_date:"expiry_date"

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

我需要在此处提及的其他内容..我不相信ajax成功函数可以从php读取真或假...所以你可能需要使用$res['error'] = "false";和js {{1}与真实相同的事情

相关问题