简体   繁体   English

如何使用PHP将HTML文档中的文本区域数据发送到MySQL数据库?

[英]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.

相关问题 在HTML文本区域中显示数据库数据(PHP,MySQL) - Display database data in HTML text area (PHP, MySQL) 如何将数据从 HTML 表单发送到文档? - How do i send data from an HTML form to a document? 如何使用ajax获取文本区域数据(文本区域中的Html文件)我需要将数据发送到Php? Ajax没有发送我的文本区域数据? - How To get Text Area Data (Html File in Text area) using ajax And i need to send data to Php ? Ajax not sending my Text area data? 如何将我的数据库中的发送数据添加到 html 中的文本区域表单 - How to add send data from my database to a text area form in html 如何将数据从KnockOut发送到Php for MySql? - how do I send data from KnockOut to Php for MySql? 如何使用HTML和PHP表单将数据插入数据库? - How do I use HTML & PHP forms to insert data into a database? 从mysql数据库中提取数据时,如何在php字符串中使用多个属性? - How do I use multiple attributes in a php string when pulling data from a mysql database? 如何从php中的mysql数据库正确输出数据 - How do I output data from a mysql database in php correctly 如何从SVG文档获取和发送数据以及如何在PHP中使用? - How to get and send Data from/to an SVG document and use in PHP? 使用PHP将数据从HTML文本字段更新到MySQL数据库 - Updating data from HTML text fields to MySQL database with PHP
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM