我有两张桌子可以说booking
和supplier
从两个表中获取记录的日期范围如下
$query = "SELECT booking.book_id AS Id,
booking.referance_no AS RefNo,
booking.entry_date AS DepDate,
booking.name AS Name,
booking.mobile AS mobile,
booking.comp_title AS Company
FROM booking WHERE active='1' $WHERE1
GROUP BY booking.book_id
UNION ALL
SELECT supplier.id AS Id,
supplier.reference_no AS RefNo,
supplier.departure_date_time AS DepDate,
supplier.name AS Name,
supplier.mobile AS Mobile,
supplier.company AS Company
FROM supplier WHERE active='1' $WHERE2
ORDER BY `DepDate` DESC LIMIT 1000";
注意:我删除了许多与此问题无关的代码行,因此$WHERE1
和$WHERE2
只是日期范围子句。
查询后将数据(while循环)提取到HTML表格
<td><?php echo $row['RefNo'];?></td>
<td><?php echo $row['Name'];?></td>
<td><?php echo $row['Mobile'];?></td>
<td><?php echo $row['DepDate'];?></td>
<td><?php echo $row['Company'];?></td>
<td><a class="btn" href="delete.php?RefNo=<?php echo $row['RefNo'];?>">Delete</a></td>
在HTML视图中,我知道<?php echo $row['RefNo'];?>
中的href
属于哪个表booking
或supplier
,但PHP并不知道{{1}我已经调用了两个表,首先必须针对每个表检查delete.php
,然后是否RefNo
删除记录
delete.php
true
问题是,是否有更好的方法来删除记录,我不必调用这两个表,并首先针对每个表检查 $ReferenceNo = $_GET['RefNo'];
//Fetch records from both tables
//Check records against `$ReferenceNo`
//If true against `booking` table
"Delete From booking where referance_no=$ReferenceNo"
//else
"Delete From supplier where reference_no=$ReferenceNo"
。
编辑以使问题更清晰:
正如我上面提到的,PHP并不知道RefNo
属于哪个表<?php echo $row['RefNo'];?>
或booking
所以我需要在任何操作之前的地方工作(删除,取消,编辑)我可以告诉PHP supplier
属于此表<?php echo $row['RefNo'];?>
或booking
,因此在执行任何操作supplier
<之前,无需针对这两个表检查<?php echo $row['RefNo'];?>
/ p>
答案 0 :(得分:1)
html表中的每一行都有:
<input type="hidden" name="myIncrNNN" value="tableX">
这是不可见的,并且被提起以告知您在处理时该做什么。
你的
<td><a class="btn" href="delete.php?RefNo=<?php echo $row['RefNo'];?>">Delete</a></td>
将被修改以获取隐藏的列线索。
你现在最终会致电:
http://example.com/somedir/delete.php?RefNo=7
在我想象的世界中它会成为
http://example.com/somedir/delete.php?RefNo=7&tc=1
其中tc表示来自隐藏输入字段的表格线索
不需要架构更改和添加表,很明显客户端正在告诉服务器该做什么,没有比原来面对的更多或更少,并且没有说客户端是一个任何事情的权威,如@Halcyon所暗示的那样。
$query = "SELECT booking.book_id AS Id,
booking.referance_no AS RefNo,
booking.entry_date AS DepDate,
booking.name AS Name,
booking.mobile AS mobile,
booking.comp_title AS Company,
'booking' as TableClue -- <------ RIGHT THERE
FROM booking WHERE active='1' $WHERE1
GROUP BY booking.book_id
UNION ALL
SELECT supplier.id AS Id,
supplier.reference_no AS RefNo,
supplier.departure_date_time AS DepDate,
supplier.name AS Name,
supplier.mobile AS Mobile,
supplier.company AS Company,
'supplier' as TableClue -- <------ RIGHT THERE
FROM supplier WHERE active='1' $WHERE2
ORDER BY `DepDate` DESC LIMIT 1000";
答案 1 :(得分:0)
您正在处理非规范化数据。实质上,您有两个包含相同数据的表(更多或更少)。要标准化,您必须添加一个表格,将reference_no
映射到booking
和supplier
中的条目。您可以查询该表以查看要删除的记录,或者设置完整的英里foreign-key cascade。
这样的事情:
CREATE TABLE `booking` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`reference_no` int(11) NOT NULL,
`etc` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `supplier` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`reference_no` int(11) NOT NULL,
`etc` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `reference_no` (
`reference_no` int(11) NOT NULL,
`booking` int(11) DEFAULT NULL,
`supplier` int(11) DEFAULT NULL,
KEY `booking` (`booking`),
KEY `supplier` (`supplier`),
CONSTRAINT `reference_no_ibfk_4` FOREIGN KEY (`booking`) REFERENCES `booking` (`id`) ON DELETE CASCADE,
CONSTRAINT `reference_no_ibfk_5` FOREIGN KEY (`supplier`) REFERENCES `supplier` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
但在实践中,您可能不必这样做。两个表上的删除对我来说似乎没问题。
答案 2 :(得分:0)
首先必须针对每个表检查RefNo,然后如果是,则删除记录
不,不。只需发出DELETE
语句:
DELETE FROM booking WHERE referance_no=$ReferenceNo
DELETE FROM supplier WHERE reference_no=$ReferenceNo
如果执行DELETE
语句时不存在匹配的记录,则不会删除任何记录。默认行为是您想要的...删除匹配记录(如果存在)。