Kuyo Kuyo - 3 months ago 11
MySQL Question

PHP update mysql table, subtract by 1

I am trying to do a ticketing system. When the user has registered successfully, subtract the available ticket seats by 1.

$AvailSeats = $_POST['myAvailSeats']; //3

//Once registration info entered, minus Available Seats

$sql = "UPDATE tblcourseinfo SET
AvailSeats = --$AvailSeats
WHERE CourseID = '$CourseID'";


However when this statement is queried, my
AvailSeats
was updated to
0
instead of
2
. My data type is int for
AvailSeats
. Why is this happening?

EDIT:

Thanks everyone who helped me. Here is the solution that everyone has provided:

$AvailSeats = $_POST['myAvailSeats']; // 3

//Once registration info entered, minus Available Seats

--$AvailSeats; // Decrease the seat by 1 first

$sql = "UPDATE tblcourseinfo SET
AvailSeats = --$AvailSeats
WHERE CourseID = '$CourseID'";

Answer

just store your value in a variable and subtract 1 from there and pass it to the query

$AvialSeats = $currentSeats - 1; 
$sql = "UPDATE tblcourseinfo set  AvailSeats = $AvialSeats  WHERE CourseID = '$CourseID'";
Comments