2012-11-19 37 views
0

(這是所有基本的php voor學校) 我做了一個表單,您可以更新您的帳戶信息,當您點擊提交按鈕時,它會來到這個php代碼。如果一個字段沒有填寫,它不需要更新,我試過「WHERE字段不是NULL」,但它似乎不工作,它給出了一個空記錄...php sql更新只填寫在域

(變量都在荷蘭,抱歉)

$klantnummer = $_COOKIE['klantnummer']; 
$naam =($_POST["naam"]); 
$adres =($_POST["adres"]); 
$postcode =($_POST["postcode"]); 
$gemeente =($_POST["gemeente"]); 
$leden =($_POST["gezinsleden"]); 
$huidigemeterstand =($_POST["huidigemeterstand"]); 
$vorigemeterstand =($_POST["vorigemeterstand"]); 
$provincie =($_POST["provincie"]); 


//set up connection and choose database 
$con = mysql_connect("localhost","root","root"); 
if (!$con) 
    { 
    die('Could not connect: ' . mysql_error()); 
    } 

mysql_select_db("opdracht3", $con); 
mysql_query("UPDATE waterstand SET naam = '$naam', adres = '$adres', postnummer = '$postcode', gemeente = '$gemeente', vorigemeterstand='$vorigemeterstand', huidigemeterstand='$huidigemeterstand', provincie='$provincie', aantalgezinsleden = '$gezinsleden' 
WHERE klantnummer = '$klantnummer' AND naam IS NOT NULL");` 

ofcourse我需要廣告的「場」的其餘部分是NOT NULL,但比如我只用「NAAM」 ..但它不工作:/

+1

停止,它們被棄用。開始使用PDO,它會更好,更安全 –

+0

數據庫字段中的默認值是否設置爲NULL?如果它們設置爲''或類似,那麼IS NULL檢查將不會返回true。 – Maccath

+0

@Ben Carey:這是我們在學校學習的方式,但我會檢查出來,謝謝! – user1830235

回答

0

這可能如果你保持這部分邏輯遠離數據庫最簡單,例如

<?php 
$con = mysql_connect("localhost","root","root"); 
if (!$con) 
{ 
    die('Could not connect: ' . mysql_error()); 
} 

if (!mysql_select_db("opdracht3", $con)) { 
    die('Could not connect: ' . mysql_error()); 
} 

// contains strings/parameters/identifiers ready-for-use with mysql 
$sql_params = array(
    'fields' => array('naam', 'adres', 'postcode', 'gemeente', 'gezinsleden', 'huidigemeterstand', 'vorigemeterstand', 'provincie'), 
    'klantnummer' => mysql_real_escape_string($_COOKIE['klantnummer']), 
    'updates' => array() 
); 

// the names of the POST-fields are the same as the column identifiers of the database table 
// go through each identifier 
foreach($sql_params['fields'] as $f) { 
    // put in here the conditions for "an empty field, e.g. 
    // if there is such POST-field and its value is one or more "usable" characters long 
    if (isset($_POST[$f]) && strlen(trim($_POST[$f])) > 0) { 
     // create a new `x`=`y` "line" as in UPDATE ... SET `x`='y' 
     // and append it to the array $sql_params['updates'] 
     $sql_params['updates'][] = sprintf("`%s`='%s'", $f, mysql_real_escape_string($_POST[$f])); 
    } 
} 

// the "lines" in $sql_params['updates'] need to be concatenated with , between them 
// join(', ', $sql_params['updates']) does that 
// insert that string and the parameter klantnummer into the query string 
$query = sprintf(
    " 
     UPDATE 
      waterstand 
     SET 
      %s 
     WHERE 
      klantnummer = '%s' 
    ", 
    join(', ', $sql_params['updates']), 
    $sql_params['klantnummer'] 
); 

使用`mysql_ functions`作爲開始(未測試的)

+0

哇,非常感謝@VolkerK,但我認爲代碼是對我來說不可讀:D,我們正在學習基礎知識,我不明白這段代碼,對不起:我檢查了你給我的過濾器頁面,我想使用filter_has_var函數,對吧? – user1830235

+0

對不起,該鏈接可能是誤導性的,因爲它可以幫助您很少修改查詢字符串。添加到示例腳本的註釋。 – VolkerK