因此,我已經完成了一個問答網站的構建,現在正試圖防禦SQL注入,但是CURRENT_DATE有問題。我想將當前的日期與問題插入到數據庫中,但是綁定標記是什麼? 「s」字符串不工作?使用mysqli預處理語句時,CURRENT_DATE的查詢綁定標記是什麼?
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "questions87";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
session_start();
$question = $_POST["question"];
$uname = $_SESSION['username'];
$qa_email =$_SESSION['email'];
// prepare and bind
$stmt = $conn->prepare("INSERT INTO login (username, username, q_date, qa_email) VALUES (?, ?, ?, ?)");
$stmt->bind_param("ssss", $question, $uname, CURRENT_DATE, $qa_email);
$stmt->execute();
if ($stmt) {echo "Thank you ". $uname . " Your question has been submitted " . "<br>";}
else {echo "Error: " . $sql . "<br>" . mysqli_error($conn);}
$stmt->close();
$conn->close();
?>
只是爲了解釋.. PHP知道你傳遞了一個從未定義的常量。如果你想使用像'$ someDate = date('Y-m-d H:i:s',time())'這樣的php函數插入一個日期(時間),''你確實需要使用's'。 – Yolo