2016-01-26 20 views
0

我有以下代碼的一些問題。在我的表格col6col7沒有在每種情況下定義。如果在數組和數據庫中

$data = Array (
    'col1' => time(), 
    'col2' => $_SESSION['user'], 
    'col3' => $_POST['incr'], 
    'col4' => 0, 
    'col6'=>$_POST['incit'], 
    'col7'=>$_POST['incex'] 
    ); 


$db->where ('id', $_POST['incag']); 
$id = $db->update ('table', $data); 

我怎樣才能讓我的$data變量,如果$_POST['incit']沒有設置,刪除COL6從陣列。

如果列保留,數據庫中的值將設置爲0或null。該值必須與POST一樣,以免更改。

回答

0

您需要使用isset()

<?php 
$data = Array (
'col1' => time(), 
'col2' => $_SESSION['user'], 
'col3' => $_POST['incr'], 
'col4' => 0, 
'col7'=>$_POST['incex'] 
); 
if(isset($_POST['incit']) && !empty($_POST['incit'])) { 
    $data['col6'] = $_POST['incit']; 
} 
?> 

查看關於isset()here更多信息。

2
$data = [ 
    'col1' => time(), 
    'col2' => $_SESSION['user'], 
    'col3' => $_POST['incr'], 
    'col4' => 0, 
    'col7' => $_POST['incex'], 
]; 
if (isset($_POST['incit'])) { 
    $data['col6'] = $_POST['incit']; 
} 

還有一件事 - 我希望你的$ db層處理所有與SQL注入相關的事情。由於你沒有進行任何過濾,它可能變得非常快速醜陋。

相關問題