2017-10-19 144 views
0

我正在显示来自数据库的图像,每个图像都有一个包含表单和提交按钮的评论框;提交按钮具有显示的图像的名称。防爆。如果图像名称为flowers.jpg,则提交按钮的名称将设置为flowers.jpg。 (尽管我做了字符串替换,所以在我的代码中它将被设置为flowersjpg)名称被添加到提交按钮中,没有任何问题。因为我有几张图片,我想将$ row ['image'](图片名称)传递给isset()函数的$ _POST []参数,但它不起作用。 - 所有的代码是一个文档

$result = mysqli_query($conn, "SELECT * FROM uploads ORDER BY timestamp DESC"); 

      while($row = mysqli_fetch_array($result)) 
      { 
       // Prints all the photos with a caption 
       echo "<div class='wrapper' name=".$row['image'].">"; 
        echo "<div class='caption'>"; 
         echo "<h3>".$row['title']."</h3>"; 
         echo "<p class='description'>".$row['description']."</p>"; 
        echo "</div>"; 
         echo "<img src='images/uploads/".$row['image']."'/>"; 

         // name for submit button, the $row['image'] contains the entire image name so certain characters have to be removed 
         **$name = str_replace(array(".", "-", ":", "'", "/", " "), "", $row['image']);** 

         // comment box - just a form with a submit 
         echo "<div class='commentBox'> 
          <form action='test.php' method='POST'> 
           <textarea placeholder='Say something nice!' name='comment'></textarea> 
            // CREATES NAME FOR BUTTON. FOR EACH IMAGE, THE BUTTON HAS THE IMG NAME 
            **<button type='submit' name='".$name."' value='submit'> post </button>** 
          </form> 
          </div>"; 
       echo "</div>"; 
      } 

      if(isset($_POST["'".$name."'"])) 
      { 
       echo '<script type="text/javascript"> alert("isset"); </script>'; 
      } 

在当我把这个名字作为$ _ POST参数纯字符串,它工作正常,但是当我把变量,这是行不通的。 $ name变量是一个字符串。

这个工程,但我敢肯定,任何使用的字符串将工作,只要按钮名称和$ _POST字符串是相同的。

 echo "<button type='submit' name='flowerspng' value='submit'> post </button>"; 

     if(isset($_POST[flowerspng])) 
     { 

      echo '<script type="text/javascript"> alert("isset"); </script>'; 
     } 

这段代码另一方面没有。我知道变量可以在$ _POST参数中传递,所以我不明白我的代码有什么问题。我的语法不正确?显然变量没有被设置,否则警报框会弹出。

 echo "<button type='submit' name='".$name."' value='submit'> post </button>"; 

     if(isset($_POST["'".$name."'"])) 
     { 

      echo '<script type="text/javascript"> alert("isset"); </script>'; 
     } 

我也试过这个,但我不认为它的工作,因为没有引号$名称附近。

 echo "<button type='submit' name=$name value='submit'> post </button>" 

     if(isset($_POST[$name])) 
     { 

      echo '<script type="text/javascript"> alert("isset"); </script>'; 
     } 
+0

作为提示,您的isset检查中的'$ name'将始终是最后一行的名称,因为每次迭代都会覆盖它。 –

+0

如果发生了这种情况,代码是否适用于数据库中的最后一幅图像?我点击了所有按钮,并且警报框根本不会弹出。我对PHP有点新,所以对于问一个愚蠢的问题表示抱歉。 – WhiteVeils

+0

它会,但你有'$ name'周围引用不应该在那里(即你最后一次尝试是正确的,但你可能没有提交最后一项)。基本上,$ _POST键是'charlie'(作为一个例子),但是引用它可以让它查找键“charlie'',这是一个不同的字符串。 –

回答

0

此代码:

echo "<button type='submit' name='$name' value='submit'> post </button>" 

    if(isset($_POST[$name])) 
    { 

     echo '<script type="text/javascript"> alert("isset"); </script>'; 
    } 

我感动的if语句的isset()函数功能到while循环的结束,现在适用于所有图像。

  while($row = mysqli_fetch_array($result)) 
      { 
       echo "<div class='wrapper' name=".$row['image'].">"; 
        echo "<div class='caption'>"; 
         echo "<h3>".$row['title']."</h3>"; 
         echo "<p class='description'>".$row['description']."</p>"; 
        echo "</div>"; 
         echo "<img src='images/uploads/".$row['image']."'/>"; 
         $name = str_replace(array(".", "-", ":", "'", "/", " "), "", $row['image']); 
         echo "<div class='commentBox'> 
          <form action='test.php' style='text-align: right; background-color: rgb(34,34,34); width: 100%; height: 120%; margin: 0;' method='POST'> 
           <textarea style='height: 90%; width: 79%; resize: none; border-radius: 3px; margin: 0; margin-top: 5px;' placeholder='Say something nice!' name='comment'></textarea> 
            <button type='submit' name='$name' value='submit' style='height: 100%; width: 20%; float: right;'> post </button> 
          </form> 
            <div class='seeMore'> see more </div> 
          </div>"; 
         echo "</br><div class='comments' style='display: none;'> comment display </div>"; 
       echo "</div>"; 

        ** 
        // testing the if statement for each image - works perfectly now 
        if(isset($_POST[$name])) 
        { 
         echo '<script type="text/javascript"> alert("'.$name.'"); </script>'; 
         mysqli_query($conn, "UPDATE uploads SET title='test' WHERE image='".$row['image']."'"); 
         header("Refresh:0"); 
        } 
        ** 
      }