显示基于首字母的“名字”数据

时间:2018-07-18 21:15:10

标签: php mysql button lookup

我正在尝试构建一个允许用户选择其名字的第一个字母的登录表,然后它将从数据库中获取所有用户并将其输出到可选择列表中,然后允许用户选择他们的名字,然后输入他们“登录”到数据库的时间,并带有他们点击他们名字的时间戳。

现在,我一直停留在使PHP进入表中的最佳方法上,并查找所有以所选字母开头的“名字”,并输出First_Name和Last_Name。 / p>

示例:我选择“ P”,因为我的名字是“ Pietro Aretino”,然后它给我其First_Name以“ P”开头并包括其姓氏的每个成员的输出。我在“ Pietro Aretino”列表中找到了我的名字,选择了它,然后它还将我的登录信息也存储在数据库中,我的名字/姓氏和时间戳也将存储在该数据库中。

但是,我离题了,目前,我不确定如何让PHP按名字进行查找,然后输出列表

现在,我的每个按钮A-Z都标有军事字母(字母,Bravo等),当选择“ A”时,它将在下方向下运行“字母”功能。试图弄清楚将什么放入“ alpha”,“ bravo”,“ charlie”等函数中,这些函数将使PHP进入数据库并进行查找并输出列表。

...这是我到目前为止所拥有的:

<?php
$servername = "localhost";
$username = "*********";
$password = "*********";
$dbname = "********** ";

//Create Connection
global $CONNECTION;
$CONNECTION = new mysqli($servername, $username, $password, $dbname);
//Check Connection
if ($CONNECTION->connect_error) {
    die("Connection To MySQL Database Failed With the Following Error: " . $connection->connect_error);
}
global $SQL; 
$SQL = "SELECT first_name, last_name FROM ppSD_member_data";
global $RESULT; 
$RESULT = $CONNECTION->query($SQL);
if (isset($_POST['alpha'])) {
    alpha();
}
if (isset($_POST['bravo'])) {
    bravo();
}
if (isset($_POST['charlie'])) {
    charlie();
}
if (isset($_POST['delta'])) {
    delta();
}
if (isset($_POST['eko'])) {
    eko();
}
if (isset($_POST['foxtrot'])) {
    foxtrot();
}
if (isset($_POST['golf'])) {
    golf();
}
if (isset($_POST['hotel'])) {
    hotel();
}
if (isset($_POST['india'])) {
    india();
}
if (isset($_POST['juliet'])) {
    juliet();
}
if (isset($_POST['kilo'])) {
    kilo();
}
if (isset($_POST['lima'])) {
    lima();
}
if (isset($_POST['mike'])) {
    mike();
}
if (isset($_POST['november'])) {
    november();
}
if (isset($_POST['oscar'])) {
    oscar();
}
if (isset($_POST['papa'])) {
    papa();
}
if (isset($_POST['quebec'])) {
    quebec();
}
if (isset($_POST['romeo'])) {
    romeo();
}
if (isset($_POST['sierra'])) {
    sierra();
}
if (isset($_POST['tango'])) {
    tango();
}
if (isset($_POST['uniform'])) {
    uniform();
}
if (isset($_POST['victor'])) {
    victor();
}
if (isset($_POST['whiskey'])) {
    whiskey();
}
if (isset($_POST['xray'])) {
    xray();
}
if (isset($_POST['yankee'])) {
    yankee();
}
if (isset($_POST['zulu'])) {
    zulu();
}
echo <<<_END
<html>
    <head>
    <title>The Yard Members</title>
    </head>
<body>
<div align="center">
<h3>Please Select The Letter of Your First Name</h3>
<form method="post" action="display_members.php">
<input type="submit" name="alpha" id="name" value="A" style="height:75px; width:75px" />
<input type="submit" name="bravo" id="name" value="B" style="height:75px; width:75px" />
<input type="submit" name="charlie" id="name" value="C" style="height:75px; width:75px" />
<input type="submit" name="name" id="name" value="D" style="height:75px; width:75px" />
<br>
<input type="submit" value="E" style="height:75px; width:75px" />
<input type="submit" value="F" style="height:75px; width:75px" />
<input type="submit" value="G" style="height:75px; width:75px" />
<input type="submit" value="H" style="height:75px; width:75px" />
<br>
<input type="submit" value="I" style="height:75px; width:75px" />
<input type="submit" value="J" style="height:75px; width:75px" />
<input type="submit" value="K" style="height:75px; width:75px" />
<input type="submit" value="L" style="height:75px; width:75px" />
<br>
<input type="submit" value="M" style="height:75px; width:75px" />
<input type="submit" value="N" style="height:75px; width:75px" />
<input type="submit" value="O" style="height:75px; width:75px" />
<input type="submit" value="P" style="height:75px; width:75px" />
<br>
<input type="submit" value="Q" style="height:75px; width:75px" />
<input type="submit" value="R" style="height:75px; width:75px" />
<input type="submit" value="S" style="height:75px; width:75px" />
<input type="submit" value="T" style="height:75px; width:75px" />
<br>
<input type="submit" value="U" style="height:75px; width:75px" />
<input type="submit" value="V" style="height:75px; width:75px" />
<input type="submit" value="W" style="height:75px; width:75px" />
<input type="submit" value="X" style="height:75px; width:75px" />
<br>
<input type="submit" value="Y" style="height:75px; width:75px" />
<input type="submit" value="Z" style="height:75px; width:75px" />
</form>
</body>
</div>
</html>
_END;
function alpha() {
#while($row = $RESULT->fetch_assoc()) {
    echo "This Is The Alpha Function";
    }
function bravo() {
    echo "This is the Bravo Function";
}
function charlie() {
    echo "This is the charlie function";
}
function charlie() {
    echo "This is the charlie function";
}
function charlie() {
    echo "This is the charlie function";
}
function charlie() {
    echo "This is the charlie function";
}
function charlie() {
    echo "This is the charlie function";
}
function charlie() {
    echo "This is the charlie function";
}
function charlie() {
    echo "This is the charlie function";
}
function charlie() {
    echo "This is the charlie function";
}
function charlie() {
    echo "This is the charlie function";
}
function charlie() {
    echo "This is the charlie function";
}
function charlie() {
    echo "This is the charlie function";
}
function charlie() {
    echo "This is the charlie function";
}
function charlie() {
    echo "This is the charlie function";
}
function charlie() {
    echo "This is the charlie function";
}
function charlie() {
    echo "This is the charlie function";
}
function charlie() {
    echo "This is the charlie function";
}
function charlie() {
    echo "This is the charlie function";
}
function charlie() {
    echo "This is the charlie function";
}
function charlie() {
    echo "This is the charlie function";
}
function charlie() {
    echo "This is the charlie function";
}
function charlie() {
    echo "This is the charlie function";
}
#}
/*if(array_key_exists('name', $_POST)){
    search();
}
function get_post($var)
{
    return mysql_real_escape_string($_POST[$var]);
}
if ($result->num_rows > 0) {
//Output data of each row
    while($row = $result->fetch_assoc()) {
    echo "Name: " . $row["first_name"]. " " . $row["last_name"]. "<br>";
    }
} else {
    echo "No Members";
}
*/
$CONNECTION->close();
?>

1 个答案:

答案 0 :(得分:1)

正如人们所说的那样,代码有点荒谬。我只是想分享一下,希望能教给您一些东西。并不是真的想回答您的问题。实际上,我建议只是删除问题,因为我不确定它可以为他人提供多少实用程序。而且“如何进行通配符SQL搜索”显然是一个重复的问题。

无论如何...可能会浓缩为:

//example.com?search_letter=A

$pattern = isset($_REQUEST('search_letter')) ? $_REQUEST('search_letter') : null;

if (empty($pattern)) {
    die('missing search paramter');
}

$pattern = $pattern . '%';
$sql = 'SELECT * FROM users WHERE first_name LIKE ?';

$sth->execute([$pattern]);
$results = $sth->fetchAll();

有什么变化?什么是变量?一封信。您呈现A的输出的方式与解释和输出B的方式是否有所不同?不,所以请保持干燥(不要重复自己)。