简体   繁体   English

如何在PHP中使用多维数组创建插入查询

[英]how to create insert query with multidimensional array in php

This class is working perfectly on single array but when i add multidimensional array dont working.i create the class which automatically get column name and values.here is demo code array coming like this. 此类在单个数组上运行完美,但是当我添加多维数组不起作用时,我创建了自动获取列名和值的类。这里是演示代码数组。

class myform{
    public $key;
    public $value;
    public $query;
    public $con;
    public function __construct(){
    $this->con = new mysqli('localhost','root','','form')or die('you are losing your mind');}   
   public function form_values($tablename,$values){
    $columns = implode(", ",array_keys($value));
    $escaped_values = array_map('esc_sql', array_values($value));
    $values  = implode("',' ", $escaped_values);
    foreach($valuee as $key => $valued){       
    $this->query = "INSERT INTO {$tablename} ($columns)VALUES('$valued[$key]')"; 
       } return $this->query;
   }
}

Here is usage of this code 这是此代码的用法

if(isset($_POST['submitt'])){
    $new = new myform;
    $values = $_POST;
    $query_form = $new->form_values('myguests',$values);
}

Array ( 数组(

[objective] => hj
[full_name] => sdad
[email] => dsafds
[phone] => dasfds
[nationality] => adf
[date_of_birth] => adsf
[country] => dsfasd
[website] => asdfds
[address] => sadfdsaf
[my_image_upload_nonce] => 3b17791a4e
[_wp_http_referer] => /cv_builder/
[job_title] => Array
    (
        [0] => asdfds
        [1] => adsfdsaf
    )

[company_name] => Array
    (
        [0] => dasfdaf
        [1] => asfdsafsd
    )

[comp_other_info] => Array
    (
        [0] => adsfdsfdsafdsafasdf
        [1] => adfadfasfdfadsfsdafsadf
    )

[qualification] => Array
    (
        [0] => adsfddsfasdfdaf
    )

[refrence] => References available upon request.
[submitt] => submit

) i am new to oop concept please suggested me to imporve.Thanks to all )我是oop概念的新手,请建议我改善。谢谢大家

Generally its a bad idea to allow users to build directly your queries to DB. 通常,允许用户直接将您的查询构建到数据库是一个坏主意。


By your case - at the begining of method form_values add code: 根据您的情况-在form_values方法的开头添加代码:

foreach($values as $key => $value) {
    if (!is_string($value)) {
       //do some stuff. Remove element from array, or convert it to string
    }
}

Yahoo... I Got it myself here is the solution hope someone would help. Yahoo ...我自己搞定了,这是希望有人提供帮助的解决方案。

class myform{
    public $key;
    public $value;
    public $query;
    public $con;
    public function __construct(){
    $this->con = new mysqli('localhost','root','','data') or die('you are losing your mind');
                                }   

    public function array_implode( $array) {
    if ( ! is_array( $array ) ) return $array;
    $string = array();
    foreach ( $array as $key => $val ) {
        if ( is_array( $val ) )
            $val = implode( ',', $val );
        $string[] = "{$val}";

                                        }
    return $string;

}
   public function form_values($tablename,$values){
//  unset($values['sections']);

    $array = $values;

    echo '<pre>';   
    var_export($dg);
    echo '</pre>';

    $columns = implode(", ",array_keys($values));
    $escaped_values = array_map('esc_sql', array_values($dg));
    $values  = implode("',' ", $escaped_values);
    $this->query = "INSERT INTO {$tablename} ($columns)VALUES($values)"; 
    return $this->query;
   }

public function insert_form($query_form){


    $stmt = $con->prepare($query_form) or trigger_error("ponka");
    $stmt->execute();

}       
}

Usage 用法

$new = new myform;
    $values = $_POST;
    $final_values = $new->array_implode($values);
    $query_form = $new->form_values('wp_cv',$final_values);
    $new->insert_form($query_form);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM