在DB(方法)上插入NULL值

时间:2014-05-06 09:31:40

标签: php mysql sql

UPDATE ----

使用此方法:

                        foreach($_POST as $k => $v) { 
  $params[] = empty($v)? "NULL":$v;
} 

$params_string_values = "'" . implode("','",$params) . "'";
$param_name_list = "tu_id,tu_status,tu_name,tu_fk_tt_id,tu_mon_1_s,tu_mon_1_e,tu_mon_2_s,tu_mon_2_e,tu_mon_3_s,tu_mon_3_e,tu_tue_1_s,tu_tue_1_e,tu_tue_2_s,tu_tue_2_e,tu_tue_3_s,tu_tue_3_e,tu_wed_1_s,tu_wed_1_e,tu_wed_2_s,tu_wed_2_e,tu_wed_3_s,tu_wed_3_e,tu_thu_1_s,tu_thu_1_e,tu_thu_2_s,tu_thu_2_e,tu_thu_3_s,tu_thu_3_e,tu_fri_1_s,tu_fri_1_e,tu_fri_2_s,tu_fri_2_e,tu_fri_3_s,tu_fri_3_e,tu_sat_1_s,tu_sat_1_e,tu_sat_2_s,tu_sat_2_e,tu_sat_3_s,tu_sat_3_e,tu_sun_1_s,tu_sun_1_e,tu_sun_2_s,tu_sun_2_e,tu_sun_3_s,tu_sun_3_e";
$param_values = "'','1',{$params_string_values}";

$insert_query = mysql_query("INSERT into turn_conf( {$param_name_list} ) values ({$param_values})");

它创建了一个有效的查询(这里我粘贴它),但数据库中没有存储NULL值,所有NULL值都以“00:00”的形式转到数据库:

INSERT into turn_conf( tu_id,tu_status,tu_name,tu_fk_tt_id,tu_mon_1_s,tu_mon_1_e,tu_mon_2_s,tu_mon_2_e,tu_mon_3_s,tu_mon_3_e,tu_tue_1_s,tu_tue_1_e,tu_tue_2_s,tu_tue_2_e,tu_tue_3_s,tu_tue_3_e,tu_wed_1_s,tu_wed_1_e,tu_wed_2_s,tu_wed_2_e,tu_wed_3_s,tu_wed_3_e,tu_thu_1_s,tu_thu_1_e,tu_thu_2_s,tu_thu_2_e,tu_thu_3_s,tu_thu_3_e,tu_fri_1_s,tu_fri_1_e,tu_fri_2_s,tu_fri_2_e,tu_fri_3_s,tu_fri_3_e,tu_sat_1_s,tu_sat_1_e,tu_sat_2_s,tu_sat_2_e,tu_sat_3_s,tu_sat_3_e,tu_sun_1_s,tu_sun_1_e,tu_sun_2_s,tu_sun_2_e,tu_sun_3_s,tu_sun_3_e ) values ('','1','12345555','1','10:00','NULL','NULL','NULL','NULL','NULL','NULL','NULL','NULL','NULL','NULL','NULL','NULL','NULL','NULL','NULL','NULL','NULL','NULL','NULL','NULL','NULL','NULL','NULL','NULL','NULL','NULL','NULL','NULL','NULL','NULL','NULL','NULL','NULL','NULL','NULL','NULL','NULL','NULL','NULL','NULL','NULL')

这是它在DB上记录的内容:

record



我有这样的查询,有时变量'$va1''$val2''$val3'没有价值:

$insert = mysql_query("INSERT INTO turn_conf (tu_id,value1,value2,value3) VALUES ('','$va1','$val2','$val3')") or die (mysql_error());

如果这些变量中没有任何值存储,则必须将与其相关的任何内容发送到DB(以便在DB上存储NULL值),例如,如果只有'$val1'存储信息,则最后查询必须是:

$insert = mysql_query("INSERT INTO turn_conf (tu_id,value1) VALUES ('','$va1')") or die (mysql_error());

为了解决这个问题,我已经为每个变量创建了一个结构,检查它是否存储了某些东西,如果不存储,则只是声明什么,不发送任何内容:

if ($_POST['value1'] == ""){
    $val1_p = "";
    $val1_s = ""; 
}else {
    $val1_p = ",value1";
    $val1_sv = $_POST['value1'];
    $val1_s = ", '$val1_sv'" ;}  

if ($_POST['value2'] == ""){
    $val2_p = "";
    $val2_s = ""; 
}else {
    $val2_p = ",value2";
    $val2_sv = $_POST['value2'];
    $val2_s = ", '$val2_sv'" ;}  

if ($_POST['value3'] == ""){
    $val3_p = "";
    $val3_s = ""; 
}else {
    $val3_p = ",value3";
    $val3_sv = $_POST['value3'];
    $val3_s = ", '$val3_sv'" ;}   

$insert = mysql_query("INSERT INTO turn_conf (tu_id $val1_p $val2_p $val3_p) VALUES ('' $val1_s $val2_s $val3_s)") or die (mysql_error());

这可行,并创建正确的查询,但如果您发现方法正确,或者如果选择其他更有效的方法,则想知道。请不要在这个例子中我只使用了3个变量,但这个真实的查询有43个变量,由于数据量的原因,我提出了这个问题。

2 个答案:

答案 0 :(得分:2)

$insert = mysql_query("INSERT INTO turn_conf (tu_id,value1,value2,value3) VALUES ('','".((isset($va1))?"'".$va1."'":"NULL")."','".((isset($va2))?"'".$va2."'":"NULL")."','".((isset($va3))?"'".$va3."'":"NULL")."')") or die (mysql_error());

基本上你想测试是否设置了值。我们使用简短的if-notation:

isset($va1)?"'".$va1."'":"NULL"

如果设置了$ va1(有一个值),我们会在查询中加上“'value'”,否则为空值“NULL”。

如果你想测试一个空字符串:

(isset($va1) && $va1 != '')?"'".$va1."'":"NULL"

答案 1 :(得分:1)

如果您不使用预备声明请尝试:

foreach($_POST as $k => $v) { 
  $params[] = empty($v)? "NULL":$v;
} 
mysql_query("insert into turn_conf(field1,field2...) values(" . implode(",",$params).  ");

如果你使用预备声明(更好!)尝试这样的事情:

foreach($_POST as $v) {
 $params[] = $v;
}
$sth = $dbh->prepare("INSERT INTO turn_conf (tu_id $val1_p $val2_p $val3_p) VALUES (?,?,?)");
$sth->execute($params);

这么简单!

PS:这是一个示例,但不直接使用$ _POST值,在(http://www.php.net/manual/en/function.filter-input.phphttp://www.php.net/manual/en/filter.filters.sanitize.php之前对其进行过滤)。例如:

$field_int= filter_input(INPUT_POST, 'field1', FILTER_SANITIZE_NUMBER_INT);

更新

您使用此代码:

 $insert = mysql_query("INSERT into turn_conf(tu_id,tu_name,tu_status,tu_fk_tt_id,tu_mon_1_s,tu_mon_1_e,tu_mon_2_s,t‌​u_mon_2_e,tu_mon_3_s,tu_mon_3_e,tu_tue_1_s,tu_tue_1_e,tu_tue_2_s,tu_tue_2_e,tu_tu‌​e_3_s,tu_tue_3_e,tu_wed_1_s,tu_wed_1_e,tu_wed_2_s,tu_wed_2_e,tu_wed_3_s,tu_wed_3_‌​e,tu_thu_1_s,tu_thu_1_e,tu_thu_2_s,tu_thu_2_e,tu_thu_3_s,tu_thu_3_e,tu_fri_1_s,tu‌​_fri_1_e,tu_fri_2_s,tu_fri_2_e,tu_fri_3_s,tu_fri_3_e,tu_sat_1_s,tu_sat_1_e,tu_sat‌​_2_s,tu_sat_2_e,tu_sat_3_s,tu_sat_3_e,tu_sun_1_s) values('','$newTurnName','1','$newTurnType'," . implode(",",$params). "))")

但是改变它:

$params_string_values = "'" . implode("','",$params) . "'";
$param_name_list = "tu_id,tu_name,tu_status,tu_fk_tt_id,tu_mon_1_s,tu_mon_1_e,tu_mon_2_s,t‌​u_mon_2_e,tu_mon_3_s,tu_mon_3_e,tu_tue_1_s,tu_tue_1_e,tu_tue_2_s,tu_tue_2_e,tu_tu‌​e_3_s,tu_tue_3_e,tu_wed_1_s,tu_wed_1_e,tu_wed_2_s,tu_wed_2_e,tu_wed_3_s,tu_wed_3_‌​e,tu_thu_1_s,tu_thu_1_e,tu_thu_2_s,tu_thu_2_e,tu_thu_3_s,tu_thu_3_e,tu_fri_1_s,tu‌​_fri_1_e,tu_fri_2_s,tu_fri_2_e,tu_fri_3_s,tu_fri_3_e,tu_sat_1_s,tu_sat_1_e,tu_sat‌​_2_s,tu_sat_2_e,tu_sat_3_s,tu_sat_3_e,tu_sun_1_s";
$param_values = "'','{$newTurnName}','1','{$newTurnType}',{$param_string_values}";

$insert_query = mysql_query("INSERT into turn_conf( {$param_name_list} ) values ({$param_values})");