<form action="index.php" method="POST" enctype="multipart/form-data">
<p>имя</p><input type="text" name="name_form"><br>
<p>возраст</p><input type="text" name="age_form"><br>
<input type="submit" name="submit">
</form>
$name_form = $_POST['name_form'];
$age_form = $_POST['age_form'];
$name = "SELECT * FROM users WHERE name = $name_form AND age = $age_form";
$result2 = mysqli_query($conn, $name);
$a = array();
if(mysqli_num_rows($result2) > 0 ) {
while($row = mysqli_fetch_assoc($result2)) {
$a[] = $row;
}
} else {
echo "0 results";
}
print_r($a);
$conditions = [];
$parameters = [];
// conditional statements
if (!empty($_GET['name']))
{
// here we are using LIKE with wildcard search
// use it ONLY if really need it
$conditions[] = 'name LIKE ?';
$parameters[] = '%'.$_GET['name']."%";
}
if (!empty($_GET['age']))
{
// here we are using equality
$conditions[] = 'age = ?';
$parameters[] = $_GET['age'];
}
$sql = "SELECT * FROM users";
// a smart code to add all conditions, if any
if ($conditions)
{
$sql .= " WHERE ".implode(" AND ", $conditions);
}
// the usual prepare/bind/execute/fetch routine
$stmt = $mysqli->prepare($sql);
$stmt->bind_param(str_repeat("s", count($parameters)), ...$parameters);
$stmt->execute();
$b = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
if($b) {
print_r($b);
} else {
echo "0 results";
}
$name = "SELECT * FROM users";
$where = '';
if (!empty($name_form)) {
$where .= 'name = ' . $name_form;
}
if (!empty($age_form)) {
$where .= 'AND age = ' . $age_form;
}
if (!empty($where)) {
$name .= 'WHERE ' . $where;
}
$result2 = mysqli_query($conn, $name);