2016-11-24 77 views
2

這是我的url鏈接和設備返回瀏覽器正文中的相應值。現在我將這些數據插入到我的數據庫表名「IpRelay」中。首先,我爆炸我的設備返回數據。然後我試圖插入爆炸數據。如何從網址插入數據到數據庫

這是我的代碼。但它不起作用。

public function get_data(){ 

    $contents = file_get_contents('http://10.5.40.83'); 

    function multiexplode ($delimiters,$string) { 
     $ready = str_replace($delimiters, $delimiters[0], $string); 
     $launch = explode($delimiters[0], $ready); 
     return $launch; 
    } 

    $get_device_data = multiexplode(array(",",":",), $contents); 



    $this->IpRelay->create(); 
    $this->IpRelay->set($this->request->data['get_device_data']); 
    $save = $this->IpRelay->save(); 
    echo $save ? "I've created the link" : "Error creating link!"; 
    die($this->IpRelay->id); 
} 

我是新的蛋糕,我使用的是蛋糕版本2.7.5。 請你幫幫我。在此先感謝

[ 

url=> http://10.5.40.83/ 

device return value=> 10,5,40,83:0,11,0,0,556 

][1] 

回答

1
$this->IpRelay->create(); 
$this->IpRelay->set($this->request->data['get_device_data']); 
$save = $this->IpRelay->save(); 
echo $save ? "I've created the link" : "Error creating link!"; 
die($this->IpRelay->id); 

而不是上面的代碼我使用下面這段代碼,它工作正常

$this->IpRelay->read(null, 1); 
$this->IpRelay->set(array(
    'a'  => $get_device_data[0], 
    'b'  => $get_device_data[1], 
    'c'  => $get_device_data[2], 
    'd'  => $get_device_data[3], 
    'e'  => $get_device_data[4], 
    'f'  => $get_device_data[5], 
    'g'  => $get_device_data[6], 
    'h'  => $get_device_data[7], 
    'volt' => $get_device_data[8] 
)); 
$this->IpRelay->save(); 
1

希望這是你

$this->Model_name->read(null, 1); 
$this->Model_name->set(
    array(
     'field_1'  => $get_device_data[0], 
     'field_2'  => $get_device_data[1] 
    ) 
); 

$this->Model_name->save(); 
易於和樂於助人