我正在使用 PHP 将文本区域输入指向 MySQL 数据库。现在它抛出以下错误:“错误:您的 SQL 语法中有错误;请检查与您的 MySQL 服务器版本相对应的手册,以获取在第 3 行 '' 附近使用的正确语法”
在我发布的代码中,以“mysql_select_db("main_data0",$con);”开头的 block 对于 html 中的每个文本区域,都会重复以“('$_POST[textarea1]'”;”结尾。
我尝试切换到“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.
}
?>
最佳答案
我给你推荐一个PDO connection到数据库。对于初学者来说更容易,而且更安全。
$servername = "localhost";
$username = "username";
$password = "password";
$databasename = "databasename";
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'])){
$postdata = $_POST['textarea1']; // here i declare post value to the $postdata variable
$NewRecord = $conn->prepare("INSERT INTO info0 (textarea1) VALUES (?)"); // here is my mysql statement
$NewRecord->execute([$postdata]); // here i use $postdata variable. The ? mark represent this.
}
多个
if(isset($_POST['textarea_1'], $_POST['textarea_2'], $_POST['textarea_3'])){
$postdata_1 = $_POST['textarea_1']; // here i declare post value to the $postdata variable
$postdata_2 = $_POST['textarea_2']; // here i declare post value to the $postdata variable
$postdata_3 = $_POST['textarea_3']; // here i declare post value to the $postdata variable
$NewRecord = $conn->prepare("INSERT INTO info0 (textarea1, textarea2, textarea3) VALUES (?,?,?)"); // here is my mysql statement
$NewRecord->execute([$postdata_1, $postdata_2, $postdata_3]); // here i use $postdata variable. Every ? mark represent one variable.
}
关于php - 如何使用 PHP 将 HTML 文档中的文本区域数据发送到 MySQL 数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57734841/