Alternative of DLookup in PHP

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • irslan rafique
    New Member
    • Oct 2014
    • 40

    Alternative of DLookup in PHP

    Please help me to fetch data from table B to table A:

    Its my input form coding(insertda ta.php):
    Code:
        <?php include ( 'header.html');
             include ('selectdriver.php'); 
             include ('selectdestination.php'); 
             include ('selectloading.php'); 
        
        ?>
             
        <head>
        <form name="regisform" action="insertdata.php" method="POST" valign="top" >
                <h3> ADD RECORDS</h3>
                
                &nbsp; &nbsp;Select Date :&nbsp; &nbsp; &nbsp;<input type="date" name="date" value=""  /> 
                &nbsp; &nbsp;Select Driver :&nbsp; <select name="driver" style="width: 163px; align: left">
                    <?php 
        			foreach ($name as $value):
        				echo '<option value="' . $value . '">' . $value . '</option>';
        			endforeach;
        			?>
        			</select><br/>  <br/>  
        
                &nbsp; &nbsp;Select Truck No:&nbsp; &nbsp;<input type="text"  name="truck_no" value="" style="width: 170px; align: left"  /><br/>  <br/>   
                       
              &nbsp; &nbsp;Loading Point: <select name="loading" style="width: 163px; align: left">
                    <?php 
        			foreach ($loading as $value):
        				echo '<option value="' . $value . '">' . $value . '</option>';
        			endforeach;
        			?>
        			</select><br/>  <br/>       
               
               &nbsp; &nbsp;Destination :&nbsp;&nbsp;&nbsp;  &nbsp;   &nbsp;   &nbsp;   &nbsp; <select name="destination" style="width: 163px; align: left">
                    <?php 
        			foreach ($destination as $value):
        				echo '<option value="' . $value . '">' . $value . '</option>';
        			endforeach;
        			?>
        			</select><br/>  <br/>       
                     
                 &nbsp; &nbsp;1st Drop :&nbsp;&nbsp;  &nbsp;   &nbsp;   &nbsp;   &nbsp; <select name="dropone" style="width: 100px; align: left">
                    <option value="KL">KL</option>
                    <option value="Shah Alam">Shah Alam</option>
                  </select>
                 
                 &nbsp; &nbsp;2nd Drop :&nbsp;&nbsp;  &nbsp;   &nbsp;   &nbsp;   &nbsp; <select name="droptwo" style="width: 100px; align: left">
                    <option value="KL">KL</option>
                    <option value="Shah Alam">Shah Alam</option>
                    
                  </select>
                 
                 &nbsp; &nbsp;3rd Drop :&nbsp;&nbsp;  &nbsp;   &nbsp;   &nbsp;   &nbsp; <select name="dropthree" style="width: 100px; align: left">
                    <option value="KL">KL</option>
                    <option value="Shah Alam">Shah Alam</option>
                    
                  </select>
               
                 &nbsp; &nbsp;4th Drop :&nbsp;&nbsp;  &nbsp;   &nbsp;   &nbsp;   &nbsp; <select name="dropfour" style="width: 100px; align: left">
                    <option value="KL">KL</option>
                    <option value="Shah Alam">Shah Alam</option>
                    
                  </select>
                 &nbsp; &nbsp;5th Drop :&nbsp;&nbsp;  &nbsp;   &nbsp;   &nbsp;   &nbsp; <select name="dropfive" style="width: 100px; align: left">
                    <option value="KL">KL</option>
                    <option value="Shah Alam">Shah Alam</option>
                    
                  </select>
                 &nbsp; &nbsp;6th Drop :&nbsp;&nbsp;  &nbsp;   &nbsp;   &nbsp;   &nbsp; <select name="dropsix" style="width: 100px; align: left">
                    <option value="KL">KL</option>
                    <option value="Shah Alam">Shah Alam</option>
                  </select>
                 
                 &nbsp; &nbsp;7th Drop :&nbsp;&nbsp;  &nbsp;   &nbsp;   &nbsp;   &nbsp; <select name="dropseven" style="width: 100px; align: left">
                    <option value="KL">KL</option>
                    <option value="Shah Alam">Shah Alam</option>
                   </select>       <br/>                  <br/>        
                      
                &nbsp; &nbsp;DO No :&nbsp;&nbsp;&nbsp;  &nbsp;   &nbsp;   &nbsp;   &nbsp; <input type="number" name="do_no" value="" />  
                      <br/> 
                <br/> 
                &nbsp; &nbsp;Select Drops :&nbsp;&nbsp;&nbsp;  &nbsp;  <select name="drops" style="width: 173px; align: left">
                    <option value="1">1</option>
                    <option value="2">2</option>
                    <option value="3">3</option>
                     <option value="4">4</option>
                    <option value="5">5</option>
                    <option value="6">6</option>
                    <option value="7">7</option>
                  </select>
                
                
                      <br/><br/> 
                &nbsp; &nbsp;Commission :&nbsp;&nbsp;&nbsp;  &nbsp;   &nbsp;   &nbsp;   &nbsp; <input type="text" name="commission" value="" /> <br/>    
                      <br/> 
                           
        &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; <input type="submit" value="Save" name="Save" /> 
         &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; <input type="submit" value="Delete" name="Delete" /><br/> <br/> <br/> <br/>  
                </form>
          
                <?php
                
            
                if($_SERVER['REQUEST_METHOD']=='POST')
                {
                    $erArr = array();             
                    require 'connectdb.php';
                   
                    $id= isset($_POST['id']) ? $_POST['id'] : '';
                    $date= $_POST['date'];
                    $driver= $_POST['driver']; 
                    $truck_no= $_POST['truck_no'];
                    $loading= isset($_POST['loading']) ? $_POST['loading'] : '';
                    $destination= $_POST['destination'];
                    $dropone= isset($_POST['dropone']) ? $_POST['dropone'] : '';
                    $droptwo= isset($_POST['droptwo']) ? $_POST['droptwo'] : '';
                    $dropthree= isset($_POST['dropthree']) ? $_POST['dropthree'] : '';
                    $dropfour= isset($_POST['dropfour']) ? $_POST['dropfour'] : '';
                    $dropfive= isset($_POST['dropfive']) ? $_POST['dropfive'] : '';
                    $dropsix= isset($_POST['dropsix']) ? $_POST['dropsix'] : '';
                    $dropseven= isset($_POST['dropseven']) ? $_POST['dropseven'] : '';
                    $do_no= $_POST['do_no'];
                    $drops= $_POST['drops'];
                    $commission= $_POST['commission'];
                    $total= ($drops*10 + $commission);
                    if(empty($_POST['driver']))
                    {
                        $erArr[] ="Mandatory field 'Name' is missing <br/>";
                    }
                                           
                    foreach ($erArr as $msg) //foreach is used here to process the array
                        {
                            echo $msg;
                        }   
                  $sql = "INSERT INTO "
                          . "main (id, date, driver, truck_no, loading, destination, dropone, droptwo, dropthree, dropfour, dropfive, dropsix, dropseven, do_no, drops, commission, total ) VALUES "
                          . "('$id', '$date', '$driver', '$truck_no', '$loading', '$destination', '$dropone', '$droptwo', '$dropthree','$dropfour','$dropfive','$dropsix','$dropseven','$do_no', '$drops', '$commission', '$total' )"; 
        
                  if(!empty($driver))
                     {
                        $insertdb = mysqli_query($dbc, $sql);
                        echo mysqli_error($dbc);//for trobleshooting eror
                        if($insertdb ==1)
                        {
                            echo"The Database has been updated successfully"  . "<br/>";
                        }
                            else 
                                {
                        echo "Please check the error";
                        echo mysqli_error($dbc);
                        }
                      
                     } 
                }
                
                ?>
        
        <?Php
        include 'connectdb.php';
        
        $count="SELECT id, date, driver, truck_no, loading, destination, dropone,droptwo, dropthree, dropfour, dropfive, dropsix, dropseven, do_no, drops, commission, total from main group by id";
        
        echo "<table border =1  bgcolor=white width=1200px fontsize=10>";
        echo "<tr ><th>id</th><th>Date</th><th>Driver</th><th>Truck No.</th><th>Loading</th><th>Destination</th><th>1st Drop</th><th>2nd Drop</th><th>3rd Drop</th><th>4th Drop</th><th>5th Drop</th><th>6th Drop</th><th>7th Drop</th><th>DO No/RM</th><th>Drops</th><th>Commission</th><th>Total</td></tr>";
        foreach ($dbc->query($count) as $row) {
        echo "<tr ><td>$row[id]</td><td>$row[date]</td><td>$row[driver]</td><td>$row[truck_no]</td><td>$row[loading]</td><td>$row[destination]</td><td>$row[dropone]</td><td>$row[droptwo]</td><td>$row[dropthree]</td><td>$row[dropfour]</td><td>$row[dropfive]</td><td>$row[dropsix]</td><td>$row[dropseven]</td><td>$row[do_no]</td><td>$row[drops]</td><td>$row[commission]</td><td>$row[total]</td></tr>";
        
        }
        echo "</table>";
        ?>
    Now i have to put commission manually in commission field of table A (main). I want this commission field fetch the commission from table B (comm) when destination is selected.
    Please check it and advice accordingly.
    Table A name is: main
    Table B name is: comm
    Last edited by Rabbit; May 8 '15, 04:40 PM. Reason: fixed code tags
  • Luuk
    Recognized Expert Top Contributor
    • Mar 2012
    • 1043

    #2
    you have a select statement like this:
    Code:
    SELECT 
      id, 
      date, 
      driver, 
      truck_no, 
      loading, 
      destination, 
      dropone,
      droptwo, 
      dropthree, 
      dropfour, 
      dropfive, 
      dropsix, 
      dropseven, 
      do_no, 
      drops, 
      commission, 
      total 
    from main 
    group by id
    and you do not want to get commission from table main, but from table comm,

    than you should do:
    Code:
    SELECT 
      id, 
      date, 
      driver, 
      truck_no, 
      loading, 
      destination, 
      dropone,
      droptwo, 
      dropthree, 
      dropfour, 
      dropfive, 
      dropsix, 
      dropseven, 
      do_no, 
      drops, 
      comm.commission, 
      total 
    from main
    join comm on <tell SQL how these tables are linked> 
    group by id
    You only have to know with which selection criteria you can select the correct commission (the '<tell SQL how these tables are linked>'-part)

    Comment

    • irslan rafique
      New Member
      • Oct 2014
      • 40

      #3
      Sorry for late reply.
      Can you help me more please in writing sql query...
      Do i need to join the tables as well?
      plz plz help me.
      Thanks in advance

      Comment

      • Luuk
        Recognized Expert Top Contributor
        • Mar 2012
        • 1043

        #4
        I can help, but I don't know what help you need.... ;)

        You only need to join tables when information is needed from more than one table.

        If you want information from table A, you can do
        SELECT a.id FROM main a

        If you also need some info from table B, than:
        SELECT a.id, b.somefield
        FROM main a
        JOIN comm b ON a.id = b.id


        After the 'ON'-clause you should write the proper expression that gives information on how the tables 'main' and 'comm' are related to each other.

        Comment

        Working...