Rob Geraghty Rob Geraghty - 7 months ago 13
PHP Question

PHP INSERT INTO localhost not working

After running a SELECT * FROM users, there is no difference to the table.

$firstName = $_POST["firstName"];
$lastName = $_POST["lastName"];
$dateOfBirth = $_POST["dateOfBirth"];
$gender = $_POST["gender"];
$fitnessLevel = $_POST["fitnessLevel"];
$number = $_POST["number"];
$address = $_POST["address"];
$password = $_POST["password"];

$user = 'root';
$pass = 'root';
$db = 'gymmembers';
$db = new mysqli('localhost',$user,$pass,$db) or die("Error, try again");


mysqli_query($db, "INSERT INTO users(`firstName`,`lastName`,`dateOfBirth`,`gender`,`fitnessLevel`,`number`,`address`,`password`)
VALUES('$firstName','$lastName','$dateOfBirth','$gender','$fitnessLevel','$number','$address','$password')" or die(mysqli_error()));


I can echo any of the variables and they show, so the data from the form is being passed to here.

Thanks :-)

Answer

Actually you putted die(mysqli_error()) inside mysqli_query() which is not correct, do like below:-

 mysqli_query($db, "INSERT INTO users(`firstName`,`lastName`,`dateOfBirth`,`gender`,`fitnessLevel`,`number`,`address`,`password`) 
            VALUES('$firstName','$lastName','$dateOfBirth','$gender','$fitnessLevel','$number','$address','$password')") or die(mysqli_error($db));

Note:- add $db in mysqli_error() so that if any error occur you will come to know.