简体   繁体   English

PHP表单在数据库中插入数据

[英]PHP Form to Insert data in Database

I've created a form that inserts data into a database. 我创建了一个将数据插入数据库的表单。 I've been given the two functions to get the data and display it, these are located in a file called queryDb.php: 我获得了两个函数来获取数据并显示它们,它们位于一个名为queryDb.php的文件中:

function addCustomer($fname, $lname, $address, $phone) {
    $db = new MyDB();
    if(!$db){
        echo '<script type="text/javascript">alert("'.$db->lastErrorMsg().'");</script>';
    } else {
        //echo "Opened database successfully\n";
    }

    $sql ='INSERT INTO CUSTOMERS (FIRSTNAME, LASTNAME, ADDRESS, PHONE) VALUES ("'.$fname.'", "'.$lname.'", "'.$address.'", "'.$phone.'");';
    $db->query($sql);
}

get function: 获取功能:

function getCustomers($searchTerm = null) {      
    $db = new MyDB();

    if(!$db){
        echo '<script type="text/javascript">alert("'.$db->lastErrorMsg().'");</script>';
    } else {
        //echo "Opened database successfully\n";
    }

    if(!$searchTerm) {
        $sql ='SELECT * from CUSTOMERS;';
    } else {
        $sql ='SELECT * FROM CUSTOMERS WHERE FIRSTNAME LIKE "'.$searchTerm.'" OR LASTNAME LIKE "'.$searchTerm.'" OR ADDRESS LIKE "'.$searchTerm.'" OR PHONE  LIKE "'.$searchTerm.'"';
    }
    $ret = $db->query($sql);
    $array = [];

    if(!$ret){
       echo $db->lastErrorMsg();
       return [];
    } else {
        while($row = $ret->fetchArray(SQLITE3_ASSOC) ){
            $array[] = $row;
        }
        $db->close();
        return $array;
    }
}

In my reviewsubmit.php I have this up the top: 在我的reviewsubmit.php中,我的排名靠前:

<?php
    require_once "queryDb.php";
    $firstname = $_POST["firstname"];
    $lastname = $_POST["lastname"];
    $address = $_POST["address"];
    $phone = $_POST["phone"];
    addCustomer($firstname, $lastname, $address, $phone);
?>

And this is my form: 这是我的表格:

<form action="reviewsubmit.php" method="post">
    Firstname
    <input type="text" id="firstname" name="firstname">  />
    Lastname
    <input type="text" id="lastname" name="lastname"">  />
    Address
    <input type="text" id="address" name="address">  />
    Phone
    <input type="text" id="phone" name="phone">  />
    <input type="submit" name="Submit" value="Submit" />
</form>

The problem is when I submit information into the Database using the form, it submits empty values for everything when I click submit the first time. 问题是当我使用表单将信息提交到数据库时,当我第一次单击提交时,它为所有内容提交空值。 Then when I hit submit again it submits the actual values: image 然后,当我再次点击Submit时,它将提交实际值: image

Your HTML Mark-up is not really the best and that could cause some anomalies. 您的HTML标记实际上并不是最好的标记,它可能会导致一些异常。 Simultaneously, it would be ideal to add the required Attribute to your Fields to ensure that all the necessary Fields are filled before submitting like so: 同时,理想的是将必需的属性添加到您的字段中,以确保在提交之前填充所有必需的字段,如下所示:

<!-- TO GUARD AGAINST ACCIDENTALLY INSERTING EMPTY VALUES -->
<!-- WHY NOT MAKE THE INPUT FIELDS MANDATORY -->
<form action="reviewsubmit.php" method="post">
    <label for="firstname">Firstname</label>
    <input type="text" id="firstname" name="firstname" required  />

    <label for="lastname">Lastname</label>
    <input type="text" id="lastname" name="lastname" required />

    <label for="address">Address</label>
    <input type="text" id="address" name="address" required />

    <label for="phone">Telephone</label>
    <input type="text" id="phone" name="phone" required/>

    <input type="submit" name="Submit" value="Submit" />
</form>

On the PHP Side, you may also go the extra mile of checking that the submitted values are not empty or null before submitting to the Database. 在PHP方面,在提交到数据库之前,您还可以花更多的精力检查提交的值不为空或为null。 That way you are both sure of what is going on at the Front and Back-ends. 这样,您就可以确定前端和后端的情况。 This could be a way of doing that: 这可能是这样做的一种方式:

<?php
    // FILE: index.php
    require_once "queryDb.php";
    $firstname  = isset($_POST["firstname"])? htmlspecialchars(trim($_POST["firstname"]))   : null;
    $lastname   = isset($_POST["lastname"]) ? htmlspecialchars(trim($_POST["lastname"]))    : null;
    $address    = isset($_POST["address"])  ? htmlspecialchars(trim($_POST["address"]))     : null;
    $phone      = isset($_POST["phone"])    ? htmlspecialchars(trim($_POST["phone"]))       : null;

    // CHECK THAT YOU DON'T HAVE NULL OR EMPTY FIELD VALUES BEFORE INSERTING ANYTHING INTO DATABASE
    if(!is_null($firstname) && !is_null($lastname) && !is_null($address) && !is_null($phone) ){
        addCustomer($firstname, $lastname, $address, $phone);
    }

?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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