如何将PHP生成的Dropdown放置到HTML表格的Manufacturer列右侧?
解决方案:将PHP生成的下拉框移到Manufacturer表格单元格
很简单,你只需要把生成下拉框的PHP代码移动到Manufacturer对应的右侧<td>标签内部,替换掉原来那个空的input就可以了。我已经帮你调整好了完整代码,直接用就行:
<form action="SubmitProduct.php" method="post"> <table> <tr> <td>Name</td> <td><input type="text" name="Name" required /><br></td> </tr> <tr> <td>Product Description</td> <td><input type="text" name="Product_Description" required /><br></td> </tr> <tr> <td>Product Price</td> <td><input type="text" name="ProductPrice" required /><br></td> </tr> <tr> <td>Product Image</td> <td><input type="text" name="ProductImage" required /><br></td> </tr> <tr> <td>Manufacturer</td> <td> <?php include 'db_connection.php'; $conn = OpenCon(); // 移除了连接成功的提示,避免破坏页面布局(调试可在后台输出) $query = "SELECT * FROM Manufacturer WHERE 1"; $result = mysqli_query($conn, $query); echo '<select name="ManufacturerName" style="width:150px;" required>'; // 添加默认提示选项,引导用户选择 echo '<option value="">请选择制造商</option>'; while($r = mysqli_fetch_assoc($result)){ echo '<option value="'.$r['ManufacturerName'].'">'.$r['ManufacturerName'].'</option>'; } echo '</select>'; ?> <br> </td> </tr> </table> <!-- 补充提交按钮,若已有可忽略 --> <input type="submit" value="提交产品"> </form>
关键调整说明:
- 把原来在表格外的PHP代码完全移入Manufacturer行的第二个
<td>中,替换掉空input,确保下拉框处于正确的布局位置。 - 将下拉框的
name改为ManufacturerName,语义更清晰,方便你在SubmitProduct.php中通过$_POST['ManufacturerName']获取选中值。 - 移除了前端显示的连接成功提示,避免破坏页面布局(如需调试数据库连接,可在后台日志输出)。
- 给下拉框添加了默认提示选项和
required属性,强制用户选择制造商,保证表单数据完整性。 - 顺便修正了原代码中
Product Desription的拼写错误(漏了字母c),改为正确的Product Description,避免表单字段名出现问题。
这样调整后,下拉框就会准确出现在Manufacturer对应的单元格内,不会再跑到页面右上角啦~
内容的提问来源于stack exchange,提问作者Lagartist
相关产品推荐
相关产品推荐

