简体   繁体   English

使用PHP将数据从HTML表单传递到MySQL数据库

[英]Pass Data from HTML Form to MySQL Database using PHP

I'm trying to insert some data into a mysql database I created on my localhost (using wamp server). 我试图将一些数据插入到我在本地主机上创建的mysql数据库中(使用wamp服务器)。 But it's not inserting any data into the table. 但这不是在表中插入任何数据。 It doesn't shows any error massages either. 它也没有显示任何错误消息。 In the other hand, it shows the echos I made. 另一方面,它显示了我所做的回声。 Please help me out. 请帮帮我。 I can't type all the code in one page like this question as I'm redirecting to this page from an intermediate php file. 我无法像这样的问题一样在一页中键入所有代码,因为我是从中间php文件重定向到此页面的。 Here is the code I wrote. 这是我写的代码。

<?php
    $con = mysqli_connect("localhost", "root", "11111", "CAR_PARKING_SYSTEM");

    $D_License_No = $_POST['D_License_No'];
    $V_License_No = $_POST['V_License_No'];
    $V_Type = $_POST['vehicle_type'];

    if(!strcmp($V_Type, "four_wheel"))
    {
        $charge = 400;
    }
    else
    {
        $charge = 50;
    }
    $query = "INSERT INTO CUSTOMER_VEHICLE (V_License_No, D_License_No, Arrived_Time, Charge) 
            VALUES ('$V_License_No', '$D_License_No', 'date('H:i')', '$charge')";

    echo "<p>Driver License Number " .$D_License_No. "</p>";
    echo "<p>Vehicle License Plate Number " .$V_License_No. "</p>";
?>

Here is a way of handling this query: 这是处理此查询的一种方法:

First we create a database handle. 首先,我们创建一个数据库句柄。 Most people these days use the object oriented way of doing it, so we will use it here. 如今,大多数人都使用面向对象的方式进行操作,因此我们将在这里使用它。

$mysqli = new MySQLi('localhost', 'root', '11111', 'CAR_PARKING_SYSTEM');

Then we define the SQL query. 然后我们定义SQL查询。 The question marks ( ? ) are place holders for where the values will be placed. 问号( ? )是放置值的占位符。

$sql = <<< SQL
    INSERT INTO `CUSTOMER_VEHICLE` (
        `V_License_No`,
        `D_License_No`,
        `Arrived_Time`,
        `Charge`
    ) VALUES (
        ?, ?, ?, ?
    );
SQL;

In order to fill out the place holders we need to prepare the query. 为了填写占位符,我们需要准备查询。 This is called a "prepared statement" (or "stmt" for short). 这称为“准备好的声明” (或简称为“ stmt”)。

Prepared statements are sent to the database, which checks it for errors and also optimizes it so that consecutive calls to the execute() method are performed faster. 将准备好的语句发送到数据库,该语句将检查它是否存在错误并对其进行优化,从而可以更快地连续execute()方法。 In this case, however, we are mostly just using a prepared statement in order to avoid malicious input from affecting the query. 但是,在这种情况下,我们主要只是使用准备好的语句,以避免恶意输入影响查询。

$stmt = $mysqli->prepare($sql);
if ($stmt === false) {
    die('Could not prepare SQL: '.$mysqli->error);
}

Before we can use the prepared statement we have to have some way of putting values into the place holders. 在使用准备好的语句之前,我们必须有一些将值放入占位符的方法。 We do that by binding some variables to the place holders. 我们通过一些变量绑定到占位符来做到这一点。 In this case we are binding the variables $vLicense , $dLicense , $arrived and $charge to the place holders. 在这种情况下,我们将变量$vLicense$dLicense$arrived$charge绑定到占位符。 The variables names can be anything. 变量名称可以是任何东西。

$ok = $stmt->bind_param('sssi', $vLicense, $dLicense, $arrived, $charge);
if ($ok === false) {
    die('Could not bind params: '.$stmt->error);
}

Now that we have bound some variables to the place holders we can start setting their values. 现在我们已经将一些变量绑定到了占位符,我们可以开始设置它们的值了。 In this case we are using the filter_input() function to sanitize the input of some variables. 在这种情况下,我们使用filter_input()函数filter_input()某些变量的输入。 The $charge variable is set to one of two values depending on what vehicle type we are dealing with. $charge变量设置为两个值之一,具体取决于我们要处理的车辆类型。

$vLicense = filter_input(INPUT_POST, 'V_License_No', FILTER_SANITIZE_STRING);
$dLicense = filter_input(INPUT_POST, 'D_License_No', FILTER_SANITIZE_STRING);
$arrived  = date('H:i');
if (isset($_POST['vehicle_type']) && $_POST['vehicle_type'] === 'four_wheel') {
    $charge = 50;
} else {
    $charge = 400;
}

The values are set and now we can execute the statement. 设置了值,现在我们可以执行该语句了。 That is done simply by calling the statement's execute() method: 只需调用语句的execute()方法即可完成:

if ($stmt->execute() === false) {
    die('Could not execute query: '.$stmt->error);
} else {
    echo '<p>Query executed successfully!</p>';
}
$stmt->close();

I encourage you to read about the MySQLi documentation and the filter extension . 我鼓励您阅读有关MySQLi文档过滤器扩展的信息

The full code can be found here . 完整的代码可以在这里找到

You are not inserting anything, $query is just an string, you need to pass the query to the mysqli function, also, is better if you check and avoid sql injection. 您不插入任何内容, $query只是一个字符串,您需要将查询传递给mysqli函数,如果您检查并避免sql注入,则更好。 Your code should be something like this: 您的代码应如下所示:

$mysqli = new mysqli("localhost", "root", "11111", "CAR_PARKING_SYSTEM");

$D_License_No = $_POST['D_License_No'];
$V_License_No = $_POST['V_License_No'];
$V_Type = $_POST['vehicle_type'];
$charge = !strcmp($V_Type, "four_wheel")?400:50;

$query = "insert into CUSTOMER_VEHICLE 
    (`V_License_No`, `D_License_No`, `Arrived_Time`, `Charge`) 
    values(? , ?, ?, ?)"
$stmt = $mysqli->prepare($query);
$stmt->bind_param("ssss",$V_License_No, $D_License_No, date('H:i'), $charge);
if($stmt->execute()){
    //success inserted
    $stmt->close();
    echo "<p>Driver License Number " .$D_License_No. "</p>";
    echo "<p>Vehicle License Plate Number " .$V_License_No. "</p>";
}
else{
    $error = $mysqli->error;
}

您忘记运行查询:

$result = mysqli_query($query);

you need to use some debugging to get some errors s oyou can investergate futher this is how i would of built said script 您需要使用一些调试程序来获取一些错误,以便您进一步投资,这就是我要构建上述脚本的方式

<?php
try 
{
    $con = mysqli_connect("localhost", "root", "11111", "CAR_PARKING_SYSTEM");

if (empty($con))
{
echo "Mysql failed".mysqli_error();
die; // should always kill script if you dont need it to continue
}

    $D_License_No = $_POST['D_License_No'];
    $V_License_No = $_POST['V_License_No'];
    $V_Type = $_POST['vehicle_type'];

    if(!strcmp($V_Type, "four_wheel"))
    {
        $charge = 400;
    }
    else
    {
        $charge = 50;
    }
    $query = "INSERT INTO CUSTOMER_VEHICLE (V_License_No, D_License_No, Arrived_Time, Charge) 
            VALUES ('$V_License_No', '$D_License_No', 'date('H:i')', '$charge')";

    echo "<p>Driver License Number " .$D_License_No. "</p>";
    echo "<p>Vehicle License Plate Number " .$V_License_No. "</p>";
}
catch (Exception $e)
{
echo $e;
}
?>

im sorry if i have some syntax off i am on a train and not the best place to code :P 对不起,如果我有一些语法问题,我在火车上,而不是最好的编码地方:P

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

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