检查该日期是否已经在数据库中

时间:2019-01-22 13:08:31

标签: php mysql pdo prepared-statement

我需要制造它,以便人们可以租车,但是租车时不需要再次预订。所以我需要检查那几天那辆车是否已经租了。

if (isset($_POST['Huur'])) {
global $db;

$kenteken = htmlspecialchars($_POST["Kenteken"]);
$klantcode = $_SESSION['USERID'];
$factuurdatum = date("Y-m-d");


    $test = "SELECT * 
        FROM factuurregel 
        LEFT JOIN auto 
        ON factuurregel.Kenteken = auto.Kenteken

        LEFT JOIN factuur 
        ON factuurregel.Factuurnummer = factuur.Factuurnummer

        LEFT JOIN gebruiker 
        ON factuur.Klantcode = gebruiker.Klantcode

        WHERE gebruiker.Klantcode =:code AND auto.kenteken =:groen";

        $stmt10 = $db->prepare($test);
        $data10 = array("code" => $klantcode, "groen" => $kenteken);

        try {
            $stmt10->execute($data10);

        } 
        catch (PDOException $e) {
          echo $e->getMessage();
        }

$check = true;
foreach ($db->query($test) as $invoice) {
    if ($invoice['Begindatum'] <= $_POST['Begindatum'] && $invoice['Einddatum'] >= $_POST['Einddatum']) {
        $check = false;
    }
}
if ($check) {
    $sqlstatement = "INSERT INTO factuur (Factuurdatum, Kenteken, Klantcode)
        VALUES (:Factuurdatum, :Kenteken, :Klantcode)";
        $stmt = $db->prepare($sqlstatement);
        $data = array("Factuurdatum" => $factuurdatum, "Kenteken" => $kenteken, "Klantcode" => $klantcode);

        try {
            $stmt->execute($data);
            $factuurnummer = $db->lastInsertId(); 

        } 
        catch (PDOException $e) {
          echo $e->getMessage();
        }

        $begindatum = htmlspecialchars($_POST["Begindatum"]);
        $einddatum = htmlspecialchars($_POST["Einddatum"]);

        $sqlstatement1 = "INSERT INTO factuurregel (Factuurnummer, Kenteken, Begindatum, Einddatum)
        VALUES (:Factuurnummer, :Kenteken, :Begindatum, :Einddatum)";
        $stmt1 = $db->prepare($sqlstatement1);
        $data1 = array("Factuurnummer" => $factuurnummer, "Kenteken" => $kenteken, "Begindatum" => $begindatum, "Einddatum" => $einddatum);

        try {
            $stmt1->execute($data1);

        } 
        catch (PDOException $e) {
          echo $e->getMessage();
        }

    $password_err = "Uw auto is succesvol gereserveerd";
    echo '<script type="text/javascript">alert("'.$password_err.'");</script>';
}

}

我想我差不多了,但是这段代码会给我这个错误:

  

致命错误:未捕获的PDOException:SQLSTATE [42000]:语法错误或访问冲突:1064您的SQL语法有错误;请参阅附录A。检查与您的MariaDB服务器版本相对应的手册以获取正确的语法,以在C:\ xampp \ htdocs \ Rent-a-Car \ pages \ auto.php的第12行的':code AND auto.kenteken =:groen'附近使用:260堆栈跟踪:#0 C:\ xampp \ htdocs \ Rent-a-Car \ pages \ auto.php(260):PDO-> query('SELECT * \ r \ n ...')#1 {main }在第260行的C:\ xampp \ htdocs \ Rent-a-Car \ pages \ auto.php中抛出

此错误将告诉我他们在这行上的概率:WHERE gebruiker.Klantcode =:code AND auto.kenteken =:groen";,但我不知道是什么。

1 个答案:

答案 0 :(得分:0)

您的错误是由foreach ($db->query($test) as $invoice) {引起的。 query(函数不适用于绑定,如果可以,它将复制execute功能。您应将其替换为whilefetch或使用fetchall

try {
   $stmt10->execute($data10);
   $invoices = $stmt10->fetchAll();
} 

然后更改:

foreach ($db->query($test) as $invoice) {

收件人:

foreach ($invoices as $invoice) {

或者,如果没有fetchall,则可以将foreach替换为:

while($invoice = $stmt10->fetch(PDO::FETCH_ASSOC)){