0

每次我运行以下 PHP 代码时,我都会得到输出

预订确认!祝贺。您的预订 ID 是:6

成功预订。

bookingid 是一个序列,然后我从插入的值中获取该预订 id 以获取序列的当前值。所以我不知道我做错了什么。

   <html><body>
   <?php
       $con = oci_connect("system", "password", "localhost/XE");
          if (!$con) {
            $m = oci_error();
        exit('Connect Error ' . $m['message']);
        }
        $thid = $_GET["hid"];
        $trno = $_GET["rno"];
        $tgid = $_GET["gid"];
        $sd = $_GET["sdate"];
        $ed = $_GET["edate"];
        $dchange = "ALTER SESSION SET NLS_DATE_FORMAT= 'YYYY-MM-DD'";
        $stid1 = oci_parse($con,$dchange);
        oci_execute($stid1);
        $c1 = "SELECT * FROM B WHERE HOTELID = '$thid' AND ROOMNO = '$trno' AND ((STARTDATE < '$sd' AND ENDDATE > '$sd') or (STARTDATE < '$ed' AND ENDDATE > '$ed') or (STARTDATE >= '$sd' AND ENDDATE <= '$ed'))";
        $c2 = oci_parse($con, $c1);
        oci_execute($c2);
        $row = oci_fetch_row($c2);
        if(!$row)
        {
        $temp =  "INSERT INTO B VALUES(bno.nextval,'$thid','$trno','$tgid','$sd','$ed')";
        $stid = oci_parse($con,$temp);
        oci_execute($stid);
         oci_free_statement($stid);
        //$c7 = "SELECT bookid FROM B WHERE HOTELID = '$thid' AND ROOMNO = '$trno' AND GUEStID = '$tgid' AND STARTDATE = '$sd' AND ENDDATE = '$ed'";
        //printf("<h3>Booking Confirmed! Congatulation. </h3>") ;
        **$c8 = oci_parse($con, "SELECT MAX(BOOKID) FROM B");
        oci_execute($c8);
        printf("<h3>Booking Confirmed! Congratulation. Your Booking Id is: %u</h3>", $c8);**
        }
        else
            printf("<h3>Booking already exist. </br>Please try with another search.</h3>");


        ?>
</n> <form action="index.php"><input type="submit" value="BACK" />
    </form>

</BODY>

4

1 回答 1

0

给你一些建议。

  1. 您应该在 SQL 语句中使用绑定变量而不是拼接文字(除非您希望在填满共享池时降低数据库的性能)。

  2. SELECT MAX(BOOKID) FROM B是获取预订 ID 的错误且不安全的方式。好像 2 个会话并行进行了预订,您可能会得到错误的结果(如果表中的最大 id 高于当前序列值,您也会得到错误的结果)。而是做select bno.currval from dual或使用返回子句作为插入的一部分(假设 PHP 使用它)

  3. 在您的插入中,您应该指定列名以获得良好的做法。即做INSERT INTO B (BOOKID, HOTELID, ROOMNO, STARTDATE, ENDDATE) VALUES(bno.nextval....

于 2013-03-19T22:05:12.353 回答