我試圖將我的數據插入到數據庫中。但是我的代碼給了我一個錯誤「你的SQL語法有錯誤;查看與你的MySQL服務器版本相對應的手冊,在第二行'''''附近使用正確的語法'」 我的代碼:將數據插入MYSQL表時出現一些錯誤
$dbhost="localhost";
$dbuser="admin";
$dbpass="password";
$dbname="form-recieve";
$connection = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
if(mysqli_connect_errno()) {
die("Database connection failed: ".
mysqli_connect_errno().
" (" . mysqli_connect_errno() . ")"
);
}
if (isset($_POST['submit'])) {
$Username = $_POST["user_name"];
$Email = $_POST["email"];
$Phone = $_POST["phone"];
$Message = $_POST["message"];
$Thanks = "Hi {$Username}, We will get to you as fast as we can.";
}
$Username = mysqli_real_escape_string($connection, $Username);
$Email = mysqli_real_escape_string($connection, $Email);
$Phone = mysqli_real_escape_string($connection, $Phone);
$Message = mysqli_real_escape_string($connection, $Message);
$query = "INSERT INTO contacted (name, email, phone, message)
VALUES ('{$Username}', {$Email}, {$Phone}, '{$Message}')";
$result = mysqli_query($connection, $query);
if (!$result) {
die("Database Query Failed" . mysqli_error($connection));
}
您的腳本存在[SQL注入攻擊]的風險(http://stackoverflow.com/questions/60174/how-c即使[如果你正在逃避投入,它不安全!](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around -mysql-real-escape-string) 使用[prepared parameterized statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) – RiggsFolly
嗯..我會嘗試 –
我猜想「email」列是文本,因此您需要VALUE子句中的「{$ Email}」。同上'phone' – RiggsFolly