javascript - updating MYSQL table gives success msg, but does'nt update the table -
the ajax msg gives successful, data doesn't update in db, can plz!
html code:
<div class="row"> <input type="text" ng-model="updateid" class="form-control" placeholder="user id update phone"> <input type="text" ng-model="updatephone" class="form-control" placeholder="user new phone"> </div> <div class="col-xs-3"> </div> <div class="col-xs-2"> <button ng-click="updateuser()" type="button" class="btn btn-primary">update </button> </div> </div>
javascript code:
$scope.updateuser = function () { var data = { updateid: $scope.updateid, updatephone: $scope.updatephone }; $.ajax({ data: data, type: "post", url: "update.php", success: function(data){ alert("data updated"); }, error:function (xmlhttprequest, textstatus, errorthrown) { if (textstatus == 'unauthorized') { alert('custom message. error: ' + errorthrown); } else { alert('custom message. error: ' + errorthrown); } } }); };
update.php code:
<?php header('content-type: application/json'); include 'connect.php'; $db = new database(); $db->setdb_name('training'); $db->connect(); if(isset($_post)){ $id = $_post['updateid']; $phone = $_post['updatephone']; $data = $db->update('user',array('phone'=>$phone),array('id',$id)); echo json_encode($data); } mysql_close(); ?>
the update() function:
public function update($table,$rows,$where) { for($i = 0; $i < count($where); $i++) { if($i%2 != 0) { if(is_string($where[$i])) { if(($i+1) != null) $where[$i] = '"'.$where[$i].'" , '; else $where[$i] = '"'.$where[$i].'"'; } } } $where = implode('=',$where); $update = 'update '.$table.' set '; $keys = array_keys($rows); for($i = 0; $i < count($rows); $i++) { if(is_string($rows[$keys[$i]])) { $update .= $keys[$i].'="'.$rows[$keys[$i]].'"'; } else { $update .= $keys[$i].'='.$rows[$keys[$i]]; } // parse add commas if($i != count($rows)-1) { $update .= ','; } } $update .= ' '.$where; $query = @mysql_query($update); } }
i using angularjs, , when trying run updating in update.php works correctly, using ajax gives "data updated" msg doesnt update table.. why?
first of all, ajax success callback (i'm assuming) jquery means http request succeeded. means got 200 response code. minor , major errors in php request still successful. if want know went wrong, enable error reporting in php , sure errors displayed:
<?php error_reporting(e_all); ini_set('display_errors', 1);
now, should able see errors. use chrome's developer console see error happened in php code. option log error in php , check error log after request.
Comments
Post a Comment