简体   繁体   中英

I have created a client address book, but can't seem to add new clients

I have created an address book for my clients app. I can log in and out. The data is not remembered by the browser when I hit refresh, redirect to log back in, all that is good, but after adding the file below to add new clients. I am unable to do so. Could someone assist me in identifying why I cannot successfully add new clients?

<?php
session_start();

// if user is not logged in
if(!$_SESSION['loggedInUser']) {

    // send them to the login page
    header("Location: index.php");
}

// connect to the database
include('includes/connection.php');

// include functions file
include('includes/functions.php');

// if add button was submitted
if(isset($_POST['add'])) {

    // set all variables to empty by default
    $clientName = $clientEmail = $clientPhone = $clientAddress =$clientCompany = $clientNotes = "";

    // check to see if inputs are empty
    // create variables with form data
    // wrap the data with our function

    if($_POST["clientName"]) {
        $nameError = "Please enter a name <br>";
    } else {
        $clientName = validateFormData($_POST["clientName"]);
    }

    if($_POST["clientEmail"]) {
        $emailError = "Please enter a email <br>";
    } else {
        $clientEmail = validateFormData($_POST["clientEmail"]);
    }

    // these inputs are not required
    // so we'll just store whatever has been entered
    $clientPhone    = validateFormData($_POST["clientPhone"]);
    $clientAddress    = validateFormData($_POST["clientAddress"]);
    $clientCompany    = validateFormData($_POST["clientCompany"]);
    $clientNotes    = validateFormData($_POST["clientNotes"]);

    // if required fields have data
    if($clientName && $clientEmail) {

        // create query
        $query = "INSERT INTO client(id, name, email, phone, address, company, notes, date_added) VALUES(NULL, '$clientName', '$clientEmail', '$clientPhone', '$clientAddress', 'clientCompany', 'clientNotes', CURRENT_TIMESTAMP)";

        $result = mysqli_query($conn, $query);

        // if query was successful
        if($result) {

            // refresh page with query string
            header("Location: clients.php?alert=success");
        } else {

            // something went wrong
            echo "Error: ".$query."<br>".mysqli_error($conn);
        }
    }
}

// close the mysql connection
mysqli_close($conn);



include('includes/header.php');
?>

<h1>Add Client</h1>

<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="post" class="row">
    <div class="form-group col-sm-6">
        <label for="client-name">Name *</label>
        <input type="text" class="form-control input-lg" id="client-name" name="clientName" value="">
    </div>
    <div class="form-group col-sm-6">
        <label for="client-name">Email *</label>
        <input type="text" class="form-control input-lg" id="client-email" name="clientEmail" value="">
    </div>
    <div class="form-group col-sm-6">
        <label for="client-phone">Phone</label>
        <input type="text"
 class="form-control input-lg" id="client-phone" name="clientPhone" value="">
    </div>
    <div class="form-group col-sm-6">
        <label for="client-address">Address</label>
        <input type="text" class="form-control input-lg" id="client-address" name="clientAddress" value="">
    </div>
    <div class="form-group col-sm-6">
        <label for="client-company">Company</label>
        <input type="text" class="form-control input-lg" id="client-company" name="clientCompany" value="">
    </div>
    <div class="form-group col-sm-6">
        <label for="client-notes">Notes</label>
        <textarea type="text" class="form-control input-lg" id="client-notes" name="clientNotes"></textarea>
    </div>
    <div class="col-sm-12">
        <a href="clients.php" type="button" class="btn btn-lg btn-default">Cancel</a>
        <button type="submit" class="btn btn-lg btn-success pull-right" name="add">Add Client</button>
    </div>
</form>

<?php
include('includes/footer.php');
?>

Check that your "client" table allows ID value as NULL. I believe that's the problem because if you setup your table correctly that would be your primary key which should not be NULL.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM