[英]Pass Data from HTML Form to MySQL Database using PHP
我試圖將一些數據插入到我在本地主機上創建的mysql數據庫中(使用wamp服務器)。 但這不是在表中插入任何數據。 它也沒有顯示任何錯誤消息。 另一方面,它顯示了我所做的回聲。 請幫幫我。 我無法像這樣的問題一樣在一頁中鍵入所有代碼,因為我是從中間php文件重定向到此頁面的。 這是我寫的代碼。
<?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>";
?>
這是處理此查詢的一種方法:
首先,我們創建一個數據庫句柄。 如今,大多數人都使用面向對象的方式進行操作,因此我們將在這里使用它。
$mysqli = new MySQLi('localhost', 'root', '11111', 'CAR_PARKING_SYSTEM');
然后我們定義SQL查詢。 問號( ?
)是放置值的占位符。
$sql = <<< SQL
INSERT INTO `CUSTOMER_VEHICLE` (
`V_License_No`,
`D_License_No`,
`Arrived_Time`,
`Charge`
) VALUES (
?, ?, ?, ?
);
SQL;
為了填寫占位符,我們需要准備查詢。 這稱為“准備好的聲明” (或簡稱為“ stmt”)。
將准備好的語句發送到數據庫,該語句將檢查它是否存在錯誤並對其進行優化,從而可以更快地連續execute()
方法。 但是,在這種情況下,我們主要只是使用准備好的語句,以避免惡意輸入影響查詢。
$stmt = $mysqli->prepare($sql);
if ($stmt === false) {
die('Could not prepare SQL: '.$mysqli->error);
}
在使用准備好的語句之前,我們必須有一些將值放入占位符的方法。 我們通過將一些變量綁定到占位符來做到這一點。 在這種情況下,我們將變量$vLicense
, $dLicense
, $arrived
和$charge
綁定到占位符。 變量名稱可以是任何東西。
$ok = $stmt->bind_param('sssi', $vLicense, $dLicense, $arrived, $charge);
if ($ok === false) {
die('Could not bind params: '.$stmt->error);
}
現在我們已經將一些變量綁定到了占位符,我們可以開始設置它們的值了。 在這種情況下,我們使用filter_input()
函數filter_input()
某些變量的輸入。 $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;
}
設置了值,現在我們可以執行該語句了。 只需調用語句的execute()
方法即可完成:
if ($stmt->execute() === false) {
die('Could not execute query: '.$stmt->error);
} else {
echo '<p>Query executed successfully!</p>';
}
$stmt->close();
您不插入任何內容, $query
只是一個字符串,您需要將查詢傳遞給mysqli函數,如果您檢查並避免sql注入,則更好。 您的代碼應如下所示:
$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);
您需要使用一些調試程序來獲取一些錯誤,以便您進一步投資,這就是我要構建上述腳本的方式
<?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;
}
?>
對不起,如果我有一些語法問題,我在火車上,而不是最好的編碼地方:P
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.