【问题标题】:How do I ensure null is sent instead of 0 for empty form fields如何确保为空表单字段发送 null 而不是 0
【发布时间】:2016-05-27 10:55:51
【问题描述】:

我有一个表,其中的列允许空值并具有默认空值。更新时,如果该字段为空(未插入数据),我的脚本将插入 0 而不是 null。我遇到了与我类似的问题,并且我尝试了给出的建议,但仍然无法解决我的问题。这是我的代码

<?php
if (isset($_POST['submit'])) {

    # process the form
    $student_id = $_POST["student_id"];
    $subject_id = $_POST['subject_id'];
    if (is_null($_POST["test1"])){$test1 = null;} else {$test1 = $_POST["test1"];}
    if (is_null($_POST["test2"])){$test2 = null;} else {$test2 = $_POST["test2"];}
    if (is_null($_POST["test3"])){$test3 = null;} else {$test3 = $_POST["test3"];}

    for($i=0; $i < count($student_id); $i++) {
        $studentid = mysqli_real_escape_string($connection, $student_id[$i]);
        $subjectid = mysqli_real_escape_string($connection, $subject_id);
        $test_1 = mysqli_real_escape_string($connection, $test1[$i]);
        $test_2 = mysqli_real_escape_string($connection, $test2[$i]);
        $test_3 = mysqli_real_escape_string($connection, $test3[$i]);

        $query = "UPDATE fullresult SET test1='{$test_1}', test2='{$test_2}', test3='{$test_3}' WHERE student_id={$studentid} AND subject_id={$subjectid}";
        $result = mysqli_query($connection, $query);
    }
}
?>

当我回显查询时,这就是我所看到的,我想知道为什么我仍然插入 0

UPDATE fullresult SET test1=' 10', test2=' ', test3=' ' WHERE student_id=51 AND subject_id=2

【问题讨论】:

  • SET test1=" . $test_1 == null ? "null" : "'$test_1'" . "
  • test1.. 字段是整数吗?
  • @ErikTerwan 是的,这些字段是整数

标签: php mysql


【解决方案1】:

is_null 不会为空字符串返回 true。尝试将您的 if 语句更改为以下内容:

$test1 = trim($_POST["test1"])
if (!strlen($test1)) $test3 = null;

【讨论】:

  • 如果您只期望数值,您也可以使用 is_numeric 函数。
  • 也无法使用此代码修复它。感谢输入
【解决方案2】:

你可以使用

ctype_digit

检查其中是否有数字字符。

功能

mysqli::real_escape_string -- mysqli_real_escape_string — Escapes special characters in a string for use in an SQL statement, taking into account the current charset of the connection

(来源:http://php.net/manual/en/mysqli.real-escape-string.php

既然你想在数据库中有 null 你应该重写代码

    if (is_null($_POST["test1"])){$test1 = null;} else {$test1 = mysqli_real_escape_string($connection, $_POST["test1"]);}

仅在需要时才对值进行转义(如果您在 $_POST 中有值)

【讨论】:

    【解决方案3】:

    怎么样

    if (isset($_POST['submit'])) {
    
        # process the form
        $student_id = $_POST["student_id"];
        $subject_id = $_POST['subject_id'];
    
        # only retrieve FILLED IN answers
        $tests = array();
        if(isset($_POST["test1"]) && strlen($_POST["test1"])) $tests['test1'] = $_POST["test1"];
        if(isset($_POST["test2"]) && strlen($_POST["test2"])) $tests['test2'] = $_POST["test2"];
        if(isset($_POST["test3"]) && strlen($_POST["test3"])) $tests['test3'] = $_POST["test3"];
    
        if(!empty($tests)){ # if there were no answers, there's no point in updating the database
            for($i=0; $i < count($student_id); $i++) {
                $studentid = mysqli_real_escape_string($connection, $student_id[$i]);
                $subjectid = mysqli_real_escape_string($connection, $subject_id);
    
                # now let's build the "SET" part of the query
                $set = array();
                foreach($tests as $key => $value) $set[]=mysqli_real_escape_string($key)."='".mysqli_real_escape_string($value)."'";
                $set = implode(', ',$set);
    
                # ...and finally update
                $query = "UPDATE fullresult SET {$set} WHERE student_id={$studentid} AND subject_id={$subjectid}";
                $result = mysqli_query($connection, $query);
            }
        }
    }
    

    这种方法的要点是,如果您在 UPDATE 查询中不包含 key=>value 对,它将使用其默认值填充。

    【讨论】:

    • 0 是一个可能的值,因此我没有使用 empty()。使用 isset() 也可以得到与使用 is_null() 相同的结果。
    • 我仍然无法让它工作。有什么方法可以试试吗?
    • @Mena 不这么认为。您现在遇到什么问题?
    • 仍然插入 0。所以我想出了另一种方法来应对这个挑战。我的新计划是这样,而不是为每个测试创建一个包含多列的表单,而是使用一列并允许用户指定要更新的测试。
    【解决方案4】:

    您必须设置'null' 字,而不是null 值。

    if (is_null($_POST["test1"])){$test1 = 'null';} else {$test1 = $_POST["test1"];}
    if (is_null($_POST["test2"])){$test2 = 'null';} else {$test2 = $_POST["test2"];}
    if (is_null($_POST["test3"])){$test3 = 'null';} else {$test3 = $_POST["test3"];}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多