skip to Main Content

I am a complete beginner when it comes to PHP and mySQL. I am currently working on a simple project where a user would go to a form created in HTML and fill out some information and when they hit submit the data should theoretically be saved into a database. I created the form and each time it would be filled, it would then say that the account is created and no errors are being displayed. However, the information is not being inserted into the database on PHPadmin. I have been trying to solve this by looking at youtube videos and other stackoverflow forums but It is still not inserting. If someone could pinpoint what is wrong with my code that would be great.I will attach what I think is relevant in terms of code below.

Here is the main php file

<?php
$email = "";
$password = "";
$errors = array();

// connect to the database
$db = new mysqli('localhost', 'root', '', 'bikeshop');

// check the connection 
if (!$db) {
    die("Connection failed: " . mysqli_connect_error());
}

echo "Connected successfully";

//if the register button is clicked
if (isset($_POST['register'])) {

    $firstname = mysqli_real_escape_string($db, $_POST['firstname']);
    $lastname= mysqli_real_escape_string($db, $_POST['lastname']);
    $streetaddress = mysqli_real_escape_string($db, $_POST['streetaddress']);
    $city = mysqli_real_escape_string($db, $_POST['city']);
    $state = mysqli_real_escape_string($db, $_POST['state']);
    $zipcode = mysqli_real_escape_string($db, $_POST['zipcode']);
    $phonenumber = mysqli_real_escape_string($db, $_POST['phonenumber']);
    $email = mysqli_real_escape_string($db, $_POST['email']);
    $password = mysqli_real_escape_string($db, $_POST['password']);

    // ensure that form fields are filled properly 
if (empty($email)) {
    array_push($errors, "Email is required"); 
}

if (empty($password)) {
    array_push($errors, "Password is required"); 
}

    // if there are no erros, save user to database

    if (count($errors) ==0) {
        $password = md5($password); //password encryption

        $sql = "INSERT INTO customer (FirstName, LastName, Address, City, State, Zip code, PhoneNumber, Email, Password) VALUES ('$firstname','$lastname','$streetaddress','$city','$state','$zipcode','$phonenumber','$email','$password')";

        if(!mysqli_query($db, $sql)) 
        {
            echo 'Account Created';
        }
        else 
        {
            echo 'Account creation failed';
        }

}

}

?>

Here is the errors.php file 

<?php if(count($errors) > 0): ?>
<div class="error">
    <?php foreach ($errors as $error): ?>
    <p><?php echo $error; ?></p>
    <?php endforeach ?>
</div>
<?php endif ?>

Here is the form section that I created in HTML

 <!-- Login page container -->
              <form method="post" action="register.php">
    <!-- display validation errors here --> 
    <?php include('errors.php'); ?> 

  <div class="container">
    <h1>Register</h1>
    <p>Please fill in this form to create an account.</p>
    <hr>

    <label for="firstname"><b>First Name</b></label>
    <input type="text" placeholder="Enter first name" name="firstname" required>

    <label for="lastname"><b>Last Name</b></label>
    <input type="text" placeholder="Enter last name" name="lastname" required>

    <label for="streetaddress"><b>Street Address</b></label>
    <input type="text" placeholder="Enter Street Address" name="streetaddress" required>

    <label for="city"><b>City</b></label>
    <input type="text" placeholder="City" name="city" required>

    <label for="state"><b>State</b></label>
    <input type="text" placeholder="Enter State" name="state" required>

    <label for="zipcode"><b>Zip Code</b></label>
    <input type="text" placeholder="Enter Zip Code" name="zipcode" required>

    <label for="phonenumber"><b>Phone Number</b></label>
    <input type="text" placeholder="Enter Phone Number" name="phonenumber" required>


    <label for="email"><b>Email</b></label>
    <input type="text" placeholder="Enter Email" name="email" value="<?php echo $email; ?>">

    <label for="password"><b>Password</b></label>
    <input type="password" placeholder="Enter Password" name="password" value="<?php echo $password; ?>">

    <p>By creating an account you agree to our <a href="#">Terms & Privacy</a>.</p>
    <button type="submit" name= "register" class="registerbtn">Register</button>
  </div>

  <div class="container signin">
    <p>Already have an account? <a href="internallogin.php">Sign in</a>.</p>
  </div>
           </form>

2

Answers


  1. if(count($errors) == 0)
    

    should be changed with if(empty($errors))
    that’s because you’re checking if an array is equal to zero. An array can be empty, while a varible can be null, zero, undefined (they’re not the same though).

    Login or Signup to reply.
  2. Try this

    if (isset($_POST['register'])) {
    
        $firstname = $_POST['firstname'];
        $lastname= $_POST['lastname'];
        $streetaddress =  $_POST['streetaddress'];
        $city =  $_POST['city'];
        $state =  $_POST['state'];
        $zipcode =  $_POST['zipcode'];
        $phonenumber =  $_POST['phonenumber'];
        $email =  $_POST['email'];
        $password = $_POST['password'];
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search