PHP代码返回错误代码500.什么错了?

时间:2016-07-18 00:42:33

标签: php android database sql-update android-volley

我最近制作了一个PHP文件来更新数据库。我在Android应用程序的StringRequest中使用此文件。请注意,我知道错误代码500是服务器错误。我的问题是我无法弄清楚代码有什么问题。有人可以帮我理解发生了什么吗?

我的代码:

<?php

    $username = $_POST["username"];
    $location = $_POST["location"];
    $lastDayName = $_POST["lastDayName"];

    $response = array();
    $response["success"] = false;

    $con = mysqli_connect("website.com", "username", "password", "dbname");

    switch($location) {
        case "1":
            $update_statement = mysqli_prepare($con, "UPDATE location SET lastDayName1=? WHERE username=?");
            mysqli_stmt_bind_param($update_statement, "ss", $lastDayName, $username);
            mysqli_stmt_execute($update_statement);
            mysqli_stmt_close($update_statement);

            $update_statement = mysqli_prepare($con, "UPDATE location SET lastDayName2=? WHERE username=?");
            mysqli_stmt_bind_param($update_statement, "ss", "", $username);
            mysqli_stmt_execute($update_statement);
            mysqli_stmt_close($update_statement);
            $response["success"] = true;
            break;
        case "2":
            $update_statement = mysqli_prepare($con, "UPDATE location SET lastDayName2=? WHERE username=?");
            mysqli_stmt_bind_param($update_statement, "ss", $lastDayName, $username);
            mysqli_stmt_execute($update_statement);
            mysqli_stmt_close($update_statement);

            $update_statement = mysqli_prepare($con, "UPDATE location SET lastDayName3=? WHERE username=?");
            mysqli_stmt_bind_param($update_statement, "ss", "", $username);
            mysqli_stmt_execute($update_statement);
            mysqli_stmt_close($update_statement);
            $response["success"] = true;
            break;
        case "3":
            $update_statement = mysqli_prepare($con, "UPDATE location SET lastDayName3=? WHERE username=?");
            mysqli_stmt_bind_param($update_statement, "ss", $lastDayName, $username);
            mysqli_stmt_execute($update_statement);
            mysqli_stmt_close($update_statement);

            $update_statement = mysqli_prepare($con, "UPDATE location SET lastDayName4=? WHERE username=?");
            mysqli_stmt_bind_param($update_statement, "ss", "", $username);
            mysqli_stmt_execute($update_statement);
            mysqli_stmt_close($update_statement);
            $response["success"] = true;
            break;
        case "4":
            $update_statement = mysqli_prepare($con, "UPDATE location SET lastDayName4=? WHERE username=?");
            mysqli_stmt_bind_param($update_statement, "ss", $lastDayName, $username);
            mysqli_stmt_execute($update_statement);
            mysqli_stmt_close($update_statement);

            $update_statement = mysqli_prepare($con, "UPDATE location SET lastDayName5=? WHERE username=?");
            mysqli_stmt_bind_param($update_statement, "ss", "", $username);
            mysqli_stmt_execute($update_statement);
            mysqli_stmt_close($update_statement);
            $response["success"] = true;
            break;
        case "5":
            $update_statement = mysqli_prepare($con, "UPDATE location SET lastDayName5=? WHERE username=?");
            mysqli_stmt_bind_param($update_statement, "ss", $lastDayName, $username);
            mysqli_stmt_execute($update_statement);
            mysqli_stmt_close($update_statement);

            $update_statement = mysqli_prepare($con, "UPDATE location SET lastDayName6=? WHERE username=?");
            mysqli_stmt_bind_param($update_statement, "ss", "", $username);
            mysqli_stmt_execute($update_statement);
            mysqli_stmt_close($update_statement);
            $response["success"] = true;
            break;
        case "6":
            $update_statement = mysqli_prepare($con, "UPDATE location SET lastDayName6=? WHERE username=?");
            mysqli_stmt_bind_param($update_statement, "ss", $lastDayName, $username);
            mysqli_stmt_execute($update_statement);
            mysqli_stmt_close($update_statement);

            $update_statement = mysqli_prepare($con, "UPDATE location SET lastDayName7=? WHERE username=?");
            mysqli_stmt_bind_param($update_statement, "ss", "", $username);
            mysqli_stmt_execute($update_statement);
            mysqli_stmt_close($update_statement);
            $response["success"] = true;
            break;
        case "7": /* I don't think this one will be used because it is the last location anyway */
            $update_statement = mysqli_prepare($con, "UPDATE location SET lastDayName7=? WHERE username=?");
            mysqli_stmt_bind_param($update_statement, "ss", $lastDayName, $username);
            mysqli_stmt_execute($update_statement);
            mysqli_stmt_close($update_statement);
            $response["success"] = true;
            break;
        default:
            $response["success"] = false;
            break;
    }

    $con = null;
    echo json_encode($response);
?>

1 个答案:

答案 0 :(得分:1)

试试这个:

<?php

    $username = $_POST["username"];
    $location = $_POST["location"];
    $lastDayName = $_POST["lastDayName"];

    $response = array();
    $response["success"] = false;

    $con = mysqli_connect("website.com", "username", "password", "dbname");

    $null_value = "";

    switch($location) {
        case "1":
            $update_statement = mysqli_prepare($con, "UPDATE location SET lastDayName1=? WHERE username=?");
            mysqli_stmt_bind_param($update_statement, "ss", $lastDayName, $username);
            mysqli_stmt_execute($update_statement);
            mysqli_stmt_close($update_statement);

            $update_statement = mysqli_prepare($con, "UPDATE location SET lastDayName2=? WHERE username=?");
            mysqli_stmt_bind_param($update_statement, "ss", $null_value, $username);
            mysqli_stmt_execute($update_statement);
            mysqli_stmt_close($update_statement);
            $response["success"] = true;
            break;
        case "2":
            $update_statement = mysqli_prepare($con, "UPDATE location SET lastDayName2=? WHERE username=?");
            mysqli_stmt_bind_param($update_statement, "ss", $lastDayName, $username);
            mysqli_stmt_execute($update_statement);
            mysqli_stmt_close($update_statement);

            $update_statement = mysqli_prepare($con, "UPDATE location SET lastDayName3=? WHERE username=?");
            mysqli_stmt_bind_param($update_statement, "ss", $null_value, $username);
            mysqli_stmt_execute($update_statement);
            mysqli_stmt_close($update_statement);
            $response["success"] = true;
            break;
        case "3":
            $update_statement = mysqli_prepare($con, "UPDATE location SET lastDayName3=? WHERE username=?");
            mysqli_stmt_bind_param($update_statement, "ss", $lastDayName, $username);
            mysqli_stmt_execute($update_statement);
            mysqli_stmt_close($update_statement);

            $update_statement = mysqli_prepare($con, "UPDATE location SET lastDayName4=? WHERE username=?");
            mysqli_stmt_bind_param($update_statement, "ss", $null_value, $username);
            mysqli_stmt_execute($update_statement);
            mysqli_stmt_close($update_statement);
            $response["success"] = true;
            break;
        case "4":
            $update_statement = mysqli_prepare($con, "UPDATE location SET lastDayName4=? WHERE username=?");
            mysqli_stmt_bind_param($update_statement, "ss", $lastDayName, $username);
            mysqli_stmt_execute($update_statement);
            mysqli_stmt_close($update_statement);

            $update_statement = mysqli_prepare($con, "UPDATE location SET lastDayName5=? WHERE username=?");
            mysqli_stmt_bind_param($update_statement, "ss", $null_value, $username);
            mysqli_stmt_execute($update_statement);
            mysqli_stmt_close($update_statement);
            $response["success"] = true;
            break;
        case "5":
            $update_statement = mysqli_prepare($con, "UPDATE location SET lastDayName5=? WHERE username=?");
            mysqli_stmt_bind_param($update_statement, "ss", $lastDayName, $username);
            mysqli_stmt_execute($update_statement);
            mysqli_stmt_close($update_statement);

            $update_statement = mysqli_prepare($con, "UPDATE location SET lastDayName6=? WHERE username=?");
            mysqli_stmt_bind_param($update_statement, "ss", $null_value, $username);
            mysqli_stmt_execute($update_statement);
            mysqli_stmt_close($update_statement);
            $response["success"] = true;
            break;
        case "6":
            $update_statement = mysqli_prepare($con, "UPDATE location SET lastDayName6=? WHERE username=?");
            mysqli_stmt_bind_param($update_statement, "ss", $lastDayName, $username);
            mysqli_stmt_execute($update_statement);
            mysqli_stmt_close($update_statement);

            $update_statement = mysqli_prepare($con, "UPDATE location SET lastDayName7=? WHERE username=?");
            mysqli_stmt_bind_param($update_statement, "ss", $null_value, $username);
            mysqli_stmt_execute($update_statement);
            mysqli_stmt_close($update_statement);
            $response["success"] = true;
            break;
        case "7": /* I don't think this one will be used because it is the last location anyway */
            $update_statement = mysqli_prepare($con, "UPDATE location SET lastDayName7=? WHERE username=?");
            mysqli_stmt_bind_param($update_statement, "ss", $lastDayName, $username);
            mysqli_stmt_execute($update_statement);
            mysqli_stmt_close($update_statement);
            $response["success"] = true;
            break;
        default:
            $response["success"] = false;
            break;
    }

    $con = null;
    echo json_encode($response);
?>