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