dongyan5706 2017-03-09 09:29
浏览 40
已采纳

PHP删除功能是删除数据库行,但响应显示错误?

I have created a PHP file called DB_Functions which contains my Delete method for removing a database row by User Id. Code:

    //Delete User
public function deleteUser($id){

    $stmt = $this->conn->prepare("DELETE FROM users WHERE user_id = ?");
    $stmt->bind_param("s", $id);
    $result = $stmt->execute();
    $stmt->close();
}

I have then created another PHP file to act as an endpoint which calls this function after the Id is received as a POST parameter. Code:

require_once 'include/DB_Functions.php';
$db = new DB_Functions();

//json response array
$response = array();

if(isset($_POST['id'])){

//recieve GET parameters
$id = $_POST['id'];

$result = $db->deleteUser($id);
if($result){
    $response["error"] = FALSE;
    $response["message"] = "User deleted succesfully";
}else{
    $response["error"] = TRUE;
    $response["error_msg"] = "User did not delete";
}
echo json_encode($response);
}

When testing this using Advanced Rest Client and when using with an Andriod development I am working on, the row is deleted from the database but the parsed response in ARC is the error message and in the Android Logcat the same response message of "User did not delete" is shown?

Any help?

  • 写回答

3条回答 默认 最新

  • dongyanfeng0546 2017-03-09 09:36
    关注

    In your deleteUser function you are missing return statement. If you do not return anything then function will always return null.

    So, In your case it's returning NULL and in further condition check it's going to else case.

    public function deleteUser($id){
    
        $stmt = $this->conn->prepare("DELETE FROM users WHERE user_id = ?");
        $stmt->bind_param("s", $id);
        $result = $stmt->execute();
        $stmt->close();
        return $result;  // add this
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误