2017-05-26 288 views
0

我有一個簡單的表單(main.php),它將輸入作爲電話號碼。的客戶:使用Oracle數據庫11g R2根據輸入值在PHP中顯示信息

<!DOCTYPE HTML> 
    <html> 
     <div style=margin:0 auto align=center > 
     <form action = "options.php" method = "get" /> 
      <p> <h3>Enter Phone Number:</h3> <input type = "text" name = 
            "cust_phone" /> 
      <p> <input type = "submit" value = "Submit" /> 
     </form> 
     </div> 
    </html> 

該no。在Oracle數據庫中檢查已輸入的數據,如果客戶存在,那麼會顯示關於該客戶的信息,否則將添加一個新客戶以及電話號碼。 (options.php)

 <!DOCTYPE HTML> 
    <html> 
     <body> Details of:<?php echo htmlentities($_GET["cust_phone"])."<br>"; 

    $link = oci_connect('hd', 'hd', 'localhost/mydb'); 
    if(!$link) 
     {  
      $e = oci_error(); 
      exit('Connection Error' . $e['message']); 
     } 
    $query = "select cust_id from customer where cust_phone = :ph_bv"; 
    $stid = oci_parse($link,$query); 
    $ph = htmlentities($_GET["cust_phone"]); 
    oci_bind_by_name($stid, ':ph_bv', $ph); 
    oci_execute($stid); 
    $row = oci_fetch_array($stid, OCI_ASSOC); 
    if(!$row) 
    { 
     exit("Person Not Found"); 
    } 
    $cust_id = $row["ID"]; 
    oci_free_statement($stid); 
?> 
    <table border = "black" /> 
     <tr> 
      <th> ADDRESS </th> 
      <th> AREA </th> 
     </tr> 
    <?php 
     $query1 = "select a.address, a.area from customer c 
       join customer_address ca on c.cust_id = ca.cust_id 
       join address a on a.address_id = ca.address_id where cust_id = 
    :id_bv"; 
     $stid1 = oci_parse($link, $query1); 
     oci_bind_by_name($stid1, ":id_bv", $cust_id); 
      oci_execute($stid1); 
      while($row = oci_fetch_array($stid1)) 
      { 
       echo "<tr><td>" . htmlentities($row["ADRESS"]) . "</td>"; 
       echo "<td>" . htmlentities($row["AREA"]) . "</td></tr>"; 
      } 
      oci_free_statement($stid1); 
      oci_close($link); 
     ?> 
     </table> 
      </body> 
     </html> 

代碼的第一部分工作正常,它顯示消息「人未找到」。然而,第二部分給出了錯誤:的 詳情:9711210000

 (!) Notice: Undefined index: ID in 
      E:\xampp\htdocs\myfiles\options.php on line 24 
    Call Stack 
    # Time Memory Function Location 
    1 0.0013 137104 {main}() ...\options.php:0 

    ADDRESS AREA 
    (!) Warning: oci_execute(): ORA-00918: column ambiguously defined in 
    E:\xampp\htdocs\myfiles\options.php on line 38 
    Call Stack 
    # Time Memory Function Location 
    1 0.0013 137104 {main}() ...\options.php:0 
    2 0.0400 139336 oci_execute () ...\options.php:38 

    (!) Warning: oci_fetch_array(): ORA-24374: define not done before 
    fetch or execute and fetch in E:\xampp\htdocs\myfiles\options.php on line 
    39 
    Call Stack 
    # Time Memory Function Location 
    1 0.0013 137104 {main}() ...\options.php:0 
    2 0.0418 139336 oci_fetch_array () ...\options.php:39 

我有兩個問題:1。 相反,我想在我的數據庫添加新客戶「找不到人」的? 2.如何解決這些錯誤? 我是PHP新手,這只是我的第一個代碼。任何幫助讚賞。

回答

0

$ row沒有記錄,所以你不能訪問$ row [「ID」],所以首先檢查記錄是否存在,然後才能訪問數據。

第二個問題與您的查詢有關,cust_id字段在兩個表中,因此在編寫查詢時使用別名。

嘗試用下面的代碼: -

<!DOCTYPE HTML> 
    <html> 
     <body> Details of:<?php echo htmlentities($_GET["cust_phone"])."<br>"; 

    $link = oci_connect('hd', 'hd', 'localhost/mydb'); 
    if(!$link) 
     {  
      $e = oci_error(); 
      exit('Connection Error' . $e['message']); 
     } 
    $query = "select cust_id from customer where cust_phone = :ph_bv"; 
    $stid = oci_parse($link,$query); 
    $ph = htmlentities($_GET["cust_phone"]); 
    oci_bind_by_name($stid, ':ph_bv', $ph); 
    oci_execute($stid); 
    $row = oci_num_rows($stid); 
    $cust_id =''; 
    if($row>0) 
    { 
     $rows = oci_fetch_array($stid, OCI_ASSOC); 
     $cust_id = $rows["ID"]; 
     oci_free_statement($stid); 
     exit("Person Not Found"); 
    } 

?> 
    <table border = "black" /> 
     <tr> 
      <th> ADDRESS </th> 
      <th> AREA </th> 
     </tr> 
    <?php 
     $query1 = "select a.address, a.area from customer c 
       join customer_address ca on c.cust_id = ca.cust_id 
       join address a on a.address_id = ca.address_id where c.cust_id = 
    :id_bv"; 
     $stid1 = oci_parse($link, $query1); 
     oci_bind_by_name($stid1, ":id_bv", $cust_id); 
      oci_execute($stid1); 
      while($row = oci_fetch_array($stid1)) 
      { 
       echo "<tr><td>" . htmlentities($row["ADRESS"]) . "</td>"; 
       echo "<td>" . htmlentities($row["AREA"]) . "</td></tr>"; 
      } 
      oci_free_statement($stid1); 
      oci_close($link); 
     ?> 
     </table> 
      </body> 
     </html> 
+0

它給錯誤的結果爲現有phone_no。它不會爲任何現有電話號碼返回任何行。 'oci_num_rows'= 0即使是現有的pnoe no。告訴我如何將CUST_ID的值存儲在由第一個查詢檢索的變量中,即「從customer where cust_phone =:ph_bv」中選擇cust_id。 –

+0

所有輸入的結果仍然相同。查詢在sql * plus中完美運行。我想問一個問題,什麼是$行[「ID」]。它存儲什麼價值?它是我數據庫中的列名,因爲我的數據庫中的列是'cust_id'。 –

+0

是的ID應該是列名。我認爲這是專欄,所以沒有更新代碼 – gyaan

0
 <!DOCTYPE HTML> 
<html> 
<body> Details of: <?php echo htmlentities($_GET["cust_phone"]) . "<br>"; 
    $link = oci_connect('hd','hd', 'localhost/mydb'); 
    if(!$link) { 
     $e = oci_error(); 
     exit('Connection error ' . $e['message']); 
    } 
    $ph = htmlentities($_GET["cust_phone"]); 

    $q1 = "select CUST_ID from customer where CUST_PHONE = :bv_ph"; 
    $q1parse = oci_parse($link, $q1); 
    oci_bind_by_name($q1parse, ':bv_ph', $ph); 

    oci_execute($q1parse); 
    oci_fetch($q1parse); 
    $res = oci_result($q1parse, 'CUST_ID'); 
    if(!$res) { 
     exit("Person Not Found"); 
    } 
    ?> 
    <table border = "black"> 
     <tr> 
      <th> ADDRESS </th> 
      <th> AREA </th> 
     </tr> 
    <?php 

     $q2 = "select A.ADDRESS, A.AREA from customer c 
      join customer_address ca on C.CUST_ID = CA.CUST_ID 
      join address a on A.ADDRESS_ID = CA.ADDRESS_ID where C.CUST_ID = :id_bv"; 
     $q2parse = oci_parse($link, $q2); 
     oci_bind_by_name($q2parse, ':id_bv', $res); 
     oci_execute($q2parse); 
     while($row = oci_fetch_array($q2parse)) { 
       echo "<tr><td>" . htmlentities($row["ADDRESS"]) . "</td>"; 
       echo "<td>" . htmlentities($row["AREA"]) . "</td></tr>"; 
     } 
     oci_free_statement($q2parse); 
     oci_close($link); 
    ?> 
    </table> 


</body>