TheAnonymous010 TheAnonymous010 - 4 months ago 12
Android Question

PHP Code Returns Error Code 500. What's Wrong?

I recently made a PHP file to update a database. I use this file in a StringRequest in an android app. Please note that I know Error Code 500 is a server error. My problem is that I cannot figure out what's wrong with the code. Can someone help me understand what's going on?

My code:

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

Answer

Try this:

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