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

时间:2015-11-30 12:42:38

标签: php arrays oop multidimensional-array mysqli

这个类在单个数组上工作得很好,但是当我添加多维数组时,不要工作。我创建自动获取列名和值的类。这是演示代码 阵列就是这样的。

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

以下是此代码的用法

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

阵 (

[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

) 我是oop概念的新手请建议我改进。谢谢所有人

2 个答案:

答案 0 :(得分:0)

通常,允许用户直接构建对DB的查询是一个坏主意。

根据您的情况 - 在方法开头form_values添加代码:

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

答案 1 :(得分:0)

雅虎......我自己就是这里的解决方案,希望有人能提供帮助。

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();

}       
}

用法

$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);