0

I am trying to write PHP code that will increase the count by 1 when the user clicks RSVP. As of now, when I click the RSVP button I get a message that says that the RSVP was successful but when I check the database the RSVP count is still 0. Here is the code that is supposed to carry out this function.

<?php
$servername = "db.sice.indiana.edu";
$username = "i494f20_tywinfre";
$password = "my+sql=i494f20";
$dbname = "i494f20_tywinfre";

// Create Connection

$conn = mysqli_connect("db.sice.indiana.edu", "i494f20_tywinfre", 
"my+sql=i494f20_tywinfre", "i494f20_tywinfre");

// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}



$id = $_POST["EVENTID"];


$sql = "SELECT CURRENTGUEST, MAXGUEST FROM EVENTS WHERE EVENTID 
$id";

$result = mysqli_query($conn, $sql);

if (mysqli_num_rows ($result) > 0) {

//OUTPUT DATA OF EACH ROW

while($row = mysqli_fetch_assoc($result)) {  

if($row['CURRENTGUEST']<$row['MAXGUEST']) {
$id = $_POST["EVENTID"];
$t = $row['CURRENTGUEST'];


$query = "UPDATE EVENTS SET CURRENTGUEST = CURRENTGUEST + 1 
WHERE EVENTID = $id";

echo "RSVP successful";
}
}

} else {

echo "Event is full";

}

mysqli_close($conn);
?>

Here is my code I am not sure what the problem could be thank you for the help!

  • 1
    You are not executing the update query – Elias Soares Aug 05 '21 at 01:04
  • As stated above, the $query = "UPDATE EVENTS"... line is just defining a variable called "query", not executing anything. you need to add a line like mysqli_query($conn, $query); – Joe Love Aug 05 '21 at 07:12

1 Answers1

1

You are only setting the query variable with the update statement, but you are never executing it.

Add

mysqli_query($conn, $query);

After your $query line.

Also, your code is vulnerable to SQL Injection. See How can I prevent SQL injection in PHP?

You should also check the result of the update query to ensure that the RSVP was done correctly, otherwise user will ger RSVP sucesfull always, even if the query fails.

Elias Soares
  • 9,884
  • 4
  • 29
  • 59