0

It looks like all of my code is correct, but when I enter text into all the text areas on my site and hit submit, it shows this error instead of redirecting to the "thank you for your submission page" and posting the data: "Parse error: syntax error, unexpected '[', expecting ')' in /hermes/bosnaweb18a/b2811/dom.standridgen77573/public_html/data.php on line 42"

Line 42 in the code would be the one starting with "$NewRecord->execute".

I was originally using MySQLi to send the form data, but I was advised here that it would be easier for a beginner to use PDO.

    <?php
    $servername = "mysqlserver";
    $username = "myusername";
    $password = "mypassword";
    $databasename = "mydatabase";

    try {
        $conn = new PDO("mysql:host=$servername;dbname=$databasename",                         
    $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'];
        $postdata_3 = $_POST['textarea3'];
        $postdata_4 = $_POST['textarea4']; 
        $postdata_5 = $_POST['textarea5'];
        $postdata_6 = $_POST['textarea6'];
        $postdata_7 = $_POST['textarea7'];
        $postdata_8 = $_POST['textarea8'];
        $postdata_9 = $_POST['textarea9'];


        $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.
    }
    ?>
  • There's no syntax error in the code you have posted. – Nick Sep 04 '19 at 03:48
  • What version of PHP are you using? – Phil Sep 04 '19 at 04:10
  • Nick, I know there is no syntax error. But it is showing a syntax error. That is why I’m posting this question.z – natstandridge Sep 04 '19 at 04:11
  • Phil- the php version on my website is 5.3.29 – natstandridge Sep 04 '19 at 04:12
  • If prior to PHP 5.4, you cannot use the short array syntax and instead must use `$NewRecord->execute(array($postdata_1, ...))`. See https://www.php.net/manual/language.types.array.php#language.types.array.syntax Also 5.3 is ancient and [unsupported](https://www.php.net/supported-versions.php). I highly recommend upgrading if possible – Phil Sep 04 '19 at 04:12
  • I'm gonna try that now, thanks for the insight! I'll check if it works and comment back if that solves it – natstandridge Sep 04 '19 at 04:27

0 Answers0