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

标签 php mysql sql

我正在使用 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/

相关文章:

php - 将一个元素推送到php中多维数组中的每个数组?

php - 存储长文本片段

php - Mysql更新查询更新功能

c# - 是否需要联合查询

php - 从命令行调用 PHP 函数

php - 如何检测模型事件中的强制删除

php - 检查购物车中是否包含多个强制产品类别之一

sql - MySQL 条件 SELECT 语句

sql - 在 SELECT 查询中组合 SQL Server 中的日期和时间

mysql - 从 GROUP_BY 查询中优化 COUNT(*)