【发布时间】:2016-01-25 14:08:02
【问题描述】:
对不起,如果我的问题标题令人困惑..我不知道该怎么说。但这就是:
所以..我试图在我的数据库中获取数据,将其放入
<select><option>
标签。并回应它。
addtocart.php 这是我的第一个代码,它工作正常,具有预设值
<?php
define('INCLUDE_CHECK',1);
require "../connectDB.php";
if(!$_POST['img']) die("There is no such product!");
$img=mysql_real_escape_string(end(explode('/',$_POST['img'])));
$row=mysql_fetch_assoc(mysql_query("SELECT * FROM rooms_addons WHERE img='".$img."'"));
echo json_encode(array(
'status' => 1,
'id' => $row['id'],
'price' => (float)$row['price'],
'txt' => '<table width="65%" id="table_'.$row['id'].'">
<tr>
<td width="5%" style="display:none;"><input type="text" name="id[]" value="'.$row['id'].'" style="width: 28px; border-width: 0px;"></td>
<td width="35%"><input type="text" name="roomname[]" value="'.$row['name'].'" style="width: 95px; border-width: 0px;"></td>
<td width="20%">Php <input type="text" name="price[]" value="'.$row['price'].'" style="width: 40px; border-width: 0px;"></td>
<td width="15%"><select name="qty[]" id="'.$row['id'].'_cnt" onchange="change('.$row['id'].');" style="width: 40px;">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option></select>
</td>
<td width="15%"><a href="#" onclick="window.remove('.$row['id'].');return false;" class="remove">remove</a></td>
</tr>
</table>'
));
?>
现在我的问题是,就像我所说的那样,我想将“数量”数字(在我的数据库表中)放入
<select><option>
..这就是我所做的:
<?php
define('INCLUDE_CHECK',1);
require "../connectDB.php";
if(!$_POST['img']) die("There is no such product!");
$img=mysql_real_escape_string(end(explode('/',$_POST['img'])));
$row=mysql_fetch_assoc(mysql_query("SELECT * FROM rooms_addons WHERE img='".$img."'"));
echo json_encode(array(
'status' => 1,
'id' => $row['id'],
'price' => (float)$row['price'],
'txt' => '<table width="65%" id="table_'.$row['id'].'">
<tr>
<td width="5%" style="display:none;"><input type="text" name="id[]" value="'.$row['id'].'" style="width: 28px; border-width: 0px;"></td>
<td width="35%"><input type="text" name="roomname[]" value="'.$row['name'].'" style="width: 95px; border-width: 0px;"></td>
<td width="20%">Php <input type="text" name="price[]" value="'.$row['price'].'" style="width: 40px; border-width: 0px;"></td>
<td width="15%">
$qty = $row["qty"];
<select name="'.$row['id'].'_cnt" id="'.$row['id'].'_cnt" onchange="change('.$row['id'].');">
for ($i=0;$i<=$qty;$i++) {
<option>' . $i . '</option>
}
</select>
</td>
<td width="15%"><a href="#" onclick="window.remove('.$row['id'].');return false;" class="remove">remove</a></td>
</tr>
</table>'
));
?>
我的 for 循环单独运行良好,但是当我将它放入 echo json_encode 时,在 addtocart.php 中整个 addtocart.php 不起作用。
我将 for 循环用于选择选项,因为它是为了在下拉列表中显示数量,例如如果数据库中的内容是 5,那么它会在下拉列表中显示为 1 2 3 4 5。
我不知道如何修复我的代码,我想不出任何其他的东西来代替选择产品/项目数量的下拉列表:(有什么建议吗?..提前谢谢
【问题讨论】:
-
请stop using
mysql_*functions。 These extensions 已在 PHP 7 中删除。了解PDO 和 MySQLi 的 prepared 语句并考虑使用 PDO,it's really pretty easy。