【问题标题】:how to pick up a radio button in a mysqli select query using php [closed]如何使用 php 在 mysqli 选择查询中选择单选按钮 [关闭]
【发布时间】:2015-07-07 11:20:15
【问题描述】:

我有一个带有许多不同复选框的 html 表单。我想使用 php,所以当查询运行时,这意味着选择部分将是用户选中的任何单选按钮。

【问题讨论】:

  • 你的问题到底是什么?
  • 我希望从查询中选择用户选择的任何单选按钮
  • 您没有单选按钮,而是复选框,其次它们位于数组中。打开用于 sql 注入,您需要获取查询。为什么$query前面有斜线?
  • 哦是的对不起意思是复选框
  • 根据您的编辑echo "Please pick a sel"

标签: php html mysqli


【解决方案1】:

我建议不要通过修改查询来执行此操作,而是通过修改输出来实现。

<?php

error_reporting(E_ALL); ini_set('display_errors', 1);
include "connect.php";

// Just select all the columns.
$query = "SELECT HouseID, DatePurchased, AskingFor, SoldFor from purchase";

// Execute the query
$result = mysqli_query($con, $query);

// Then output only the columns you want.
echo "<table><thead><tr>";
foreach ($_POST["choice"] as $column) {
    echo "<th>$column</th>";
}
echo "</tr></thead><tbody>";
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) { // 
    echo "<tr>";
    foreach ($_POST['choice'] as $column) {
        echo "<td>{$row[$column]}</td>";
    }
    echo "</tr>";
}
echo "</tbody></table>";
?>

【讨论】:

  • 似乎不喜欢 fetch 数组
  • 例如,我使用了fetchArray(),因为从您的问题中不清楚您将使用哪个数据库扩展。如果您在问题中添加代码以显示您当前如何执行查询和获取结果,我可以相应地修改此答案。
  • 在您编辑的问题的代码中,您没有执行查询。并且如果使用这种方法生成表,则不需要修改SQL字符串。
  • 做什么?执行查询?就是这一行$result = mysqli_query($con, $query);
  • 我赞成这个答案,为了你的努力,即使这个问题无处可去。 OP 应该在发布之前通过教程。
【解决方案2】:

要从您的 html 表单中正确选择,您可以根据允许的选择字段检查输入的值。这是一个示例:

<?php

error_reporting(E_ALL); ini_set('display_errors', 1);

$choice = $_POST["choice"];

include "connect.php";

switch($choice){
   case("HouseID"): $selection = "HouseID";
   break;
   case("DatePurchased"): $selection = "DatePurchased";
   break;
   case("AskingFor"): $selection = "AskingFor";
   break;
   case("SoldFor"): $selection = "SoldFor";
   break;
   Default: $selection = "*";
}

$query = "SELECT $selection from purchase";

?>

这种方式不会将用户输入直接放入查询中,并且您可以拥有与表格字段不同的输入值,从而为您提供了额外的保护层。

【讨论】:

    【解决方案3】:

    也许是这样的:

    <?php
    
    error_reporting(E_ALL); ini_set('display_errors', 1);
    
    $choice = $_POST["choice"];
    
    if(!empty($choice)){
      $select = implode(',',$choice); // this will split all choices with a comma 
      $query = "SELECT $select from purchase";
    }else{
      // IF NOTHING IS SELECTED
    }
    
    include "connect.php";
    
    ?>
    

    【讨论】:

    • 如何在表格中打印?
    • 很遗憾这不起作用
    • include "connect.php"; 应该在查询之前去。
    猜你喜欢
    • 1970-01-01
    • 2021-04-08
    • 1970-01-01
    • 2011-05-10
    • 2023-03-07
    • 1970-01-01
    • 2015-08-17
    • 2015-03-14
    • 2015-01-15
    相关资源
    最近更新 更多