Set value to NULL in MySQL

前端 未结 9 1350
逝去的感伤
逝去的感伤 2020-11-30 01:17

I want a value to be set to NULL if nothing is put into the text box in the form I\'m submitting. How can I make this happen? I\'ve tried inserting \'NULL

9条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-30 02:15

    if (($_POST['nullfield'] == 'NULL') || ($_POST['nullfield'] == '')) {
       $val = 'NULL';
    } else {
       $val = "'" . mysql_real_escape_string($_POST['nullfield']) . "'";
    }
    
    $sql = "INSERT INTO .... VALUES ($val)";
    

    if you put 'NULL' into your query, then you're just inserting a 4-character string. Without the quotes, NULL is the actual null value.

提交回复
热议问题