I am using the primary key of the room_extras table to create a unique name for my extra text field.
Here’s the example:
<tr>
<td><?php echo $extrasInfo['description'] ?></td>
<td>
<input type="text" name="extra[<?php echo $extrasInfo['re_id'] ?>]" id="qty" value="0" style="text-align: right" size="5" onKeyPress="return isNumberKey(event)" />
</td>
<td align="right"><?php echo number_format($extrasInfo['price'],2) ?></td>
</tr>
As you can see the name is generated using this code extra[<?php echo $extrasInfo['re_id'] ?>].
The result of this in html is:
<tr>
<td colspan="3"><h3>Economy Room</h3></td>
</tr>
<tr>
<td>Extra Person</td>
<td><input type="text" name="extra[19]" id="qty" value="1" style="text-align: right" size="5" onKeyPress="return isNumberKey(event)"></td>
<td align="right">560.00</td>
</tr>
<tr>
<td>Breakfast for Extra Person</td>
<td><input type="text" name="extra[20]" id="qty" value="1" style="text-align: right" size="5" onKeyPress="return isNumberKey(event)"></td>
<td align="right">150.00</td>
</tr>
<tr>
<td colspan="3"><h3>Deluxe</h3></td>
</tr>
<tr>
<td>Extra Person</td>
<td><input type="text" name="extra[1]" id="qty" value="0" style="text-align: right" size="5" onKeyPress="return isNumberKey(event)"></td>
<td align="right">730.00</td>
</tr>
<tr>
<td>Breakfast for Extra Person</td>
<td><input type="text" name="extra[2]" id="qty" value="0" style="text-align: right" size="5" onKeyPress="return isNumberKey(event)"></td>
<td align="right">269.00</td>
</tr>
Depending on the value of re_id field (i.e. $extrasInfo['re_id']) the array return a different key like:
extra[19]
extra[20]
extra[1]
extra[2]
So my question is how do I get the value after “extra”? That is, the numbers 19, 20, 1, and 2.
Here’s what I have started:
$size = count($_POST['extra']);
$i = 0;
while ($i < $size) {
$qty = $_POST['extra'][$i];
$re_id = ????; //what should be the code here?
$query = "$sql_extras="INSERT INTO `lf_bookings_extras`(`re_id`, `booking_number`, `price`, `qty`, `amount`) VALUES ('$re_id','$booking_number','$price','$qty','$amount')";";
mysql_query($query) or die ("Error in query: $query");
++$i;
}
You should not name your
extraarray from form input, that way you can loop in that array.and HTML
or if you really want to do that
PS: please, don’t use
mysql_*functions for new code. They are no longer maintained and the community has begun the deprecation process. See the red box? Instead you should learn about prepared statements and use either PDO or MySQLi. If you can’t decide, this article will help to choose. If you care to learn, here is a good PDO tutorial.