2011-09-25 79 views
0

我想在数据库表的一行上插入下面的php代码(foreach),但插入后得到以下错误,我该怎么办?json_encode在数据库中插入值的问题

值:

<div class="column"> 
    <input type="text" name="start_date[1][]" value="1111"> 
    <input type="text" name="end_date[1][]" value="1111"> 
    <input type="text" name="price_change[1][]" value="1111"> 
</div> 
<div class="column"> 
    <input type="text" name="start_date[2][]" value="2222"> 
    <input type="text" name="end_date[2][]" value="2222"> 
    <input type="text" name="price_change[2][]" value="2222"> 
</div> 
<div class="column"> 
    <input type="text" name="start_date[3][]" value="3333"> 
    <input type="text" name="end_date[3][]" value="3333"> 
    <input type="text" name="price_change[3][]" value="3333"> 
</div> 

PHP代码:

$residence_ups_input = $this->input->post('start_date'); 
    $residence_upe_input = $this->input->post('end_date'); 
    $residence_upc_input = $this->input->post('price_change'); 
    var_dump($residence_ups_input); // output this is: nobool(false) 
    $residence_p = array(); 
    foreach ($residence_ups_input as $idx => $name) { //line 134   
     $residence_p[] = array(
      'start_date' => $residence_ups_input[$idx], 
      'end_date' => $residence_upe_input[$idx], 
      'price_change' => $residence_upc_input[$idx] 
     ); 
    } 
    ; 
    $data = array(
     //'name' => $this -> input -> post('name'), 
     'residence_p' => json_encode($residence_p) 
    ); 
    $this->db->insert('tour_foreign', $data); 

错误:

A PHP Error was encountered
Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: inser.php
Line Number: 134

+0

请参阅我的**值**在上面的鳕鱼,他们是阵列。 –

回答

0

更改所有列:

<div class="column"> 
    <input type="text" name="start_date[1][]" value="1111"> 
    <input type="text" name="end_date[1][]" value="1111"> 
    <input type="text" name="price_change[1][]" value="1111"> 
</div> 
.... 

到:

<form method="post"> 
<div class="column"> 
    <input type="text" name="start_date[]" value="1111"> 
    <input type="text" name="end_date[]" value="1111"> 
    <input type="text" name="price_change[]" value="1111"> 
</div> 
.... 

</form> 
+0

我改变了它们,但在'var_dump'中仍然有相同的错误和相同的输出。 !? –

+0

你的代码有问题,因为我已经测试过它的工作原理,你有'

'tag?看到更新 – tttony

+0

谢谢,完成。 –

0

更换

foreach ($residence_ups_input as $idx => $name) { 

foreach (get_object_vars($residence_ups_input) as $idx => $name) { 

你传递一个对象的foreach,它想关联数组。从现在开始做

json_decode($string, true) 

,而不是

json_decode($string) 

和访问这样的变量:

$var['key']; 

,而不是 $ VAR->键

+0

我没有在代码'json_decode($ string)'中使用'codeigniter'。这是真的 –

+0

我很抱歉,但我不熟悉'codeigniter',那是什么? –

+0

转到此处:http://codeigniter.com/ –

0

$residence_ups_input变量不是阵列

使用此代码:

array_push($residence_ups_input,$this->input->post('start_date')); 
array_push($residence_upe_input,$this->input->post('end_date')); 
array_push($residence_upc_input,$this->input->post('price_change')); 

得到的值,然后使用foreach()

+0

这是数组,请在我的文章中查看我的html代码。什么是问题? –

+0

$ residence_ups_input是一个仅包含price_change的数组 –