• <bdo id='IAXfM'></bdo><ul id='IAXfM'></ul>
  • <i id='IAXfM'><tr id='IAXfM'><dt id='IAXfM'><q id='IAXfM'><span id='IAXfM'><b id='IAXfM'><form id='IAXfM'><ins id='IAXfM'></ins><ul id='IAXfM'></ul><sub id='IAXfM'></sub></form><legend id='IAXfM'></legend><bdo id='IAXfM'><pre id='IAXfM'><center id='IAXfM'></center></pre></bdo></b><th id='IAXfM'></th></span></q></dt></tr></i><div id='IAXfM'><tfoot id='IAXfM'></tfoot><dl id='IAXfM'><fieldset id='IAXfM'></fieldset></dl></div>

        <tfoot id='IAXfM'></tfoot>

        <small id='IAXfM'></small><noframes id='IAXfM'>

      1. <legend id='IAXfM'><style id='IAXfM'><dir id='IAXfM'><q id='IAXfM'></q></dir></style></legend>

        带数组的准备语句

        时间:2023-07-30
          <tbody id='VyQSo'></tbody>

        <i id='VyQSo'><tr id='VyQSo'><dt id='VyQSo'><q id='VyQSo'><span id='VyQSo'><b id='VyQSo'><form id='VyQSo'><ins id='VyQSo'></ins><ul id='VyQSo'></ul><sub id='VyQSo'></sub></form><legend id='VyQSo'></legend><bdo id='VyQSo'><pre id='VyQSo'><center id='VyQSo'></center></pre></bdo></b><th id='VyQSo'></th></span></q></dt></tr></i><div id='VyQSo'><tfoot id='VyQSo'></tfoot><dl id='VyQSo'><fieldset id='VyQSo'></fieldset></dl></div>

              <bdo id='VyQSo'></bdo><ul id='VyQSo'></ul>

                <small id='VyQSo'></small><noframes id='VyQSo'>

                <tfoot id='VyQSo'></tfoot>
                1. <legend id='VyQSo'><style id='VyQSo'><dir id='VyQSo'><q id='VyQSo'></q></dir></style></legend>
                  本文介绍了带数组的准备语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有一个函数可以做一个简单的插入,但我试图通过传递一个数组来使该方法更加健壮.这是我传递给它的数组:

                  I have a function to do a simple insert, but am trying to make the method more robust by passing an array. And this is the array I pass into it:

                          $form_data = array(
                          "sort_order"=>$_POST['sort_order'],
                          "name"=>$_POST['page_name'],
                          "text"=>$_POST['page_text'],
                          "image"=>$_POST['page_image'],
                          "meta_desc"=>$_POST['meta_desc'],
                          "meta_kw"=>$_POST['meta_kw'],
                          "meta_author"=>$_POST['meta_author'],
                          "image_thumb"=>"NULL",
                      );
                  

                  这是函数代码:

                  public function insert_data($array){
                          $keys = array();
                          $values = array();
                          
                          foreach($array as $k => $v){
                              $keys[] = $k;
                              if(!empty($v)){
                                  $values[] = $v;
                              } else {
                                  $values[] = "NULL";
                              }
                          }
                  
                          $stmt = self::$mysqli->stmt_init();
                          $query = "INSERT INTO `".DB_TABLE_PAGES."` (".implode(",",$keys).") VALUES (?,?,?,?,?,?,?,?)";
                          
                          
                          $stmt->prepare($query);
                          $stmt->bind_param('ssssssss',implode(",",$values));
                  
                          //$stmt->execute();
                      }
                  

                  但我收到此错误:

                  类型定义字符串中的元素数与绑定变量数不匹配.

                  Number of elements in type definition string doesn't match number of bind variables.

                  我知道问题出在哪里,但我不明白如何实现它.

                  I know what the problem is, but I don't understand how I can achieve it.

                  推荐答案

                  试试这个:

                  public function insert_data($array){
                      $placeholders = array_fill(0, count($array), '?');
                  
                      $keys = $values = array();
                      foreach($array as $k => $v) {
                          $keys[] = $k;
                          $values[] = !empty($v) ? $v : null;
                      }
                  
                      $stmt = self::$mysqli->stmt_init();
                      $query = 'INSERT INTO `'.DB_TABLE_PAGES.'` '.
                               '('.implode(',', $keys).') VALUES '.
                               '('.implode(',', $placeholders).')';
                      $stmt->prepare($query);
                  
                      call_user_func_array(
                          array($stmt, 'bind_param'), 
                          array_merge(
                              array(str_repeat('s', count($values))),
                              $values
                          )
                      );
                  
                      $stmt->execute();
                  }
                  

                  或者更好的是,改用 PDO:

                  Or better yet, use PDO instead:

                  public function insert_data($array){
                      $placeholders = array_fill(0, count($array), '?');
                  
                      $keys = $values = array();
                      foreach($array as $k => $v){
                          $keys[] = $k;
                          $values[] = !empty($v) ? $v : null;
                      }
                  
                      // assuming the PDO instance is $pdo
                      $query = 'INSERT INTO `'.DB_TABLE_PAGES.'` '.
                               '('.implode(',', $keys).') VALUES '.
                               '('.implode(',', $placeholders).')';
                      $stmt = $pdo->prepare($query);
                  
                      $stmt->execute($values);
                  }
                  

                  注意:我使用了 null 常量,因为 "NULL" 字符串将被转义为字符串(而不是空值).

                  Note: I've used the null constant because the "NULL" string will be escaped as a string (not as a null value).

                  这篇关于带数组的准备语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何在 CentOS 上配置 php 以启用 pdo 并包含 mysqli 下一篇:utf 8 - PHP 和 MySQLi UTF8

                  相关文章

                  最新文章

                  <tfoot id='OXSIh'></tfoot>
                    <bdo id='OXSIh'></bdo><ul id='OXSIh'></ul>

                2. <small id='OXSIh'></small><noframes id='OXSIh'>

                  <i id='OXSIh'><tr id='OXSIh'><dt id='OXSIh'><q id='OXSIh'><span id='OXSIh'><b id='OXSIh'><form id='OXSIh'><ins id='OXSIh'></ins><ul id='OXSIh'></ul><sub id='OXSIh'></sub></form><legend id='OXSIh'></legend><bdo id='OXSIh'><pre id='OXSIh'><center id='OXSIh'></center></pre></bdo></b><th id='OXSIh'></th></span></q></dt></tr></i><div id='OXSIh'><tfoot id='OXSIh'></tfoot><dl id='OXSIh'><fieldset id='OXSIh'></fieldset></dl></div>

                  1. <legend id='OXSIh'><style id='OXSIh'><dir id='OXSIh'><q id='OXSIh'></q></dir></style></legend>