mysql没有插入数组

时间:2016-03-03 02:39:01

标签: php mysql arrays insert

我不知道为什么不在db中插入数组的值,我解析数组,但是db中的值没有改变,我手动将一些数据添加到db表中:

public function widget( $args, $instance ) {
        global $wpdb;
        $table_name = $wpdb->prefix . "cbrrate";
        $limit=12;
        $i=$x=0;
        $currency = array();

        $codes = array('USD','EUR');
        //creando la tabla 
        if( $wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name)
        {
                $sql = 'CREATE TABLE ' .$table_name . '(
                hit_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
                code CHAR(3),
                rate FLOAT UNSIGNED,
                PRIMARY KEY  (code) )';

        require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
        dbDelta($sql);

        }

            //initialise an array to hold the values to be inserted
            $sqlParts1=array();
        //loop through the code to create an array with keys as value
            foreach($codes as $code){




                    //add the rate to the sql query
                    $sqlParts1[] = "('$code', ".((float)$currency[0][$code]).") ";

            }


        $sql = "INSERT INTO $table_name (code, rate) VALUES ".implode(', ', $sqlParts1).' ON DUPLICATE KEY UPDATE rate = VALUES(rate)';

        print_r($sql);

数组$currency是:

Array ( 
   [0] => Array ( [USD] => 73.63 [EUR] => 79.97 ) 
   [1] => Array ( [USD] => 74.05 [EUR] => 80.53 ) 
)

0 个答案:

没有答案