PHP:数组中的错误?

时间:2014-08-12 06:14:02

标签: php arrays

这是我的functions.php文件,运行时遇到错误。该文件的源代码是:

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

错误是:

  

解析错误:语法错误,意外&#39 ;;&#39;,期待&#39;)&#39;在/home/appfesti/public_html/functions.php第30行

第30行包含:

$sqlVars = array(

任何人都可以帮我解决这个问题吗?任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:3)

数组值必须以逗号分隔:

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