2

我遇到了一个问题。我有一个表单,我正在尝试添加一个编辑页面以插入到 mysql 中。我从一张桌子上得到所有房间,然后我必须检查它们是否在另一张桌子上“检查”。

Table 1 (HotelRooms):
ID | HotelRoom | Hid
---------------------
1  | Standard  | 20
2  | Deluxe    | 20
2  | Basic     | 20

Table 2 (HotelPromos)
ID | Promo | Rooms | Hid
-------------------------
1  | 10%   | deluxe, standard | 20

现在在我的表格中,我首先需要找到属于 Hotel with Hid 20 的所有房间。所以这是我的选择声明:

<?php 
$getRooms = mysql_query("SELECT HotelRoom FROM HotelRooms WHERE HotelId = '$hid'") or die(mysql_error());
$rr = 1;
$numRooms = mysql_num_rows($getRooms);
if($numRooms == 0){
    echo "Lo sentimos pero no hay habitaciones registrados para este hotel. Favor de agregar habitacion para poder agregar una promocion.";
    }
while($ro = mysql_fetch_array($getRooms)){
    $roomName = $ro['HotelRoom'];
    ?>
    <input type="checkbox" name="room<?php echo $rr ?>" /><?php echo $roomName ?>
    <?php
    $rr++;
    }
?>

有谁知道我如何检查 HotelPromos 表以查看它是否在“房间”列下并添加一个已选中的 =“已检查”?

任何帮助将不胜感激!!!

4

2 回答 2

1

I'm assuming that Basic's ID is actually 3 not 2 and that is a typo.

First of all, in HotelPromos, you should use the room IDs (1, 2) and not the names (Deluxe, Standard).

Second, you should split HotelPromos into two tables to comply with "First Normal Form", part of Database Normalization.

Yes, this is a little more difficult to design and you will have to modify your code, but it makes complex queries much easier to write.

PromoCodes
ID | Promo
----------
1  | 10%

HotelPromos
RoomID | PromoID
----------------
1      | 1
2      | 1

Now you can get all rooms matching a hotel ID

SELECT HotelRooms.ID, HotelRooms.HotelRoom as Name, HotelPromos.PromoID
    FROM HotelRooms
    LEFT JOIN HotelPromos
        ON HotelRooms.ID = HotelPromos.RoomID
    WHERE HotelId = '$hid'

The LEFT JOIN means I want all rooms in Hotel 20 regardless of whether or not they have a promo. The PromoID will be either the ID of the promo code or NULL if no code is available, and you can use this to decide whether or not to check the checkbox.

while( $ro = mysql_fetch_assoc($getRooms) ){
    $roomName = $ro['Name'];
?>
<input type="checkbox" name="room<?= $rr ?>" <?php if( $ro['PromoID'] ) { echo 'checked="checked" '; } ?>/><?php echo $roomName ?>
<?php
    $rr++;
}

And one final thing, I think you should be using $ro['ID'] instead of a new variable $rr

于 2012-06-30T00:46:54.040 回答
0

不确定您的表架构,但类似于以下内容。然后检查是否设置了id。PS,逃避你的输入!

SELECT HotelRoom, hp.id
FROM HotelRooms AS hr
LEFT OUTER JOIN HotelPromos AS hp ON hp.hid=hr.id
WHERE HotelId = '$hid'"
于 2012-06-29T20:27:05.477 回答