【问题标题】:PHP search query results used as form variable用作表单变量的 PHP 搜索查询结果
【发布时间】:2017-10-29 00:06:22
【问题描述】:

我正在处理的这个项目我有一个将结果列出到表中的搜索表单,我在循环中添加了两个提交表单,如果单击应该将结果传输到另一个表。但是当我单击提交按钮时,它只会刷新页面。我已经更改了表单的名称和值,并尝试在 PHP 中输​​入和输出值中的引号以及其他一些具有相同结果的更改。有什么想法我可能做错了吗?

?>
            <table>
            <tr>
                 <th>Name</th>
                 <th>Brewery</th>
                 <th>City</th>
                 <th>State</th>
                 <th>Style</th>
                 <th>ABV</th>
                 <th>IBU</th>
             </tr>
             <tr>
                 <td><?php echo $result['Name']; ?></td>
                 <td><?php echo $result['Brewery']; ?></td>
                 <td><?php echo $result['City']; ?></td>
                 <td><?php echo $result['State']; ?></td>
                 <td><?php echo $result['Style']; ?></td>
                 <td><?php echo $result['ABV'];?> </td>
                 <td><?php echo $result['IBU']; ?></td>
                 <td>

                 <form method="post" name="ontap" action="form.php">
                     <input type="hidden" name="name3" value=<?php echo $result['Name'] ?>>
                     <input type="hidden" name="brewery3" value=<?php echo $result['Brewery']; ?>>
                     <input type="hidden" name="city3" value=<?php echo $result['City']; ?>>
                     <input type="hidden" name="state3" value=<?php echo $result['State']; ?>>
                     <input type="hidden" name="style3" value=<?php echo $result['Style']; ?>>
                     <input type="hidden" name="abv3" value=<?php echo $result['ABV']; ?>>
                     <input type="hidden" name="ibu3" value=<?php echo $result['IBU']; ?>>
                     <input type="submit" name="ontap" value="On Tap">
                     </form> </td>
                         <?php
                         if(isset($_POST['ontap'])){

                             $name3 = $_POST['name3'];
                             $brewery3 = $_POST['brewery3'];
                             $city3 = $_POST['city3'];
                             $state3 = $_POST['state3'];
                             $style3 = $_POST['style3'];
                             $abv3 = $_POST['abv3'];
                             $ibu3 =$_POST['ibu3'];

                             mysqli_connect($conn,"INSERT INTO `Test` (`Name`, `Brewery`, `City`, `State`, `Style`, `ABV`, `IBU`) VALUES ('".$name3."', '".$brewery3."', '".$city3."', '".$state3."', '".$style3."', '".$abv3."', '".$ibu3."')") or die(mysqli_error($conn));   

                             echo("Beer Added");                                 

                             }
                             ?>                                        



                 <td>
                     <form  method="post" action="form.php"  id="bottle">
                     <input type="submit" name="bottle" value="bottle"> </form></td>
             </tr>    
             </table>

             <?php

我在刷新页面时仍然遇到同样的问题。进行了一些建议的更改,但似乎没有任何效果。但到目前为止,mysqli_connect 已更改为 mysqli_query。我已将每个输入名称放入括号 ontap['name3'];等。将操作值留空。我注意到拉入表单的每个变量也只使用第一个单词。我会尝试上传正在使用的表格的图片 searchformwithtable

【问题讨论】:

  • 如果您的表单添加了新行,则需要在 SELECT one 之前执行 INSERT 查询。
  • 您的表单未提交。将表单中的操作设为空白,即 action=" " 并尝试。
  • 我将操作留空并更改为 mysqli_query 但它仍然只是刷新页面。有没有办法查看表单是否正在提交变量值?

标签: php html forms html-table


【解决方案1】:

mysqli_connect() 用于打开与 MySQL 服务器的新连接。 你可以像这样使用它:

$conn = mysqli_connect(server,username,password,database);

如果你想在你的表中插入数据,你需要使用 mysqli_query() 来达到这个目的。你可以像这样使用它:

$sql="INSERT INTO `Test` (`Name`, `Brewery`, `City`, `State`, `Style`, `ABV`, `IBU`) VALUES ('$name3', '$brewery3', '$city3', '$state3', '$style3', '$abv3', '$ibu3')";

$result = $mysqli_query($conn,$sql);
if($result) {
   echo 'Beer Added';
}

休息一切都好。进行此更改,它应该可以工作。

【讨论】:

  • 请务必使用 isset(),无论你在哪里从变量中获取值,如果变量不包含任何值,使用 isset() 失败将导致错误。
  • @Beau 您可以通过以下方式使用 isset() 检查表单是否正在提交值: if(isset($__POST['name3']) { echo 'Something' ; }
  • 您可以类似地检查所有变量
  • 抱歉,我没有 50 个声望,因此无法在上面发表评论;)
猜你喜欢
  • 2020-04-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-24
  • 2011-03-22
  • 2012-09-14
相关资源
最近更新 更多