skip to Main Content

I’m working on a project that uses a sort of roomCode host/join system to function. It’s written in js/html/css with a php server. I’ve had success so far with using PHP and MySQL on this project to insert stuff into some tables.

The specific feature I’m working on allows the host to close the room, which deletes the roomCode from the database along with all the users connected to it. However, I’m running into an issue where the mysqli query isn’t deleting anything from the database. I’m just starting out with trying to delete the room info (all in one row), but it shows every indication of not working. I’ve verified that phpMyAdmin has the data I’m trying to delete, and the query works when I manually execute it there, but when run from my app, affected_rows remains 0, and the data is still on phpMyAdmin.

Here’s the code (SQLConnect() is a helper function that holds my DB login info):

$mysql = SQLConnect();
$status = 'wait';

$stmt = $mysql -> prepare('DELETE FROM room WHERE roomCode = ?');
$stmt -> bind_param('s', $_POST['roomCode']);
$stmt -> execute();

if($mysql -> affected_rows == 0){
    $response = ['status' => 'error',
                 'error' => 'Error closing room!'];
    echo json_encode($response);
    return;
} else {
    $response = [
        'status' => 'ok' //Placeholder response for now
    ];
    echo json_encode($response);
    return;
}

Interestingly enough, when trying this same setup with a SELECT query looking for the roomCode, it doesn’t return anything back. All other php files with mySQL queries work to insert and select stuff, so I’m stumped as to why this file isn’t working when it uses the same SQLConnect() function to setup the connection.

2

Answers


  1. "$stmt -> bind_param(‘s’, $_POST[‘roomCode’]);"

    Have you tried setting it to ‘i’ instead? I’m assuming the roomCode is an integer and not a string. That might be causing the code to not execute.

    Login or Signup to reply.
  2. Did you check if "$stmt -> execute();" returns true or false?
    It should return true if the statement was executed properly, false if it doesn’t.

    Also, if the $mysql->error didn’t show nothing, you should try checking $mysql->connect_error just in case.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search