dourui7186 2015-08-31 12:43
浏览 57
已采纳

处理两个不同的表时如何从表中删除记录

I've two tables let's say booking and supplier

Fetching the records from both tables with-in date-range as follow

$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";

Note: I remove lots of code lines as they are not relevant to this question so as $WHERE1 and $WHERE2, they are just date range clause.

After query fetching data (while loop) to HTML table

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

In HTML view, I know that <?php echo $row['RefNo'];?> in href belongs to which table booking Or supplier but PHP doesn't know it and on delete.php I've to call both tables and first have to check the RefNo against each table and then if it's true delete the record

delete.php

 $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"

The question, is there better approach to delete the record where I don't have to call both tables and first check RefNo against each table.

Edit to make Question more clear:

As I mentioned somewhere above that PHP doesn't know <?php echo $row['RefNo'];?> belongs to which table booking or supplier so I need the work around where before any action (Delete, Cancel, Edit) I can tell PHP that <?php echo $row['RefNo'];?> belongs to this table booking or supplier so no need to check <?php echo $row['RefNo'];?> against both tables before any action Delete, Cancel, Edit

  • 写回答

3条回答 默认 最新

  • douhui1957 2015-08-31 12:51
    关注

    Each row in html table has something like:

    <input type="hidden" name="myIncrNNN" value="tableX">
    

    that is not visible, and picked up to clue you in to what to do upon processing.

    Edit: to make it more clear

    your

    <td><a class="btn" href="delete.php?RefNo=<?php echo $row['RefNo'];?>">Delete</a></td>
    

    Would be altered to pick up the hidden column clue.

    Yours now would be an ultimate call to :

    http://example.com/somedir/delete.php?RefNo=7
    

    in my imaginary world it would become

    http://example.com/somedir/delete.php?RefNo=7&tc=1
    

    Where tc means table clue from the hidden input field

    Does not require a schema change, and added table, and is obvious that the client is telling the server what to do, no more or less than the original in the face of it all, and does not say the client is an Authority of anything, like @Halcyon is suggesting.

    Edit 2: (to show UNION chg)

    $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";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。