Learn PHP Basics: Master CRUD Operations to Build Dynamic Applications

    4. Add data (Create) 

    Let’s add in a link that takes us to our create form.

    <a href="create.php" class="btn btn-primary">Create New Contact</a>

    See it in context:

    <?php include('connect.php'); include('head.php'); ?> 
        <h1>Contact Manager</h1> 
        <a href="create.php">Create New Contact</a> 
    <?php include('foot.php') ?>

    This is what our screen looks like after adding the link to our form.

    To add data, we will add a form. The completed code is below. Check out the “Break it down” section, immediately afterwards, to understand the various parts of the code.

    In summary: The php code will test if a form has been submitted, if it has then it validates the fields of the form. If the fields pass validation, then it will create a new record, in the database, using the data from our form.

    Create the file: create.php. Paste in the following code.

    <?php
    include('connect.php');
    include('head.php');
    
    // if form is not empty
    if (!empty($_POST)) {
    // retrieving data from form
        $name = $_POST['name'];
        $email = $_POST['email'];
        $phone = $_POST['phone'];
        $notes = $_POST['notes'];
    
    // validating the data is not empty
        $valid = true;
        if (!$name) {
           echo '<script> alert("Please enter valid name"); </script>';
           $valid = false;
        }
    
        if (!$email) {
           echo '<script> alert("Please enter valid email"); </script>';
           $valid = false;
        }
    
    // if valid, add the data
        if ($valid) {
           $sql = 'INSERT INTO contacts (name,email,phone,notes) VALUES (?,?,?,?)';
           $row = $connection->prepare($sql);
           $row->execute([$name, $email, $phone, $notes]);
    
    // redirect to index echo '<script> alert("Contact added");
           window . location = "index.php" </script > ';
    }
    }
    ?>
    
    <h1>Add Contact</h1>
    <!-- after form is submitted, will be redirected to the same page and the code at the top will be executed resulting in data added and redirected to index page -->
    
    <form action="create.php" method="post">
    
    <label for="name">Name</label>
    <input type="text" name="name" placeholder="John Doe" id="name" require> <br>
    
    <!-- by choosing email type, a simple validation will be made by html to check if the input is in email format -->
    
    <label for="email">Email</label>
    <input type="email" name="email" placeholder="johndoe@email.com" id="email" require> <br>
    
    <label for="phone">Phone</label>
    <input type="tel" name="phone" placeholder="928174823" id="phone"> <br>
    
    <label for="notes">Notes</label>
    <input type="text" name="notes" placeholder="My Friend" id="notes"> <br>
    <input type="submit" value="Create">
    </form>
    
    <?php include('foot.php') ?>

    Break it down

    Add form

    <!-- after form is submitted, will be redirected to the same page and the code at the top will be executed resulting in data added and redirected to index page --> 
    
    <form action="create.php" method="post"> 
        <label for="name" class="form-label">Name</label> 
        <input type="text" name="name" class="form-control" placeholder="John Doe" id="name" require> 
        <br>  
    
        <!-- by choosing email type, a simple validation will be made by html to check if the input is in email format --> 
        
        <label for="email" class="form-label">Email</label> 
        <input type="email" name="email" class="form-control" placeholder="johndoe@email.com" id="email" require> 
        <br>  
        <label for="phone" class="form-label">Phone</label> 
        <input type="tel" name="phone" class="form-control" placeholder="928174823" id="phone"> 
        <br> 
        <label for="notes" class="form-label">Notes</label> 
        <input type="text" name="notes" class="form-control" placeholder="My Friend" id="notes"> 
        <br> 
        <input type="submit" class="mt-3 btn btn-primary" value="Create"> 
    </form>

    Validate Data

    <?php 
    ... 
    
    // if form is not empty 
    if(!empty($_POST)){ 
        // retrieving data from form 
        $name = $_POST['name'];
        $email = $_POST['email']; 
        $phone = $_POST['phone']; 
        $notes = $_POST['notes']; 
    
        // validating the data is not empty 
        $valid = true; 
    
        if (!$name) { 
            echo '<script> alert("Please enter valid name"); </script>'; 
            $valid = false; 
        } 
    
        if (!$email) { 
            echo '<script> alert("Please enter valid email"); </script>'; 
            $valid = false; 
        } 
    }
    
    ... 
    
    ?>

    Save data to database

    <?php 
    ... 
    
        // if valid, add the data 
        if ($valid) { 
            $sql = 'INSERT INTO contacts (name,email,phone,notes) VALUES (?,?,?,?)'; 
            $row = $connection->prepare($sql); 
            $row->execute([$name, $email, $phone, $notes]); // redirect to index 
            echo '<script> alert("Contact added"); window.location="index.php" </script>'; 
         } 
    }
    ?>

    Now, after you click the Create New Contact link, you should see this form

    If you submit it, there’ll be a confirmation and you’ll be directed to index with your newly added data