我連接到數據庫以加載html表單數據,並且在mysqli查詢中遇到了語法錯誤。我已經嘗試了語法上的變體,但它沒有清除。另一組眼睛將不勝感激。關於mysqli查詢的語法錯誤
<?php
// define variables and set to empty values
$ProjectErr = $ClientErr = $LastNameErr = $DateReceivedErr = "";
$Project = $Client = $LastName = $DateReceived = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["Project"])) {
$ProjectErr = "Project name is required";
} else {
$Project = test_input($_POST["Project"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$Project)) {
$ProjectErr = "Only letters and white space allowed";
}
}
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["Client"])) {
$ClientErr = "Client name is required";
} else {
$Client = test_input($_POST["Client"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$Client)) {
$ClientErr = "Only letters and white space allowed";
}
}
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["LastName"])) {
$LastNameErr = "Tech writer name is required";
} else {
$LastName = test_input($_POST["LastName"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$LastName)) {
$LastNameErr = "Only letters and white space allowed";
}
}
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["DateReceived"])) {
$DateReceivedErr = "The date the project was received is required";
} else {
$DateReceived = test_input($_POST["DateReceived"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$DateReceived)) {
$DateReceivedErr = "Only letters and white space allowed";
}
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<?php
$servername = "localhost";
$username = "oldga740_Tonymm";
$password = "JtAjDm#6";
$dbname = "oldga740_SeniorProject";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//Sending form data to sql db.
mysqli_query($connect,"INSERT INTO Projects (Project, Client, LastName, DateReceived)
VALUES ('$_POST[post_Project]', '$_POST[post_Client]', '$_POST[post_LastName]', '$_POST[post_DateReceived]')";
?>
<h2>New Project</h2>
<p><span class="error">* required field.</span></p>
<form action="send_post.php" method="post">
Project: <input type="text" name="Project" value="<?php echo $Project;?>">
<span class="error">* <?php echo $ProjectErr;?></span>
<br><br>
Client: <input type="text" name="Client" value="<?php echo $Client;?>">
<span class="error">* <?php echo $ClientErr;?></span>
<br><br>
LastName: <input type="text" name="LastName" value="<?php echo $LastName;?>">
<span class="error">* <?php echo $LastNameErr;?></span>
<br><br>
DateReceived: <input type="text" name="DateReceived" value="<?php echo $DateReceived;?>">
<span class="error">* <?php echo $DateReceivedErr;?></span>
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
<?php
echo "<h2>Your Input:</h2>";
echo $Project;
echo "<br>";
echo $Client;
echo "<br>";
echo $LastName;
echo "<br>";
echo $DateReceived;
?>
如果更改代碼以使用參數化查詢和執行,您將A)不會很容易受到SQL注入攻擊,並且B)更好地瞭解您實際執行的SQL是什麼。 – Michael
感謝您的幫助Micheal – Tony
順便說一句,您剛剛發佈了用於數據庫的用戶名和密碼,可能現在想改變它... –