[英]How do I use PHP to send text area data from an HTML document to MySQL database?
I am using PHP to point the text area input to MySQL database. 我正在使用PHP将文本区域输入指向MySQL数据库。 Right now it is throwing the following error: "Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 3"
现在,它会引发以下错误:“错误:您的SQL语法有错误;请查看与您的MySQL服务器版本相对应的手册,以获取在第3行的''附近使用的正确语法”
In the code I posted, the block starting with "mysql_select_db("main_data0",$con);" 在我发布的代码中,以“ mysql_select_db(“ main_data0”,$ con);“开头的代码块 and ending in "('$_POST[textarea1]'";" is repeated for each text area that I have in my html.
对我在html中的每个文本区域重复并以“(''$ _POST [textarea1]'” ;;”结尾。
I tried switching to "mysqli" instead of "mysql_connect", but this only caused it to show WAY more errors. 我尝试切换到“ 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.
}
?>
I'll recommend you a PDO connection to the database. 我建议您与数据库建立PDO连接 。 It's much easier for beginners, and it's much secured.
对于初学者来说,这要容易得多,而且非常安全。
$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();
}
Then after you connected to the database you can do something like this. 然后,连接到数据库后,您可以执行以下操作。
Single 单
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.
}
Multiple 多
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.
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.