单击另一页上的按钮时显示php表中的值

时间:2017-12-15 17:53:14

标签: php html mysql

以下是我创建的用于显示表所有者的餐馆名称,位置和菜单的表格。 现在,列Menu的每一行都有Button作为值。 我的桌子准备好了完美的价值观。

现在我的问题是如何: -

点击每个餐厅对应的按钮后,将打开一个新文件(openmenu.php),并将回显餐厅名称,该餐厅的手机号码和菜单。 但到目前为止,单击每个Button时,我只能显示表格最后一行的上面条目。帮帮我。我是php的新手。

table.php

<?php


include 'nav.php';
$sql = 'SELECT * FROM owners';

$query = mysqli_query($con, $sql);

if (!$query) {
die ('SQL Error: ' . mysqli_error($con));
}


?>

<html>
<head>
<link rel = "stylesheet" type = "text/css" href = "css/style.css">

<style> 
    .data-table{
 width: 1024px;
    margin-left: 150px;
    text-align:center;
     border: 1px solid firebrick;
    background-color: white;

}
td,th{
    border: 1px solid firebrick; padding: 3px 2px 1px 1px;
}


</style>
</head>

<body>

<div class="container">

<article>

<table class="data-table">

    <thead>
        <tr>

            <th>Restuarant Name</th>
            <th>Location</th>
            <th>Menu</th>
        </tr>
        <tr>

        </tr>
    </thead>
    <tbody>
    <?php

    while ($row = mysqli_fetch_array($query)){

   $_SESSION['resphone'] = $row['resphone'];
      $_SESSION['restaur'] = $row['restaur'];
        echo '<tr>


                <td>'.$row['restaur'].'</td>
                <td>'.$row['loc'].'</td>

                <td style="background-color:firebrick;"><form method="post" action="openmenu.php?id=$row[restaur]"><input value="<?php echo $restaur;?>" type="hidden">
 <input type="submit"  value="View"></form></td>
            </tr>';


    }


        ?>

            </tbody>

</table>


</form>







</article>



</div>

</body>
</html>

openmenu.php

<?php 


include('nav.php');


?>

<html>
<head>

 <link rel="stylesheet" href="css/style.css">
 <style>
table, td {
border: none;
 text-align: center;
    text-align-last: center;
 }

</style>
</head>
<body>

<div class="container">




<article>

<form  method="get" align="center"  action="" class="formwrap" enctype='multipart/form-data'>
<h1><?php $restaur = $_SESSION['restaur'];
echo $restaur ;?></h1>
<h1>Call to Order:</h1>
<?php $resphone = $_SESSION['resphone'];

echo $resphone;

?>

<br>
<br>
 <?php


 $sql = "select img from owners where restaur ='$restaur'";
 $result = mysqli_query($con,$sql);
 $row = mysqli_fetch_array($result);

 $image_src2 = "upload/".$row['img'];


?>
<img src='<?php echo $image_src2; ?>' >   

    </form>

</article>


 </div>

  </body>
</html>

2 个答案:

答案 0 :(得分:0)

first you getting data from database  & then use view button for openmenu.php but why u use this way 
<form method="post" action="openmenu.php?id=$row[restaur]"><input value="<?php echo $restaur;?>" type="hidden"><input type="submit"  value="View"></form>

答案 1 :(得分:0)

第1期

在此代码段中,您将会话变量resphonerestaur设置为您当前正在迭代的商店的值。一遍又一遍地。这就是为什么你只得到最后一个商店的信息 - 这是你设置这些变量的最后一件事。

while ($row = mysqli_fetch_array($query)){

  $_SESSION['resphone'] = $row['resphone'];
  $_SESSION['restaur'] = $row['restaur'];

第2期

您应该更改form方法以获取并丢弃未使用的隐藏输入,如下所示:

<form method="post" action="openmenu.php?id=<?=$row['restaur']?>">
  <input type="submit"  value="View">
</form>

或者更有可能只是将其更改为普通的a链接:

<a href="openmenu.php?id=<?=$row['restaur']?>">View</a>

第3期

您完全忽略openmenu.php中请求的商店ID。您正在使用$_SESSION$_REQUEST$_GET。我不打算举例说明你应该怎么做。相反,请在进一步移动之前参考this answer