dqf67993 2019-02-02 22:42
浏览 79
已采纳

将选中的复选框值与DB中的值进行比较

Let's say I have a huge premade database with car information(columns) as followed(this DB already has like 3000 rows):

ID .... CAR NAME .... WHEEL SIZE .... YEAR OF PRODUCTION .... MAX SPEED

On mainpage user inputs 4 kinds of information in a form:

    <form action="sanitize.php" method="post"> 
        <input type="text" name="car_name"  placeholder="CAR NAME"><br>
        <input type="text" name="wheel_size" placeholder="WHEEL SIZE"><br>
        <input type="text" name="year" placeholder="YEAR of PROD"><br>
        <input type="text" name="max_speed" placeholder="MAX SPEED"><br>
              <button>Submit </button>
  </form>

I have created sanitize.php for future use if I’d like to insert those values into DB and it looks smth like this:

<?php

$dbServerName = "localhost";
$dbUsername = "root";
$dbPassword = "";
$dbName = "cars";
$conn = mysqli_connect($dbServerName, $dbUsername, $dbPassword, $dbName);
// Sanitize POST Array
 $POST = filter_var_array($_POST, FILTER_SANITIZE_STRING);

 $car_name = mysqli_real_escape_string($conn, $POST['car_name']);
 $wheel_size = mysqli_real_escape_string($conn,$POST['wheel_size']);
 $year = mysqli_real_escape_string($conn,$POST['year']);
 $max_speed = mysqli_real_escape_string($conn,$POST['max_speed']);

session_start();
  $_SESSION['car_name'] = $car_name;
  $_SESSION['wheel_size'] = $wheel_size;
  $_SESSION['year'] = $year;
  $_SESSION['max_speed'] = $max_speed;

// Redirect to decide page
header('Location: decide.php');
?>

On decide.php page are checkboxes and based on which checkboxes user chooses to tick, the website will echo out the number of occurances it is inside database.

<form action="function.php" method="post">
  <input type="checkbox" name = "checkbox[]" value="<?php echo   $_SESSION['car_name']; ?>"> Car name<br>
  <input type="checkbox" name = "checkbox[]" value="<?php echo   $_SESSION['wheel_size']; ?>"> Wheel size<br>
  <input type="checkbox" name = "checkbox[]" value="<?php echo   $_SESSION['year']; ?>"> YEAR of PROD<br>
  <input type="checkbox" name = "checkbox[]" value="<?php echo   $_SESSION['max_speed']; ?>"> MAX SPEED<br>

  <button>Submit CHOICES</button>
</form>
For example if user ticked only car name and clicked button Submit CHOICES, the website will redirect the user to function.php and count and echo out the number of rows from DB that has the same name as he enetered.

If he ticked year of production AND wheel size the webpage(function.php) will count and echo out number of rows from DB where year of production AND wheel size are just like user entered.

Same goes for 3 ticked checkboxes, if he decides to tick max speed, year of production, and car name the webpage(function.php) will count and echo out number of rows from DB where max speed && year of production && car name match the user’s input.

I'd like to know if there's a way to create a function for this, not just manually using if statement for each case. It would be never-ending code like this if I decided that I need to add some extra columns into my database.

If statement in php would look smth like this:

$KAR = $_SESSION['car_name'];

            $sql="SELECT * FROM ecars WHERE car_name = '".$KAR"'";

        if ($result=mysqli_query($conn,$sql))
        {
            // Return the number of rows in result set
            $rowcount=mysqli_num_rows($result);
            printf("Number of cars which have same NAME as you entered: %d.
",$rowcount);
            echo "<br>";
            // Free result set
            mysqli_free_result($result);
        }   

</div>
  • 写回答

2条回答 默认 最新

  • dongying195959 2019-02-03 01:38
    关注

    Thanks that helped me understand better what you are trying to do. Looks to me that your main issue in your logic is that you can't identify the col name you need to query because you not passing that data. Try something like this:

    <form action="function.php" method="post">
      <input type="checkbox" name = "checkbox[car_name]" value="<?php echo   $_SESSION['car_name']; ?>"> Car name<br>
      <input type="checkbox" name = "checkbox[wheel_size]" value="<?php echo   $_SESSION['wheel_size']; ?>"> Wheel size<br>
      <input type="checkbox" name = "checkbox[year]" value="<?php echo   $_SESSION['year']; ?>"> YEAR of PROD<br>
      <input type="checkbox" name = "checkbox[max_speed]" value="<?php echo   $_SESSION['max_speed']; ?>"> MAX SPEED<br>
    
      <button>Submit CHOICES</button>
    </form>
    

    Once you have the col name inside your checkbox array, you can use that so you don't need to manually do the query for every possibility.

    <?php
    // The $_POST['checkbox'] array needs to survive the sanitizing part
    foreach($_SESSION['checkbox'] as $col=>$value){
    
        $sql="SELECT * FROM ecars WHERE $col = '$value'";
        $col_title = str_replace('_', ' ', $col);
    
        if ($result=mysqli_query($conn,$sql)){
            // Return the number of rows in result set
            $rowcount=mysqli_num_rows($result);
            $vars = array($col_title, $rowcount); 
            printf("Number of %d which have same VALUE as you entered: %d.
    ",$vars);
            echo "<br>";
            // Free result set
            mysqli_free_result($result);
        }   
    
    }
    ?>
    

    Let me know how that works out

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Fluent udf 编写问题
  • ¥15 求合并两个字节流VB6代码
  • ¥15 Pyqt 如何正确的关掉Qthread,并且释放其中的锁?
  • ¥30 网站服务器通过node.js部署了一个项目!前端访问失败
  • ¥15 WPS访问权限不足怎么解决
  • ¥15 java幂等控制问题
  • ¥15 海湾GST-DJ-N500
  • ¥15 氧化掩蔽层与注入条件关系
  • ¥15 Django DRF 如何反序列化得到Python对象类型数据
  • ¥15 多数据源与Hystrix的冲突