PHP将数据表单导入数据库

时间:2016-05-26 06:15:19

标签: php database forms

我创建了一个将数据插入数据库的表单。 我已经获得了两个函数来获取数据并显示它们,它们位于一个名为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);
}

获取功能:

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;
    }
}

在我的reviewsubmit.php中,我把它放在首位:

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

这是我的表格:

<form action="reviewsubmit.php" method="post">
<label for="firstname">First name:</label>
<input type="text" id="firstname" name="firstname" placeholder="Enter First name" required  /><br>
<label for="lastname">Last Name:</label>
<input type="text" id="lastname" name="lastname" placeholder="Enter Last name" required /><br>
<label for="address">Address:</label>
<input type="text" id="address" name="address" placeholder="Enter Address" required  /><br>
<label for="phone">Phone:</label>
<input type="text" id="phone" name="phone" placeholder="Enter Phone" required /><br>
<input type="submit" name="Submit" value="Submit" />
</form>

问题是我第一次点击“提交”时,它会将空值放入数据库,当我再次单击“提交”时,它会输入我输入的实际值: image

是什么导致它将空值添加到数据库中?

4 个答案:

答案 0 :(得分:0)

试试这个

<?php
require_once "queryDb.php";
if(isset($_post['Submit')){
$firstname = $_POST["firstname"];
$lastname = $_POST["lastname"];
$address = $_POST["address"];
$phone = $_POST["phone"];
addCustomer($firstname, $lastname, $address, $phone);
 }
?>

答案 1 :(得分:0)

第一次加载页面时,会在数据库中插入一行。发生这种情况是因为您没有检查表单是否已实际提交。

您需要检查表单是否已发布。你可以这样做:

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    IF (isset($_POST['submit'])) {
        // Do the database stuff
    }
}

// Your form here

答案 2 :(得分:0)

在将提交的值插入数据库之前检查提交的值是否为空或空是最好的选择。

    <?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);
        }

    ?>

答案 3 :(得分:0)

在调用addCustomer函数之前,您可以检查是否设置了$ _POST ['Submit']并且$ _POST不为空。 PHP函数emptyisset可以帮助您解决此问题。