在php中插入日期中添加#无效

时间:2019-01-15 07:34:34

标签: php ms-access

我已经阅读了很多有关通过PHP在MS Access中插入日期的信息。但是,根据他们的建议,我必须在插入日期中使用#。但是,PHP中的#用于注释掉一行代码。我现在的问题是,我无法在数据库中插入/更新日期。

    <?php
    session_start();
    $blnPage = $_SESSION['blnCount'];

    try{
        if ($blnPage == "EAP"){
            if (isset($_POST['AudPlanNo']) && !empty($_POST['AudPlanNo'])){

                $txtAPNo = trim($_POST['AudPlanNo']);
                $txtAPUnivNo = trim($_POST['AudUnivNo']);
                $txtAPBackground = trim($_POST['APBackground']);
                $txtAPCoverage = trim($_POST['APCoverage']);
                $txtAPObjective = trim($_POST['APObjective']);
                $txtAPMethodology = trim($_POST['APMethodology']);
                $txtAPWTManhours = trim($_POST['APWTManhours']);
                $txtAPWTDate2 = strtotime($_POST['dWTDate']);
                $txtAPWTDate = date("m-d-Y",$txtAPWTDate2);
                $txtAPFWManhours = trim($_POST['APFWManhours']);
                $txtAPFWDate2 = strtotime($_POST['dFWTDate']);
                $txtAPFWDate = date("m-d-Y",$txtAPFDate2);
                $txtAPRTManhours = trim($_POST['APRTManhours']);
                $txtAPRTDate2 = strtotime($_POST['dRTDate']);
                $txtAPRTDate = date("m-d-Y", $txtAPRTDate2);

                $dbPath = $_SERVER["DOCUMENT_ROOT"]."/msaccess/dbAMS.accdb";
                if(!file_exists($dbPath))
                {
                    die("database not found");
                }
                $dbh = new PDO("odbc:DRIVER={Microsoft Access Driver (*.mdb, *.accdb)}; Dbq=$dbPath; Uid=Admin; pwd=;");

                $sql = "UPDATE tblAudPlan SET tblAudPlan.[Background] = '$txtAPBackground', tblAudPlan.[AudObjective] = '$txtAPObjective', tblAudPlan.[AudCoverage] = '$txtAPCoverage', tblAudPlan.[ApproachMethodology] = '$txtAPMethodology', tblAudPlan.[WTManhours] = '$txtAPWTManhours',tblAudPlan.[WTDate] = '#$txtAPWTDate#' WHERE tblAudPlan.[AudPlanNo] = '$txtAPNo'";
                $result = $dbh->query($sql);
                $row = $result->fetch();
                echo 1;   
            }
            else{
                echo 0;
            }
        }

    }

    catch(PDOException $e)
    {
        echo $e->getMessage();
    }
    $dbh = null;
?>

这就是我显示代码的方式

<div class="col-75">
<input type="date" placeholder = "YYYY-mm-dd" class ="dWTDate" id="dWTDate" name="dWTDate" value="<?php $datetime1 = $_SESSION['sesAPWTDate']; echo date("Y-m-d",strtotime($datetime1));?>">
</div>

我也尝试了“ m-d-Y”,“ mm-dd-YYYY”,但无济于事。我真的不知道该怎么办。

当我尝试在开头和结尾使用单个#时,我的代码将如下所示 enter image description here

2 个答案:

答案 0 :(得分:0)

用双引号将SQL换行

$sql = "UPDATE tblAudPlan SET WTDate = #$txtAPWTDate# WHERE AudPlanNo = '$txtAPNo'";

答案 1 :(得分:0)

我做了一些实验,以澄清我的想法:

我写了一个小脚本进行测试;

<?php

$alpha   = "myvar";
$bravo   = "UPDATE myTable SET mycol='#$alpha#' WHERE a = 1";
$charlie = "UPDATE myTable SET mycol=#$alpha#   WHERE a = 1";
$delta   = <<< EOT
UPDATE myTable SET mycol=#$alpha#  WHERE a = 1
EOT;


echo "a:\n";
echo $alpha;
echo "\n";

echo "b:\n";
echo $bravo;
echo "\n";

echo "c:\n";
echo $charlie;
echo "\n";
echo "d:\n";
echo $delta;
echo "\n";

...然后我使用php test.php在命令行上运行

结果如下:

a:
myvar
b:
UPDATE myTable SET mycol='#myvar#' WHERE a = 1
c:
UPDATE myTable SET mycol=#myvar#   WHERE a = 1
d:
UPDATE myTable SET mycol=#myvar#  WHERE a = 1

这意味着我和@ MH2K9的写作建议 $sql = "UPDATE tblAudPlan SET WTDate = #$txtAPWTDate# WHERE AudPlanNo = '$txtAPNo'";

应该起作用。

如果您的脚本将其视为注释,那么您必须有一个中间步骤将$ sql解析为php代码,否则它仅作为字符串被插入...

我的php版本:

  

PHP 7.0.33-0 + deb9u1(cli)(内置:2018年12月7日11:36:49)(NTS)
  版权所有(c)1997-2017 The PHP Group
  Zend Engine v3.0.0,版权所有(c)1998-2017 Zend Technologies
      与Zend OPcache v7.0.33-0 + deb9u1一起使用,Zend Technologies版权所有(c)1999-2017