INSERT上的“函数名必须是字符串” - mysqli PHP

时间:2017-08-03 00:51:02

标签: php ajax mysqli syntax-error

我已经困惑了一段时间。这是令人费解的,因为上面几行非常相似的查询工作正常。我对mysqli很新,所以我可能会遗漏一些非常基本的东西。 连接设置如下:

   Class dbObj{
      /* Database connection start */
      var $servername = "myserver";
      var $username = "myusername";
      var $password = "mypassword";
      var $dbname = "mydb";
      var $conn;

      function getConnstring() {
          $con = mysqli_connect($this->servername, $this->username, $this->password, $this->dbname) or die("Connection failed: " . mysqli_connect_error());

          /* check connection */
          if (mysqli_connect_errno()) {
              printf("Connect failed: %s\n", mysqli_connect_error());
              exit();
          } else {
              $this->conn = $con;
          }
          return $this->conn;
      }
    }

然后,在Ajax处理文件中:

include_once({the connection file above});
$db = new dbObj();
$connString =  $db->getConnstring();
$params = $_REQUEST;
$action = isset($params['action']) != '' ? $params['action'] : '';
$NeedsCls = new Needs($connString);

然后,在一个名为“需要”的类中:

   protected $conn;
   protected $data = array();
   function __construct($connString)
  {
       $this->conn = $connString;
   }
   function insertNeeds($params)
   {
       $ExpDate = $params[Expire];
       $sql = "INSERT INTO `pNeeds` (PubCode, Title, Description, Keywords, Expire) VALUES('".$_SESSION["PubCode"]."','".$params["Title"]."','".$params["Description"]."','".$params[NeedsTags]."','".$ExpDate."'); ";
       echo $result = mysqli_query($this->conn, $sql) or die("Error - Failed inserting Needs data");
     $Record = $db->insert_id;
     $KW = explode(';',$params[NeedsTags]);
     $KWCount = count($KW);
     for($x=0;$x<$KWCount;$x++)
     {
        $Keyword = $KW[$x];
        $sql = "INSERT INTO `Keywords` (Keyword, PubCode, Table, Record, Expire) VALUES ('".$Keyword."','".$_SESSION["PubCode"]."','pNeeds','".$Record."','".$ExpDate."'); ";
        echo $result = mysqli_query($this->conn,$sql) or die("Error - Keywords not saved<br />".$mysqli_error());
     }
  }

第一个查询工作正常。第二个失败,错误“函数名称必须是字符串”。我已经验证了进入Ajax代码的数据是否正确。我似乎没有错过一些愚蠢的东西。 (着名的遗言)错误信息对我没有意义。 StackOverflow和其他地方的类似帖子在这个例子中似乎并不相关。

1 个答案:

答案 0 :(得分:2)

看起来你只是抓错了$ params:

$ExpDate = $params[Expire];
$params[NeedsTags];

应该是:

$ExpDate = $params['Expire'];
$params['NeedsTags'];

修改

您的实际错误来自:

$mysqli_error()

删除$

修改 RationalRabbit:为了避免混淆任何人,我应该提到整个语法都是错误的。使用面向对象的mysqli,错误语法应该是

 db->error

OR

mysqli($this->conn)