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

问题描述 投票:0回答:2

比方说,我有车信息(列)一个巨大的预制数据库其次(这DB已经有像3000行):

生产....最大速度的ID ....车名....车轮尺寸.... YEAR

上炫魅用户输入4种中的形式的信息:

    <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>

我创建sanitize.php以备将来使用,如果我想要将这些值到数据库,它看起来水木清华这样的:

<?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');
?>

在decide.php页面是复选框,并在此基础上检查框用户选择打勾,网站将回声出occurances是内部数据库的数量。

<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.

如果他选中的生产和车轮尺寸的网页(function.php)的年会计算从DB回声出的行数,其中生产和车轮尺寸的一年就像是用户输入的。

这同样适用于3个勾选复选框,如果他决定剔最大速度,生产年份,车名的网页(function.php)将计算和数据库回声出的行数,其中最大速度&&年的生产&&车名匹配用户的输入。

我想知道是否有创建此功能的方式,而不是仅仅使用手动如果每个case语句。这将是永无止境的这样的代码,如果我决定,我需要一些额外的列添加到我的数据库。

如果在PHP语句应该水木清华这样的:

$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.\n",$rowcount);
		    echo "<br>";
		    // Free result set
		    mysqli_free_result($result);
		}	
php database function checkbox compare
2个回答
0
投票

感谢那些帮助我更好地理解你正在尝试做的。看来,我认为在你的逻辑你的主要问题是,你不能确定你需要查询,因为你不通过该数据的山坳名。尝试是这样的:

<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>

一旦你有你的复选框阵列内的山坳名称,你可以使用,所以你不需要手工做的查询每一种可能性。

<?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.\n",$vars);
        echo "<br>";
        // Free result set
        mysqli_free_result($result);
    }   

}
?>

让我知道如何工作了


0
投票

你需要查询您打造取决于勾选复选框。像下面的东西

select
    a.count_cars,
    b.count_wheel
from
(
select
        count(*) count_cars
from
    dc_cars
) a
inner join
(
    select
        count(*) count_wheel
    from dc_wheel
)b

*这可能不是选择最好的方式,但只给你一个想法

因此,根据用户选中的东西,你会构造选择部分,如果选择2个或更多的内部联接查询。

© www.soinside.com 2019 - 2024. All rights reserved.