【发布时间】:2020-01-04 04:28:17
【问题描述】:
我正在使用 PHP 将文本区域输入指向 MySQL 数据库。现在它抛出以下错误:“错误:您的 SQL 语法有错误;请查看与您的 MySQL 服务器版本相对应的手册,以在第 3 行的 '' 附近使用正确的语法”
在我发布的代码中,以 "mysql_select_db("main_data0",$con);" 开头的块并以 "('$_POST[textarea1]'";" 结尾为我在我的 html 中的每个文本区域重复。
我尝试切换到“mysqli”而不是“mysql_connect”,但这只会导致它显示更多错误。
<?php
$servername = "standridgen77573.domaincommysql.com";
$username = "nat_0g";
$password = "Lens981#bent";
$databasename = "main_data0";
try {
$conn = new PDO("mysql:host=$servername;dbname=$databasenam", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
if(isset($_POST['textarea1'], $_POST['textarea2'], $_POST['textarea3'], $_POST['textarea4'], $_POST['textarea5'], $_POST['textarea6'], $_POST['textarea7'], $_POST['textarea8'], $_POST['textarea9'])){
$postdata_1 = $_POST['textarea1']; // here i declare post value to the $postdata variable
$postdata_2 = $_POST['textarea2']; // here i declare post value to the $postdata variable
$postdata_3 = $_POST['textarea3']; // here i declare post value to the $postdata variable
$postdata_4 = $_POST['textarea4']; // here i declare post value to the $postdata variable
$postdata_5 = $_POST['textarea5']; // here i declare post value to the $postdata variable
$postdata_6 = $_POST['textarea6']; // here i declare post value to the $postdata variable
$postdata_7 = $_POST['textarea7']; // here i declare post value to the $postdata variable
$postdata_8 = $_POST['textarea8']; // here i declare post value to the $postdata variable
$postdata_9 = $_POST['textarea9']; // here i declare post value to the $postdata variable
$NewRecord = $conn->prepare("INSERT INTO info0 (textarea1, textarea2, textarea3, textarea4, textarea5, textarea6, textarea7, textarea8, textarea9) VALUES (?,?,?,?,?,?,?,?,?)"); // here is my mysql statement
$NewRecord->execute([$postdata_1, $postdata_2, $postdata_3, $postdata_4, $postdata_5, $postdata_6, $postdata_7, $postdata_8, $postdata_9]); // here i use $postdata variable. Every ? mark represent one variable.
}
?>
【问题讨论】:
-
你错过了
) -
$sql="INSERT INTO info0 (textarea1) VALUES ('{$_POST['textarea1']}')" -
防止SQL注入! stackoverflow.com/questions/129677/…
-
伙计,我不知道我是怎么错过的。会尝试解决这个问题,如果这不起作用,我会尝试下面建议的 PDO 连接。