MYSQL PHP prepared statement problem

Hello, I am trying to insert values into a tables from a signup php script. When I run the following part of my code i get -1 echoed to the screen I dont know why so I came here to see if anyone would know.

                    $query = "INSERT INTO login (ID,FirstName,LastName,StudentID,UserName,Password)                  VALUES (NULL,?, ?, ?, ?, ?)";
                    $stmt = mysqli_prepare($con, $query);
                    mysqli_stmt_bind_param($stmt, "ssiss", $f_Name, $l_Name, $SID, $email, $password);
                    mysqli_stmt_execute($stmt);

                    $affected_rows = mysqli_stmt_affected_rows($stmt);
                    if($affected_rows == 1)
                    {
                            require ('regPass.html');
                    }
                    else
                    {
                            print_r($affected_rows);
                            require('failedReg.html');
                            mysqli_close($con);
                    }

You are counting on some conditions that may not work. For instance, if the database is not reachable, the query fails. Did you open the connection before trying to do the insert? I don’t see you opening the connection.

Try wrapping the code in a try catch statement and echoing the errors. You should also turn error reporting on for testing your code. Otherwise you are working blind.

Sponsor our Newsletter | Privacy Policy | Terms of Service