在PHP中向MySQL插入多行(单个查询):Prepare-Execute与Prepare-Bind-Execute

时间:2022-09-25 16:43:32

I'm writing some PHP to accept an array of numbers and names in POST and insert them into a MySQL table (named Contacts_table) Here's the version that works fine without any error:

我正在编写一些PHP来接受POST中的数字和名称数组,并将它们插入MySQL表(名为Contacts_table)这里的版本工作正常,没有任何错误:

<?php

  // Includes
  require_once 'Admin/Connector.php';

  // Test if payload exists
  if($_POST){

  // Read payload into arrays
      $ar = 0;
      foreach($_POST as $entry){
          $namenum = explode(',', $entry);
          $names[$ar] = $namenum[1];
          $numbers[$ar] = $namenum[0];
          $ar += 1;
      }
      $namenum = NULL;

  // Build SQL query
      $sql = 'INSERT INTO Contact_table (NAME, PHONE) VALUES ';
      $insertQuery = array();
      $insertData = array();
      $n = 0;
      foreach ($numbers as $num) {
        $insertQuery[] = '(?, ?)';
        $insertData[] = $names[$n];
        $insertData[] = $num;
        $n++;
      }
      $sql .= implode(', ', $insertQuery);
      $sql .= ' ON DUPLICATE KEY UPDATE name = COALESCE(VALUES(name), name);';
      $n = NULL;
      $num = NULL;

  // Connect to MySQL database
      $connect = dbconn(PROJHOST,PROJDB,PROJDBUSER,PROJDBPWD);

  // Execute SQL query
      $query = $connect->prepare($sql);
      $query->execute($insertData);
      $insertQuery = NULL;
      $insertData = NULL;
      $sql = NULL;
      $query = NULL;

  // Close connection to MySQL database
      $connect = NULL;

  }

?>

However, as you can see, I'm not using the bindParam() function here and just feeding the values directly to the execute() function. Many have recommended that I use bindParam() instead for server performance gains. Is it true or I am better off with this program as it stands? I did try writing and running a version of the above code using bindParam:

但是,正如您所看到的,我不是在这里使用bindParam()函数,只是将值直接提供给execute()函数。许多人建议我使用bindParam()代替服务器性能提升。它是真的还是我现在更喜欢这个项目?我尝试使用bindParam编写并运行上述代码的版本:

<?php

  // Includes
  require_once 'Admin/Connector.php';

  // Test if payload exists
  if($_POST){

  // Read payload into arrays
      $ar = 0;
      foreach($_POST as $entry){
          $namenum = explode(',', $entry);
          $names[$ar] = $namenum[1];
          $numbers[$ar] = $namenum[0];
          $ar += 1;
      }
      $namenum = NULL;

  // Build SQL query
      $sql = 'INSERT INTO Contact_table (NAME, PHONE) VALUES ';
      $insertQuery = array();
      $insertData = array();
      $n = 0;
      foreach ($numbers as $num) {
        $insertQuery[] = '(?, ?)';
        $insertData[] = $names[$n];
        $insertData[] = $num;
        $n++;
      }
      $sql .= implode(', ', $insertQuery);
      $sql .= ' ON DUPLICATE KEY UPDATE name = COALESCE(VALUES(name), name);';
      $n = NULL;
      $num = NULL;

  // Connect to MySQL database
      $connect = dbconn(PROJHOST,PROJDB,PROJDBUSER,PROJDBPWD);

   // Prepare SQL query 
      $query = $connect->prepare($sql);

  // Bind variables
      foreach($insertData as $key => &$ins) {
          $connect->bindParam($key+1,$ins);
      }

  // Execute SQL query
      $query->execute();
      $insertQuery = NULL;
      $insertData = NULL;
      $sql = NULL;
      $query = NULL;
      $key = NULL;
      $ins = NULL;

  // Close connection to MySQL database
      $connect = NULL;

  }

?>

But this code refuses to run and returns a fatal error – Call to undefined method PDO::bindParam(). What am I doing wrong here? I understand it's possible to write a much simpler code if I include execute() within the loop but that would spawn multiple queries which I want to avoid at all costs. My goal is a single query no matter what.

但是这段代码拒绝运行并返回致命错误 - 调用未定义的方法PDO :: bindParam()。我在这做错了什么?我理解,如果我在循环中包含execute(),那么编写一个更简单的代码是可能的,但这会生成我想要不惜一切代价避免的多个查询。无论如何,我的目标都是单一查询。

1 个解决方案

#1


You can't $connect->bindParam($key+1,$ins);. Because PDO object doesn't have such method. Only PDOStatement has. That is why you've got error message.

你不能$ connect-> bindParam($ key + 1,$ ins);.因为PDO对象没有这样的方法。只有PDOStatement有。这就是为什么你有错误信息。

You should :

你应该 :

 $query->bindValue($key+1,$ins);

And you should use bindValue because if not, all your inserted values will get same value (the last one of $ins before you call execute).

您应该使用bindValue,因为如果没有,所有插入的值将获得相同的值(在调用execute之前$ ins中的最后一个)。

#1


You can't $connect->bindParam($key+1,$ins);. Because PDO object doesn't have such method. Only PDOStatement has. That is why you've got error message.

你不能$ connect-> bindParam($ key + 1,$ ins);.因为PDO对象没有这样的方法。只有PDOStatement有。这就是为什么你有错误信息。

You should :

你应该 :

 $query->bindValue($key+1,$ins);

And you should use bindValue because if not, all your inserted values will get same value (the last one of $ins before you call execute).

您应该使用bindValue,因为如果没有,所有插入的值将获得相同的值(在调用execute之前$ ins中的最后一个)。