我有一个通过复选框设置产品属性的表单。表单将复选框发送到数据库,数据库将其存储为 0 或 1。如果为 0,则该产品不存在该属性,如果为 1,则该属性存在。我希望其中一些显示属性名称,而不是“1”或“0”。例如,如果它是水平方向的,我希望它显示“水平”。
我现在尝试从数据库中选择这些结果并将它们显示在我的表格上。这是我表中的代码。
include 'connect.php';
$result = mysql_query ("SELECT * FROM inventory");
echo "Inventory";
echo "<table border='1' id='listings1table'>";
echo "<tr><th colspan='6'>inventory list</th></tr>";
echo "<tr><td>ID #</td><td>Size</td><td>Material</td><td>Orientation</td><td>Has Photo</td><td>Manage</td></tr>";
while ($row = mysql_fetch_array($result))
{
echo "<tr><td>" . $row[id] . "</td><td>" . $row['size'] . " " . "Gallons" . "</td><td>" . $row['material'] . "</td>";
echo "<td>" . $row['horizontal'] . "</td><td> No </td>";
echo "<td><a href='deleteitem.php?id=" . $row['id'] . "'>Delete</a></td></tr>";
echo "<br />";
}
echo "</table>";
?>
我该怎么做?我的插入页面看起来像
include 'connect.php';
$horizontal = isset($_POST['horizontal']) ? 1 : 0;
$sql="INSERT INTO inventory (horizontal, vertical) VALUES (''$horizontal', '$vertical')";
if (!mysql_query($sql,$con))
{
die ('Error : ' . mysql_error());
}
echo "<br /><br /><center><font size='5' face='Arial' color='green'>1 record added</font></center>";
echo '<meta http-equiv="refresh" content="2; index.php" />';
mysql_close($con)
?>
最佳答案
试试这个
while ($row = mysql_fetch_array($result)) : ?>
<tr>
<td><?php echo $row['id'] ?></td>
<td><?php echo $row['size'] ?> Gallons</td>
<td><?php echo $row['material'] ?></td>
<td><?php echo $row['horizontal'] == 1 ? 'Horizontal' : 'Not Horizontal?' ?></td>
<td>No</td>
<td><a href="deleteitem.php?id=<?php echo $row['id'] ?>">Delete</a></td>
</tr>
<?php endwhile ?>
</table>
或者类似的东西。
关于php - 从 Tinyint 中检索并转换为 html 中的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7265331/