使用PHP将数据插入数据库

时间:2017-11-26 22:50:07

标签: php mysql arrays database

问题是:在filter_input过滤并保存到关联数组后,如何将用户输入的数据发送到数据库。我尝试这样做的方式,即使我用过滤的,已清理的输入填充数组,我也会在filter_inputs数组的每个字段中得到错误的未定义索引。此外,如果有人知道替代方案或更好的方法,请告诉我们。有问题的数据库是MariaDB mySQL方言。所有内容都放在服务器上并运行并使用分配给服务器的phpmyadmin。

using DifferentialEquations
using Plots
function lorenz(t,u,du)
du[1] = 10.0*(u[2]-u[1])
du[2] = u[1]*(28.0-u[3]) - u[2]
du[3] = u[1]*u[2] - (8/3)*u[3]
end

u0 = [1.0;1.0;1.0]
tspan = (0.0,100.0)
prob = ODEProblem(lorenz,u0,tspan)
sol = solve(prob,reltol=1e-8,abstol=1e-8,saveat=collect(0:0.01:100))

这是PHP类:(我使用相同的函数来过滤几乎每个输入字段,但我删除了它们,因此这里的代码较少。基本上每个过滤器方法都会向数组中添加一个新元素它的关联名称:firstName,lastName,email等。)出于某种原因,当我尝试insertAll()应该将数据发送到数据库时,该数组似乎是空的。

<?php require_once("Filtering.php");

$filter = new Filtering();

$filter->connect();

if (isset($_POST['submit'])){
    $filter->insertAll();
}

?>
<!DOCTYPE html>
<html lang="en">

<head>
</head>

<body>

<div class="container-fluid">
    <form action="<?php $_SERVER['PHP_SELF']; ?>" method="POST" name="order" novalidate>
        <fieldset>
            <legend>Order Summary</legend>
            <br>
            <div class="row">
                <div class="col-12 d-flex flex-row">
                    <div class="col-4">
                        <span><strong>Product Name</strong></span>
                    </div>
                    <div class="col-2 justify-content-center">
                        <span><strong>Quantity</strong></span>
                    </div>
                    <div class="col-3 justify-content-center">
                        <span><strong>Price</strong></span>
                    </div>
                </div>
            </div>
            <br>
            <div class="row">
                <div class="col-12 d-flex flex-row">
                    <div class="col-4">
                        <span>Gigabyte GTX1060 G1 Gaming 3GB GDDR5</span>
                    </div>
                    <div class="col-2 justify-content-center">
                        <select class="form-control col-3" id="quantity">
                        </select>
                    </div>
                    <div class="col-3 justify-content-center">
                        <span id="price">$ 349.99</span>
                    </div>
                </div>
            </div>
        </fieldset>
        <br>
        <fieldset>
            <legend>Customer Information</legend>
            <br>
            <div class="row">
                <div class="col-8 d-flex flex-row">
                    <div class="col-2">
                        <label class="col-form-label">First Name<span class="required">*</span></label>
                    </div>
                    <div class="col-auto">
                        <input class="form-control" placeholder="Enter your first name" type="text" name="firstName">
                    </div>
                </div>
            </div>
            <?php
            if ($_SERVER["REQUEST_METHOD"] == "POST") {
                if (!$filter->filterFirstName()) {
                    echo "<div class='row'>
                             <div class='col-5'>
                                  <small class='text-muted ml-3 warning'>! Field must not be empty.</small>
                             </div>
                          </div>";
                }
            }
            ?>
            <br>
            <div class="row">
                <div class="col-8 d-flex flex-row">
                    <div class="col-2">
                        <label class="col-form-label">Last Name<span class="required">*</span></label>
                    </div>
                    <div class="col-auto">
                        <input class="form-control" placeholder="Enter your last name" type="text" name="lastName">
                    </div>
                </div>
            </div>
            <?php
            if ($_SERVER["REQUEST_METHOD"] == "POST") {
                if (!$filter->filterLastName()) {
                    echo "<div class='row'>
                             <div class='col-5'>
                                  <small class='text-muted ml-3 warning'>! Field must not be empty.</small>
                             </div>
                          </div>";
                }
            }
            ?>
            <br>
            <div class="row">
                <div class="col-8 d-flex flex-row">
                    <div class="col-2">
                        <label class="col-form-label">E-mail<span class="required">*</span></label>
                    </div>
                    <div class="col-auto">
                        <input class="form-control" placeholder="Enter your e-mail" type="text" name="email">
                    </div>
                </div>
            </div>
            <?php
            if ($_SERVER["REQUEST_METHOD"] == "POST") {
                if (!$filter->filterEmail()) {
                    echo "<div class='row'>
                             <div class='col-5'>
                                  <small class='text-muted ml-3 warning'>! Field must not be empty.</small>
                             </div>
                          </div>";
                }
            }
            ?>
            <br>
            <div class="row">
                <div class="col-8 d-flex flex-row">
                    <div class="col-2">
                        <label class="col-form-label">Number<span class="required">*</span></label>
                    </div>
                    <div class="col-auto">
                        <input class="form-control noButton" placeholder="Enter your number" type="number"
                               name="number">
                    </div>
                </div>
            </div>
            <?php
            if ($_SERVER["REQUEST_METHOD"] == "POST") {
                if (!$filter->filterNumber()) {
                    echo "<div class='row'>
                             <div class='col-5'>
                                  <small class='text-muted ml-3 warning'>! Field must not be empty.</small>
                             </div>
                          </div>";
                }
            }
            ?>
        </fieldset>
        <br>
        <br>
        <fieldset>
            <legend>Payment Information</legend>
            <br>
            <div class="row">
                <div class="col-8 d-flex flex-row">
                    <div class="col-3">
                        <label class="col-form-label">Card Number<span class="required">*</span></label>
                    </div>
                    <div class="col-auto">
                        <input class="form-control noButton" placeholder="xxxx-xxxx-xxxx-xxxx" type="number"
                               name="cardNum">
                    </div>
                </div>
            </div>
            <?php
            if ($_SERVER["REQUEST_METHOD"] == "POST") {
                if (!$filter->filterCard()) {
                    echo "<div class='row'>
                             <div class='col-5'>
                                  <small class='text-muted ml-3 warning'>! Field must not be empty. Must not be longer than 16 digits</small>
                             </div>
                          </div>";
                }
            }
            ?>
            <br>
            <div class="row">
                <div class="col-8 d-flex flex-row">
                    <div class="col-3">
                        <label class="col-form-label">Expiration Date<span class="required">*</span></label>
                    </div>
                    <div class="col-2">
                        <input class="form-control" placeholder="MM/YY" type="text" name="expDate">
                    </div>
                </div>
            </div>
            <?php
            if ($_SERVER["REQUEST_METHOD"] == "POST") {
                if (!$filter->filterExpDate()) {
                    echo "<div class='row'>
                             <div class='col-5'>
                                  <small class='text-muted ml-3 warning'>! Field must not be empty. Enter a valid date format: 'MM/YY'</small>
                             </div>
                          </div>";
                }
            }
            ?>
            <br>
            <div class="row">
                <div class="col-8 d-flex flex-row">
                    <div class="col-3">
                        <label class="col-form-label">Security Number<span class="required">*</span></label>
                    </div>
                    <div class="col-2">
                        <input class="form-control noButton" placeholder="xxxz" type="number" name="secNum">
                    </div>
                </div>
            </div>
            <?php
            if ($_SERVER["REQUEST_METHOD"] == "POST") {
                if (!$filter->filterSecurityNum()) {
                    echo "<div class='row'>
                             <div class='col-5'>
                                  <small class='text-muted ml-3 warning'>! Field must not be empty. Must be at most 4 digits long</small>
                             </div>
                          </div>";
                }
            }
            ?>
        </fieldset>
        <br>
        <br>
        <fieldset>
            <legend>Delivery Information</legend>
            <br>
            <div class="row">
                <div class="col-8 d-flex flex-row">
                    <label class="form-check-label">
                        <strong>Delivery Option</strong>
                    </label>
                </div>
            </div>
            <div class="row">
                <div class="col-8 d-flex flex-row">
                    <label class="form-check-label">
                        <input class="form-check-input" type="radio" name="exampleRadios" id="exampleRadios1"
                               value="option1" checked>
                        Standard Delivery - Free of charge
                    </label>
                </div>
                <div class="col-8 d-flex flex-row">
                    <label class="form-check-label">
                        <input class="form-check-input" type="radio" name="exampleRadios" id="exampleRadios1"
                               value="option1">
                        Express Delivery - $ 5.99
                    </label>
                </div>
            </div>
            <br>
            <div class="row">
                <div class="col-8 d-flex flex-row">
                    <div class="col-3">
                        <label class="col-form-label">Address Line 1<span class="required">*</span></label>
                    </div>
                    <div class="col-auto">
                        <input class="form-control" placeholder="Enter your address" type="text" name="address">
                    </div>
                </div>
            </div>
            <?php
            if ($_SERVER["REQUEST_METHOD"] == "POST") {
                if (!$filter->filterAddress()) {
                    echo "<div class='row'>
                             <div class='col-5'>
                                  <small class='text-muted ml-3 warning'>! Field must not be empty.</small>
                             </div>
                          </div>";
                }
            }
            ?>
            <br>
            <div class="row">
                <div class="col-8 d-flex flex-row">
                    <div class="col-3">
                        <label class="col-form-label">Address Line 2</label>
                    </div>
                    <div class="col-auto">
                        <input class="form-control" placeholder="Optional" type="text" name="address2">
                    </div>
                </div>
            </div>
            <br>
            <div class="row">
                <div class="col-8 d-flex flex-row">
                    <div class="col-3">
                        <label class="col-form-label">Country</label>
                    </div>
                    <div class="col-auto">
                        <input class="form-control" value="Croatia" type="text" name="country" disabled>
                    </div>
                </div>
            </div>
            <br>
            <div class="row">
                <div class="col-8 d-flex flex-row">
                    <div class="col-3">
                        <label class="col-form-label">County</label>
                    </div>
                    <div class="col-auto">
                        <select class="custom-select" id="county">
                        </select>
                    </div>
                </div>
            </div>
            <br>
            <div class="row">
                <div class="col-8 d-flex flex-row">
                    <div class="col-3">
                        <label class="col-form-label">City<span class="required">*</span></label>
                    </div>
                    <div class="col-auto">
                        <input class="form-control" placeholder="Enter your city" type="text" name="city">
                    </div>
                </div>
            </div>
            <?php
            if ($_SERVER["REQUEST_METHOD"] == "POST") {
                if (!$filter->filterCity()) {
                    echo "<div class='row'>
                             <div class='col-5'>
                                  <small class='text-muted ml-3 warning'>! Field must not be empty.</small>
                             </div>
                          </div>";
                }
            }
            ?>
            <br>
            <div class="row">
                <div class="col-8 d-flex flex-row">
                    <div class="col-3">
                        <label class="col-form-label">Zipcode<span class="required">*</span></label>
                    </div>
                    <div class="col-auto">
                        <input class="form-control noButton" placeholder="Enter your zipcode" type="number" name="zip">
                    </div>
                </div>
            </div>
            <?php
            if ($_SERVER["REQUEST_METHOD"] == "POST") {
                if (!$filter->filterZip()) {
                    echo "<div class='row'>
                             <div class='col-5'>
                                  <small class='text-muted ml-3 warning'>! Field must not be empty. Must be at most 5 digits long</small>
                             </div>
                          </div>";
                }
            }
            ?>
            <br>
            <div class="col-auto">
                <label class="custom-control custom-checkbox mb-2 mr-sm-2 mb-sm-0">
                    <input type="checkbox" class="custom-control-input">
                    <span class="custom-control-indicator"></span>
                    <span class="custom-control-description">Receive mails about news and special offers</span>
                </label>
            </div>
            <br>
            <div class="row">
                <div class="col-8 d-flex flex-row">
                    <div class="col-auto">
                        <label class="col-form-label">Additional Information</label>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-8 d-flex flex-row">
                    <div class="col-5">
                        <textarea class="form-control" id="txtarea" rows="5" name="additionalInfo"></textarea>
                    </div>
                </div>
            </div>
            <br>
            <div class="row">
                <div class="col-8 d-flex flex-row">
                    <input class="btn btn-primary col-2 ml-3" type="submit" name="submit" value="Submit">
                </div>
            </div>
        </fieldset>
    </form>
</div>
<script src="js/script.js"></script>
</body>

</html>

2 个答案:

答案 0 :(得分:1)

有几个问题(请参阅注释),但这里的主要问题是您调用函数的顺序。

单个过滤器功能嵌入在表单本身中,因此在此行后称为

$filter->insertAll();

所以实际上数组仍然是空的,函数试图插入空值。

您需要重新排列,以便在尝试插入值之前调用各个函数。

答案 1 :(得分:1)

你的第一个问题是:

class Filtering {
    ...
    var $connection;

    public function connect() {

        $this->connection= mysqli_connect($this->DBHostName, $this->DBUserName, $this->DBPassword, $this->DBName);

您会看到$this->表示查看此变量的当前类,因此数据库连接正在寻找:

class Filtering {
    var $connection = "something"; // var is optional and not really needed.
    $DBHostName = "host";
    $DBUserName = "user"; 
    $DBPassword = "pass"; 
   // Etc.

您尚未显示已设置这些连接详细信息,因此您的脚本永远不会连接到数据库。

接下来,您全局用法。作为referenced by tereško,使用global是不明智的,而您在此代码中根本不需要

您正在引用已在该类中建立的值;所以简单地使用魔术$this->功能来调用它(令人惊讶的是我找不到专门用于$this 的任何合适的参考材料),所以你不要使用虚拟对象但是而是在课堂上保持整洁。

示例:

  public function filterFirstName() {
        $this->filtered_inputs['firstName'] = "something";

您要求的问题:

下一步;您var_dump($this->filtered_inputs);未输出数据的问题,您在调用之前调用并转储变量

阅读您的表单/ HTML代码;您在页面后面的 之前调用$filter->insertAll(); ,调用实际将任何值保存到变量的filterFirstName()函数。

将表单提交到表单所来自的同一页面是不好的形式;如果我刷新页面,则$_POST ed数据每次都会重新提交。 (此代码库还存在许多其他问题)

此外,您似乎没有任何代码将数据发送到MySQL数据库,也许您只是没有显示它,但您执行拥有SQL 字符串$query)但你应该使用这个字符串:

 $this->connection->query($query);

这是上面的一个非常基本的示例,您可以highly recommended探索 - 并使用 - Prepared Statements

简化您的代码:

改进是一项不断的努力,您的问题似乎是您正在为表单提供给PHP的每个输入编写hardcode;废料;这是重复和浪费的。

尝试使用foreach来浏览an array中的每个元素:

public function processFilter() {
    $check = false;

    foreach($_POST as $key=>$value){
        if (strlen($_POST[$key]) == 0 && $key == 'firstName') {
            $check = false;
        } 
       else {
            $this->filtered_inputs[$key] = filter_input(INPUT_POST, $key, FILTER_SANITIZE_STRING);
            $check = true;
        }
    }
    unset($key,$value);

    return $check;
}

这段代码可以进一步降低并提高效率,但我想给你一个起点;现在,此函数将使用每个POST语句无限填充->filtered_inputs数组,并检查firstName键是否为零长度字符串。