如何删除当前页面中的数据,然后使用后退按钮重定向到另一页面

时间:2019-02-03 09:34:17

标签: php html mysqli

我当前的页面是displayitems.php。我得到了一个按钮type='hidden',其样式和按钮的背景图片以及一个隐藏的input type ='hidden'。

单击后可以检索图像的ID。每当我单击按钮(带有图像)时,它将在另一个页面userdetails.php中显示按钮的确切图像,并且我得到一个后退按钮,它将重定向到displayitems.php。

现在我的问题是,如果我单击另一个按钮(图像),它将显示过去的图像,而不是新的图像。你们中的任何人都可以删除过去的图像,然后显示我单击的当前按钮

这是我的主页

<?php
  require 'header.php';
?>
  <main>
<?php
  require 'uploaditems.php';
  echo "<br>";
  require 'loginlogout.php';
  echo "<div>";
  require 'displayitems.php';
  echo "</div>";
?>
</main>

我的displayitems.php

<?php
if(isset($_SESSION['userId'])){
   require "includes/dbh.php";
   ?>
   <!DOCTYPE html>
   <html>
   <head>
     <meta charset="utf-8" />
     <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <title>Page Title</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="style.css" type="text/css" media="screen" href="style.php"/>
    <script src="main.js"></script>
    <style>
      .product{
          border:1px solid
          margin:-1px 19px 3px -1px;
          padding: 10px;
          text-align:center;
          bacgkround-color:#efefef;
      }
    </style>
    </head>
    <body>

    <?php
    include_once 'includes/dbh.php';

    $sql="SELECT * FROM gallery ORDER BY orderitems DESC;";
    $stmt=mysqli_stmt_init($conn);
    if(!mysqli_stmt_prepare($stmt,$sql)){
       echo "sql statement failed in displayitems.php";            
    }else{
       mysqli_stmt_execute($stmt);
       $result=mysqli_stmt_get_result($stmt);
       if(mysqli_num_rows($result)>0){

          while($row=mysqli_fetch_assoc($result)){
             ?>
             <div class="col-md-3">
             <form method="POST" action="userdetails.php">
               <input type="hidden" name="hiddenId" value=<?php echo $row['idGallery'];?>>
               <div class="product" style="float: left;">
                 <button type="hidden" name='displaydetails' 
                 style='background- 
   image:url(images/<?php echo $row['imgFullNameGallery'];?>
  );width:150px; height:200px; margin:auto; background-size:100% 100%; b 
   ackground-repeat:no-repeat;>
                 </button>
             </form>';
             <br>
             <br><br><h3><?php echo $row['nameitem']?></h3>
             <h3><?php echo $row['price']?></h3>
             <br>
             </div>
             </div>
             <?php
         }
      }
   }
}
?>
</body>
</html>

和我的userdetails.php

  <?php
    session_start();
  if(isset($_POST['displaydetails'])){
 if(isset($_SESSION['userId'])){

这就是我获取图像的隐藏ID

  $idofimg=$_POST['hiddenId'];
  require 'includes/dbh.php';

然后我创建了一个查询,我想要所有隐藏ID的数据

  $sql="SELECT * FROM gallery WHERE idGallery=? ;";
  $stmt=mysqli_stmt_init($conn);
  if(!mysqli_stmt_prepare($stmt,$sql)){

 echo "sql statement failed in displayitems.php";            
 }else{
   mysqli_stmt_bind_param($stmt,'i',$idofimg);
  mysqli_stmt_execute($stmt);
   $result=mysqli_stmt_get_result($stmt);
   if(mysqli_num_rows($result)>0){
   while($row=mysqli_fetch_assoc($result)){
      ?>
    <!DOCTYPE html>
     <html>
     <head>
         <meta charset="utf-8" />
         <meta http-equiv="X-UA-Compatible" content="IE=edge">
         <title>Page Title</title>
         <meta name="viewport" content="width=device-width, initial- 
 scale=1">
         <link rel="stylesheet" type="text/css" media="screen" 
   href="main.css" />
         <script src="main.js"></script>
     </head>   
     <body>
           <img src="images/<?php echo $row['imgFullNameGallery'];?>" style 
   =width:330px; height:300px; margin:auto; background-size:100% 100%; 
   background-repeat:no-repeat;>
         <div>
        <h3>Details</h3>
         <table>

             <tr>
                <td>Condition:</td>
                 <td>ha</td>
           </tr>
    </div>

这是我要放置条件的地方,我将删除所有当前数据并返回index2.php,然后单击另一个图像按钮时,它将显示当前图像而不是过去的图像

     <form action='index2.php' method='POST'>
      <button type='submit'>BACK</button>
      <?php
    $count=1;
       ?>
       </form>
       <?php
     }
    }   
      }

  } else{
  echo "fail";
  }}
 ?>
      </body>

    </html>

1 个答案:

答案 0 :(得分:0)

在我的displayitems.php中,当按钮type =“ hidden” name ='displaydetails'>的点击没有获得任何值时,我的输入type =“ hidden” name =“ hiddenId” value =>仅获得1个值。因此,当我转到userdetails.php时,由于输入类型的缘故,它仅显示1个详细信息。所以我应该在displayitems.php中删除输入类型,只给按钮一个值,这样每当单击按钮时,它都会生成一个新的ID并显示一个新的详细信息。

只需将其更改为displayitems.php

  <input type="hidden" name="hiddenId" value=<?php echo $row['idGallery'];?>>
           <div class="product" style="float: left;">
             <button type="hidden" name='displaydetails' 
                style='background- 

                image:url(images/<?php echo $row['imgFullNameGallery'];?>
              );width:150px; height:200px; margin:auto; background-size:100% 100%; b 
             ackground-repeat:no-repeat;>
                 </button>

对此

              <div class="product" style="float: left;">
               <button type="hidden" name='displaydetails' value=<?php echo 
   $row['idGallery'];?> 
                style='background- 
    image:url(images/<?php echo $row['imgFullNameGallery'];?>
     );width:150px; height:200px; margin:auto; background-size:100% 100%; b 
    ackground-repeat:no-repeat;>
                </button>

和到userdetails.php

     if(isset($_POST['displaydetails'])){
  if(isset($_SESSION['userId'])){

 $idofimg=$_POST['hiddenId'];

对此

 if(isset($_POST['displaydetails'])){
     if(isset($_SESSION['userId'])){

  $idofimg=$_POST['displaydetails'];