php - 从 Tinyint 中检索并转换为 html 中的文本

标签 php mysql

我有一个通过复选框设置产品属性的表单。表单将复选框发送到数据库,数据库将其存储为 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/

相关文章:

php curl - 允许使用过期证书进行调用

php - Woocommerce 从不在结帐页面上保存送货地址

javascript - 提交前的 Ajax 验证

javascript - 使用 javascript 进行 PHP 和 HTML 表搜索

PHP/Ajax DataGrid 永远不会显示在真实网络服务器的页面上,仅显示本地主机 :(

mysql - PhpMyAdmin 将安装在我的系统上的什么位置?

php - FullCalendar on Wordpress 集成 - wpdb 和 json 问题

php - 将用户数据存储在 $_SESSION 中与重复的数据库访问

MySQL:如果没有恰好重复 4 次,则选择 id

mysql - 如何在TypeORM迁移中创建自增整数字段?