简体   繁体   English

根据用户提供的信息,使用php中的join从两个表的多个列中检索数据

[英]Retrieving data from multiple columns from two tables using join in php, as per the info provided by user

I'm trying to create a basic shipment tracking page for my courier company. 我正在尝试为我的快递公司创建一个基本的货运跟踪页面。 The user will provide the reference number and the query is required to pull the data in different columns from two tables. 用户将提供参考号,并且需要查询才能从两个表的不同列中提取数据。 Below is the HTML code and below that is the php code. 下面是HTML代码,下面是php代码。 The HTML works ok but when I submit the form, the php throws a blank page. HTML可以正常工作,但是当我提交表单时,php会抛出空白页。 Can anyone tell me what mistake I'm doing here. 谁能告诉我我在这里犯什么错误。 Thanks in advance for the help. 先谢谢您的帮助。

HTML Code: HTML代码:

<form action="tracking.php" method="POST" id="insert">
<table>
<tr>
<td>Please enter your 9 digit Docket No:</td>
<td><input type="text" size=9 name="docketno"></td>
</tr>
<tr>
<td colspan=2 id="sub"><input type="submit"onClick="confSubmit(this.form);" name="submit" value="submit" >&nbsp;&nbsp;<input type="reset" value="Reset!"></td>
</tr></Table></form>

PHP Code: PHP代码:

<?php

// contact to database

$connect = mysql_connect('localhost', 'userid', 'password') or die ("Error , check your server connection.");

mysql_select_db("testing", $connect);



//Get data in local variable

$_docketno=$_POST['docketno'];


// check for null values

if ($_docketno=="")

echo "Docket Number must be entered, please hit back button and re-enter Docket Number";

else{

$query = "SELECT shipment_data.docketno, shipment_data.bookingdatetime, shipment_data.customercode, shipment_data.shippername, shipment_data.bookingou, shipment_data.assureddeliverydate, for_delivery_details.deliverydatetime, for_delivery_details.deliverystatus, for_delivery_details.name_and_number_of_the_person_to_whom_del, for_delivery_details.deliveryattemptnumber_1, for_delivery_details.deliveryattemptnumber_2, for_delivery_details.deliveryattemptnumber_3, for_delivery_details.faileddel.attempt,reason_1, for_delivery_details.faileddel.attempt,reason_2, for_delivery_details.faileddel.attempt,reason_3 "
 "FROM shipment_data LEFT JOIN for_delivery_details where docketno=".$_POST['docketno'].
    "ON shipment_data.docketno = for_delivery_details.docketno"; 

$result = mysql_query($query) or die(mysql_error());

// Print out the contents of each row into a table 
while($row = mysql_fetch_array($result)){
    echo Docket Number          " - ". $row[docketno];  
    echo Booking Date Time          " - ". $row[bookingdatetime];
    echo Customer Code          " - ". $row[customercode];
    echo Shipper Name           " - ". $row[shippername];
    echo Booked From            " - ". $row[bookingou];
    echo Assured Delivery Date      " - ". $row[assureddeliverydate];
    echo Delivery Date Time         " - ". $row[deliverydatetime];
    echo Delivery Status            " - ". $row[deliverystatus];
    echo Name Number to whom delivered  " - ". $row[name_and_number_of_the_person_to_whom_del];
    echo First Delivery attempt date    " - ". $row[deliveryattemptnumber_1];
    echo Second Delivery attempt date   " - ". $row[deliveryattemptnumber_2];
    echo Third Delivery attempt date    " - ". $row[deliveryattemptnumber_3];
    echo First delivery, fail reason    " - ". $row[faileddel.attempt,reason_1];
    echo Second delivery, fail reason   " - ". $row[faileddel.attempt,reason_2];
    echo Third delivery, fail reason    " - ". $row[faileddel.attempt,reason_3];

}
?>

MySql tables: MySql表:

create table if not exists `shipment_data` (
  `docketno` int(11) not null,
  `referenceno` int(11) default null,
  `bookingdatetime` datetime not null,
  `customercode` int(11) not null default '9999',
  `shippername` varchar(100) not null,
  `shipperaddress` varchar(255) not null,
  `shipperphone` varchar(12) not null comment 'no special characters like (,),+',
  `shipperemail` varchar(50) null default '@',
  `consigneename` varchar(100) not null,
  `consigneeaddress` varchar(255) not null,
  `consigneephone` varchar(12) not null comment 'no special characters like (,),+',
  `consigneeemail` varchar(50) null default '@',
  `bookingou` varchar(20) not null,
  `deliveryou` varchar(20) not null,
  `assureddeliverydate` date not null,
  `numberofpackages` int(9) not null default '1',
  `firststickernum` int(11) not null,
  `laststickernum` int(11) not null,
  `packagedetails` enum('documents','carton box','gunny bag','wooden box','plastic bag') not null,
  `goodstypecode` int(6) not null,
  `goodsdescription` varchar(255) not null,
  `goodsvalue` decimal(10,0) not null,
  `freestoragedays` smallint(6) not null default '1',
  `customerinvoiceno` varchar(100) default null,
  `waybillnumber` varchar(100) default null,
  `servicecode` varchar(255) not null,
  `servicedetails` varchar(255) not null,
  `riskcoverage` enum('shipper risk','carrier risk') not null,
  `length_inches` decimal(10,2) not null default '1.00',
  `breadth_inches` decimal(10,2) not null default '1.00',
  `height_inches` decimal(10,2) not null default '1.00',
  `volumetricweight` double not null comment '((length x breadth x height)/1728)x10',
  `actualweight_kg` decimal(10,2) not null default '0.10',
  `chargedweight_kg` decimal(10,2) not null default '0.10',
  `totalcharge` decimal(10,2) not null default '1.00',
  `arrivaldateatdel_ou` datetime default null,
  `movementstatus` enum('','in-transit','reached') default null,
  `movementid` varchar(75) default null,
  primary key (`docketno`)
) engine=innodb default charset=utf8;


create table if not exists `for_delivery` (
  `docketno` int(11) not null,
  `assureddeliverydate` date not null,
  `deliverydatetime` datetime null,
  `deliverystatus` enum('','taken for delivery','delivered','undelivered','partial delivery') not null,
  `undeliveredfor18hrs_reason` tinytext null,
  `adicode` varchar(20) not null,
  `adiname` varchar(100) not null,
  `adinumber` int(11) not null,
  `deliveryattemptnumber_1` datetime null,
  `faileddel_attempt_reason_1` varchar(255) null,
  `deliveryattemptnumber_2` datetime null,
  `faileddel_attempt_reason_2` varchar(255) null,
  `deliveryattemptnumber_3` datetime null,
  `faileddel_attempt_reason_3` varchar(255) null,
  `name_and_number_of_the_person_to_whom_del.` varchar(255) null,
  `if_not_the_named_person,_upload_copy_of_identity_proof` blob null,
  `first_noticedate` date null,
  `first_noticenum` int(11) null comment 'after non-delivery 6 days of arrival at ou',
  `second_noticedate` date null,
  `second_noticenum` int(11) null comment 'after non-delivery 15 days of arrival at ou',
  `third_noticedate` date null,
  `third_noticenum` int(11) null comment 'for auction/disposal intimation after non-delivery 30 days of arrival at ou',
  primary key(docketno),
  foreign key (docketno) references shipment_data(docketno) on delete cascade
) engine=innodb default charset=utf8;

Try this: 尝试这个:

if ($_POST['docketno'] == ""){

   echo "Docket Number must be entered, please hit back button and re-enter Docket Number";
}

else{

   $query = "SELECT * FROM shipment_data LEFT JOIN for_delivery_details ON shipment_data.docketno = for_delivery_details.docketno where docketno=".$_POST['docketno']; 

   $result = mysql_query($query) or die(mysql_error());

   // Print out the contents of each row into a table 
   while($row = mysql_fetch_array($result))
   {
       echo "Docket Number           - ". $row[docketno];  
       echo "Booking Date Time           - ". $row[bookingdatetime];
       echo "Customer Code           - ". $row[customercode];
       echo "Shipper Name            - ". $row[shippername];
       echo "Booked From             - ". $row[bookingou];
       echo "Assured Delivery Date       - ". $row[assureddeliverydate];
       echo "Delivery Date Time          - ". $row[deliverydatetime];
       echo "Delivery Status             - ". $row[deliverystatus];
       echo "Name Number to whom delivered   - ". $row[name_and_number_of_the_person_to_whom_del];
       echo "First Delivery attempt date     - ". $row[deliveryattemptnumber_1];
       echo "Second Delivery attempt date    - ". $row[deliveryattemptnumber_2];
       echo "Third Delivery attempt date     - ". $row[deliveryattemptnumber_3];
       echo "First delivery, fail reason     - ". $row[faileddel.attempt,reason_1];
       echo "Second delivery, fail reason    - ". $row[faileddel.attempt,reason_2];
       echo "Third delivery, fail reason     - ". $row[faileddel.attempt,reason_3];

    }
}

Update: 更新:

Method 1 方法1

echo "Docket Number           - ". $row[docketno] ."</br>";

use it in all echo statement same as like this. 像这样在所有echo语句中使用它。

method 2 方法2

Use HTML table tag to display in a table style. 使用HTML 表格标签以表格样式显示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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