gpt4 book ai didi

php - 如何在 php 中执行搜索?

转载 作者:行者123 更新时间:2023-11-29 03:47:53 24 4
gpt4 key购买 nike

我想使用 php 从数据库中执行搜索,当我输入名称 arun 时,它应该搜索名称 arun 并显示带有名称 arun 的结果,当我输入名称和电子邮件时,它应该搜索名称和电子邮件并且应该显示单个结果,因为电子邮件是唯一字段我已经编写了以下代码但它不起作用有人可以帮助我吗?如果有人投反对票,请添加评论,为什么?

<?php

if (isset($_SESSION['message11'])) {
echo $_SESSION['message11'];
unset($_SESSION['message11']);
}
?>

<?php

include('connection.php');
$name = $_POST['name'];
$email = $_POST['email'];
$qualification = $_POST['qualify'];
$sql = "SELECT * FROM form WHERE Name ='$name' OR EmailAddress = '$email' OR Qualification = '$qualification' ";
$result = $conn->query($sql);

if (!empty($_POST)) {
if ($result->num_rows === 0) {
echo '<p style="margin-left:340px">no records</p>';
}
}



while ($row = $result->fetch_assoc()) {
$_SESSION["snum"] = $row['sno'];
$_SESSION["nam"] = $row['Name'];
$_SESSION["quali"] = $row['Qualification'];
$_SESSION["emai"] = $row['EmailAddress'];

echo '<br>';
echo '<form name="friend" action="received.php" method="post">';
echo '<input style="margin-left:340px;padding-bottom:10px" type="checkbox" value="' . $row['sno'] . '" name="friend[]">&nbsp;user Details</input>';
echo '<br>';
echo '<br>';
echo '<div class="container" style="border-style:solid; border-width:medium;width: 550px;">';
echo '<br>';
echo 'Name: ' . $row['Name'];
echo '<br /> EmailAddress: ' . $row['EmailAddress'];
echo '<br /> Qualification: ' . $row['Qualification'];
echo '<br /> DOB: ' . $row['DOB'];
echo '<br/>';
echo '<br/>';

echo '<br/>';
echo '<br/>';

echo '</div>';
echo '<br/>';
}

if (!empty($_POST)) {
echo '<button style="margin-left:340px" type=""submit">invite</button>';
}
echo '</form>';
$conn->close();
?>

最佳答案

您正在向三个字段添加 OR 条件,即使它们不是由用户输入的。

使用条件让 sql 只搜索输入的字段。

使用array()

$sql = "SELECT * FROM form WHERE ";
$conditions = array();
if (! empty($name)) {
$conditions[] = "Name ='$name'";
}
if (! empty($email)) {
$conditions[] = "EmailAddress ='$email'";
}
if (! empty($qualification)) {
$conditions[] = "Qualification ='$qualification'";
}
$sql .= ! empty($conditions) ? implode(' OR ', $conditions) : '1';
$result=$conn->query($sql);

关于php - 如何在 php 中执行搜索?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34543872/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com