0

I am using PHP to point the text area input to MySQL database. 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"

In the code I posted, the block starting with "mysql_select_db("main_data0",$con);" and ending in "('$_POST[textarea1]'";" is repeated for each text area that I have in my html.

I tried switching to "mysqli" instead of "mysql_connect", but this only caused it to show WAY more errors.

<?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.
}
?>

1 Answers1

1

I'll recommend you a PDO connection to the database. 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.
}
Nemanja Jeremic
  • 334
  • 4
  • 18