如果用户输入为空,则为SQL数据库的NULL值

时间:2014-07-22 13:27:10

标签: php sql sql-server

我从一个框中获取用户输入日期,并将该值保存在名为Originated的SQL服务器单元中。如果用户输入了正确的日期,则此代码可以正常工作并使用值更新单元格Originated。但是,如果用户未在日期框中输入值,则我的SQL查询将自动分配1900-01-01。如果没有收到输入,我希望它分配值NULL。任何帮助将不胜感激

<?php   
    $connInfo = array(
        'Database' => 'databasename',
        'UID' => 'user',
        'PWD' => 'pasword',
        'ReturnDatesAsStrings' => true 
        );

    $connectString = sqlsrv_connect('sever.name.com', $connInfo) or die("Can't connect to the database");

    echo "<form action=\"\" method=\"POST\">Originated Date<input id=\"Originated\" type=\"date\" name=\"Originated\" \" >
    <input type=\"submit\" name=\"submit\" value=\"Submit\" /></form>";

    $Originated = $_POST['Originated'];     
    echo "<br>Originated date is:".$Originated;

        //if the unser input value is empty, this going to assin 1900-01-01 to the table. I want it to assign NULL
    $query_update = "UPDATE ElectronicShop SET Originated='$Originated' WHERE WorkOrder=5";
    $data = sqlsrv_query($connectString, $query_update) or die(print_r(sqlsrv_errors(SQLSRV_ERR_ALL), true));       


    if($data)
    {
        echo "<br><h3>Table was updated Successfully</h3>"; 
        echo "<br><form action=\"\" method=\"POST\">Click here to go back: <input type=\"submit\" name=\"refresh\" value=\"Refresh\"/></form><br><br>";
    }               
    else 
    {
        echo "<br><h3>Unable to update the database</h3>";  
        echo "<br><form action=\"\" method=\"POST\">Click here to go back: <input type=\"submit\" name=\"refresh\" value=\"Refresh\"/></form><br><br>";
    }
?>

0 个答案:

没有答案