【问题标题】:how to pass null in date column using storedprocedure in PHP?如何使用 PHP 中的存储过程在日期列中传递 null?
【发布时间】:2019-11-11 16:40:34
【问题描述】:

我有日期列,如果它为空,则它不会进一步插入任何值。

在数据库中出现错误 CALL

return_ship_stones("1.01","IF",
         "G", "EC/0122/19-20", "", "2", 
        "5.44x5.23x3.92" , "3822" , "1333041478" , "1" ,
         "1333041478" , "Princess" , "3860.2200000000003", "", "125")   

Error Code: 1292. Incorrect date value: '' for column ``.``.`_invoicedate` at row 5 0.000 sec

$updstmt = '';

      foreach ($StoneArr as $Stone)
      {
        $textboxval = $_REQUEST['textboxval'];
        $refVal = $textboxval;
        $clientname = $_REQUEST['clientname'];

         $updstmt .= 'CALL return_ship_stones('.'"'.$Stone["carat"].'"'.','.'"'.$Stone["clarity"].'"'.',
        '.'"'.$Stone["color"].'"'.', '.'"'.$Stone["invcno"].'"'.', '.'"'.$StoneArr["invoicedate"].'"'.', '.'"'.$Stone["lab"].'"'.', 
       '.'"'.$Stone["measurement"].'"'.' , '.'"'.$Stone["ppt"].'"'.' , '.'"'.$Stone["qstonesid"].'"'.' , '.'"'.$Stone["qty"].'"'.' ,
        '.'"'.$Stone["reportno"].'"'.' , '.'"'.$Stone["shape"].'"'.' , '.'"'.$Stone["totalvalue"].'"'.', '.'"'.$refVal.'"'.', '.'"'.$clientname.'"'.');';
      }

我希望如果它为空,那么日期列应该为空或默认值,如“0000-00-00”。

【问题讨论】:

  • 添加coalesce('"'.$StoneArr["invoicedate"].'"', '0000-00-00')
  • 对绑定变量使用准备好的语句。
  • 那是一些疯狂的代码......
  • $StoneArr["invoicedate"] 为空,您的例程需要一个有效日期,而 0000-00-00 不是日期。尝试发送 NULL 或类似 1970-01-02 的日期,您可以检查

标签: php mysql


【解决方案1】:

完全未经测试,但也许您会发现以下更简单、更健壮。如果您使用prepared statement,您可以通过为bound 变量分配新值来使用相同的语句进行多次执行。这不仅使您的代码更易于阅读和调试,而且速度更快(可能是边际)并减轻了 SQL 注入的危险。

$sql='call `return_ship_stones`(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)';
$stmt=$db->prepare( $sql );

if( $stmt ){

    $refVal=$_REQUEST['textboxval'];
    $clientname = $_REQUEST['clientname'];  

    /* 15 type placeholders must match 15 variables ... */
    $stmt->bind_param( 'sssssssssssssss', $carat, $clarity, $color, $invcno, $invoicedate, $lab, $measurement, $ppt, $qstonesid, $qty, $reportno, $shape, $totalvalue, $refVal, $clientname );

    /*
        iterate through your array and assign a value 
        to the above variables prior to executing the 
        statement.

        Was the use of `$StoneArr['invoicedate']` in 
        the loop rather than `$Stone['invoicedate']`
        correct?
    */
    foreach( $StoneArr as $Stone ){
        $carat=$Stone['carat'];
        $clarity=$Stone['clarity'];
        $color=$Stone['color'];
        $invcno=$Stone['invcno'];
        $invoicedate=!empty( $Stone['invoicedate'] ) ? $Stone['invoicedate'] : '0000-00-00';
        $lab=$Stone['lab'];
        $measurement=$Stone['measurement'];
        $ppt=$Stone['ppt'];
        $qstonesid=$Stone['qstonesid'];
        $qty=$Stone['qty'];
        $reportno=$Stone['reportno'];
        $shape=$Stone['shape'];
        $totalvalue=$Stone['totalvalue'];

        $result = $stmt->execute();
    }

    $stmt->close();
    $db->close();

} else {
    exit( 'Failed to prepare SQL query' );
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-23
    • 2014-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-15
    • 2017-12-24
    相关资源
    最近更新 更多