user3696690
user3696690

Reputation: 61

PHP: Errors in array?

This is my functions.php file and I have gotten an error when I run it. The source code of the file is:

<?php

function data_insert($to, $from, $text, $ip1, $ip2, $ip3, $ua) {
  try {

    $db = pdoConnect();

    $query = 
      "INSERT INTO sms (
        recepient,
        sender,
        message,
        ip1,
        ip2,
        ip3,
        ua,
        result
      )
      VALUES (
        :recepient,
        :sender,
        :message,
        :ip1,
        :ip2,
        :ip3,
        :ua,
        :result
      )";

    $sqlVars = array(
      ':recepient' => $to;
      ':sender' => $from;
      ':message' => $text;
      ':ip1' => $ip1;
      ':ip2' => $ip2;
      ':ip3' => $ip3;
      ':ua' => $ua;
      ':result' => $result;
    );

    $stmt = $db->prepare($query); 

    if (!$stmt->execute($sqlVars)){
      // Error: column does not exist
      return false;
    }

    $inserted_id = $db->lastInsertId();

    $stmt = null;

    return $inserted_id;

  } catch (PDOException $e) {
    addAlert("danger", "Oops, looks like our database encountered an error.");
    error_log("Error in " . $e->getFile() . " on line " . $e->getLine() . ": " . $e->getMessage());
    return false;
  } catch (ErrorException $e) {
    addAlert("danger", "Oops, looks like our server might have goofed. If you're an admin, please check the PHP error logs.");
    return false;
  } 
}
?>

The error is:

Parse error: syntax error, unexpected ';', expecting ')' in /home/appfesti/public_html/functions.php on line 30

Line 30 contains:

$sqlVars = array(

Can anyone help me with the fix? Any help would be appreciated!

Upvotes: 0

Views: 146

Answers (1)

Ende Neu
Ende Neu

Reputation: 15783

Array values must be comma separated:

$sqlVars = array(
  ':recepient' => $to,
   ':sender' => $from,
   ':message' => $text,
   ':ip1' => $ip1,
   ':ip2' => $ip2,
   ':ip3' => $ip3,
   ':ua' => $ua,
   ':result' => $result
 );

Upvotes: 3

Related Questions