JohnyChew
JohnyChew

Reputation: 147

Post from form not working

When I click the submit button , the imputed data should go into the query bellow , However it doesn't.

It seems that the value of the button is being used instead.

Can someone figure out why it doesn't work. Its the same code being used for my login page and that works.

It goes to the else part of the if statement

Error :

Error: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'FROM test_set WHERE Room_Code='room'' at line 1

Query :

if (isset($_POST['room']))
    {
    require "connect.php";

    if (count($_POST) > 0)
        {
        $result = mysqli_query($conn, "SELECT id, FROM test_set WHERE Room_Code='" . $_POST["room"] . "'");
        if (!$result) {
    printf("Error: %s\n", mysqli_error($conn));
    exit();
}
        $row = mysqli_fetch_array($result);
        if (is_array($row))
            {
            $_SESSION["Room_ID"] = $row['id'];
            header("Location: ../views/student/question.php?id='" . $_SESSION["Room_ID"] . "'");
            }
          else
            {
            echo "No";
            }
        }
    }

Form :

 <form method="POST" name="room" action="../../config/functions.php">
            <label for="room" class="sr-only">Room Code</label>
            <input type="text" id="room" name="room" class="form-control" placeholder="Please Enter Room Code">
            <br>
            <input type="submit" class="waves-effect waves-light btn blue darken-3" name="room" value="room">
        </form>

Edit:

Still not working also changing in this way:

$result = mysqli_query($conn, "SELECT id FROM test_set WHERE Room_Code='" . $_POST["room"] . "'");

Upvotes: 0

Views: 53

Answers (2)

drakyoko
drakyoko

Reputation: 505

The value of the button is used because the name you assigned to the submit button is 'room': the same name you're using for the input text. Remove the name attribute from the submit button and it should work.

after edit2

change

header("Location: ../views/student/question.php?id='" . $_SESSION["Room_ID"] . "'");

to

header("Location: ../views/student/question.php?id=" . $_SESSION["Room_ID"]);

you don't have to quote variables in the query string

Upvotes: 1

Gouda Elalfy
Gouda Elalfy

Reputation: 7023

remove , from your query:

SELECT id,

$result = mysqli_query($conn, "SELECT id FROM test_set 
WHERE Room_Code='" . $_POST["room"] . "'");

Upvotes: 4

Related Questions