I created a registration form that takes 3 inputs from the user and then submits data to MySQL
database but in the localhost MySQL
, data is not saving. I am PHP Newbie, HELP PLEASE!
On using below registration.php(1) code I get no errors on localhost/register.html but register-form data is not saving in MySql database:
<?php
session_start();
$conn = mysqli_connect('localhost','root','pass','gm-registration');
$username = $_POST['username'];
$email = $_POST['email'];
$password = $_POST['password'];
$s = "select * from users where username = '$username'";
$result = mysqli_query($conn, $s);
$num = mysqli_num_rows($result);
if($num == 1){
echo "This Username is Already Taken";
}
else{
$reg = "insert into users(username, email id, password) values('$username', '$email', '$password')";
mysqli_query($conn, $reg);
echo "Registration Successful";
}
?>
On using below registration.php(2) code I get one localhost/register.html error ->> Fatal error: Uncaught Error: Call to a member function bind_param() on bool in C:\xampp\htdocs\gamingmode\registration.php:22 Stack trace: #0 {main} thrown in C:\xampp\htdocs\gamingmode\registration.php on line 22.
<?php
session_start();
$conn = mysqli_connect('localhost','root','7777','gm-registration');
$username = $_POST['username'];
$email = $_POST['email'];
$password = $_POST['password'];
$s = "select * from users where username = '$username'";
$result = mysqli_query($conn, $s);
$num = mysqli_num_rows($result);
if($num==1){
echo "This Username is Already Taken";
}
else{
$stmt = $conn->prepare("INSERT INTO users(username, email id, password) VALUES(?,?,?)");
$stmt->bind_param("sss", $username, $email, $password); //line 22
$stmt->execute();
echo "Registration Successful";
$stmt->close();
$conn->close();
}
?>
My `html` form code :
<form id="register" class="input_group" action="registration.php" method="post">
<input type="username" class="input_field" placeholder="Username" name="username">
<input type="email" class="input_field" placeholder="Email Id" name="email">
<input type="password" class="input_field" placeholder="Password" name="password">
<input type="checkbox" class="checkbox" name="checkbox"> <span>I agree to the term & conditions</span>
<button type="submit" class="submit_btn">Register</button>
</form>
What should I do now to save forms data to MySql Database? registration.php(1) is not saving the form's data. & registration.php(2) is throwing one fatal error.