简体   繁体   中英

Displaying Information based on entered value in PHP with Oracle Database 11g R2

I have a simple form (main.php) which takes input as a Phone no. of the customer:

    <!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>

The no. entered is checked in the Oracle DB and if a customer is present with the no. then information regarding that customer is displayed else a new customer is added with the phone no. (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>

The first part of the code works fine and it displays the message "Person Not Found". However the second part gives error: Details of: 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

I have two questions: 1. Instead of "person not found", I want to add a new customer in my DB? 2. How to resolve these errors? I am new to PHP and this is just my first code. Any help appreciated.

$row don't have the record so you can't access $row["ID"] so first, check if records are there then only access the data.

Second problem is with your query, cust_id field is in two tables so use it with aliases while writing queries.

try with below code:-

    <!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>
     <!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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM