将数组转换为字符串错误

时间:2014-02-10 17:02:49

标签: php codeigniter

即时通讯使用codeigniter和编码php,我的问题是我有一个错误.. 我试图存储我的表格中的数据,但错误阻碍了我..

 A PHP Error was encountered

Severity: Notice

Message: Array to string conversion

Filename: mysql/mysql_driver.php

Line Number: 589

和数据库错误:

Error Number: 1064

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Array' at line 1

INSERT INTO `grade1` () VALUES ('7'), Array

Filename: C:\wamp\www\thisisit\system\database\DB_driver.php

Line Number: 330

这是我的观点:

    <form action="<?php echo site_url('contrl/firstgrading') ?>" method="post">
    <strong><input type="hidden" id="sid[]" name="sid[]" value="<?php echo $sid ?>"><?php echo $fname." ".$mname." ".$lname ?></strong><br>
        <?php                
                          $this->load->database();          
                          $query = $this->db->query("SELECT subid,subjectname FROM subjects ");           
                          foreach ($query->result_array() as $row)
                           {
                        ?>
                            <tr>
                                <td><input type="hidden" id="subid[]" name="subid[]" value="<?php echo $row['subid'] ?>"><?php echo $row['subjectname']?></td>                        
                                <td align="center"> <input class="form-control" id="GFirst[]" name="GFirst[]" type="text"></td>
                                <td align="center"> <input class="form-control" id="GSecond[]" name="GSecond[]" type="text"></td>
                                <td align="center"> <input class="form-control" id="GThird[]" name="GThird[]" type="text"></td>
                                <td align="center"> <input class="form-control" id="GFourth[]" name="GFourth[]" type="text"></td>
                                <td align="center"> <input class="form-control" id="GFinal[]" name="GFinal[]" type="text"></td>                                                                                                            
                            </tr>                      
                        <?php
                          }
                        ?>
</form>

这是我的控制者:

    $temp = array();
            $temp = $this->input->post(NULL, TRUE);        
            for($i = 0 ; $i < count($this->input->post('subid')); $i++){            
                $temp[] = array(
                        'gradeid'           => '0',
                        'firstgrading'      => $temp['GFirst'][$i],
                        'secondgrading'     => $temp['GSecond'][$i],
                        'thirdgrading'      => $temp['GThird'][$i],
                        'fourthgrading'     => $temp['GFourth'][$i],
                        'finalgrading'      => $temp['GFinal'][$i],
                        'subidLink'         => $temp['subid'][$i],
                        'studidLink'        => $temp['sid']
                    );
            };
            $this->load->model('model');
            $this->model->grade($temp);            

这是我的模特:

function grade($array = NULL)
        {
            if(!isset($array) || !is_array($array)) return FALSE;
            $confirm = $this->db->insert_batch('grade1',$array);
            if($confirm)
            {
                echo "Success";
            }
            else
            {
                echo "Fail";
            }
        }

2 个答案:

答案 0 :(得分:3)

对于在CodeIgniter 2.1.4之前使用Active Record进行批量插入,应该记住以下几点 -

Keys for each data array should be same, whatever the values. 

例如:

$data = array(
array('A'=>'col-11', 'B'=>'col-12', 'C'=>'col-13', 'D'=>'col-14'),
array('A'=>'col-21', 'B'=>'col-22', 'C'=>'col-23', 'D'=>'col-24'),
array('A'=>'col-31', 'B'=>'col-32', 'C'=>'col-33', 'D'=>'col-34')
);
$this->db->insert_batch('table',$data);

但它不能像下面那样不同:

$data = array(
array('A'=>'col-11', 'B'=>'col-12', 'C'=>'col-13', 'D'=>'col-14'),
array('A'=>'col-21', 'B'=>'col-22', 'C'=>'col-23', 'E'=>'col-24'),
array('A'=>'col-31', 'B'=>'col-32', 'D'=>'col-33', 'F'=>'col-34')
);
$this->db->insert_batch('table',$data);

答案 1 :(得分:0)

希望您在模型中插入。

因此,请在模型等级()

中更改以下代码
$this->db->insert_batch("Table_name",$data);