【问题标题】:How to search my database using php如何使用 php 搜索我的数据库
【发布时间】:2013-06-05 13:30:31
【问题描述】:

(Q) 我有一个包含表单的网页,我希望能够使用用户在这些表单中插入的信息(提交后),以便运行 SQL 查询并使用 php 搜索数据库和显示结果。

我成功创建了一个页面,从用户那里获取信息并将这些信息插入到数据库中的表中,我认为我在上面(Q)中解释的内容应该类似于插入,但我卡住了。

这是我为插入而编写的代码,它可以工作,你能告诉我如何搜索数据库吗?

<html>
<body>
<?php
    // Connecting to the database
$con = @mysql_connect('localhost','XXXXXXX','YYYYYYYY');
if (!$con) die("Could not connect to the server!");
if (!@mysql_select_db('XXXXXXXX')) die('Couldn\'t locate the database!');
// If form has been submitted - take action
if (isset($_POST["submit"]))
{
    // Inserting data to Branch
    $sql = "INSERT INTO Branch(branchName,address,size)
            VALUES('".$_POST["BRANCHNAME"]."','".$_POST["ADDRESS"]."',".$_POST["SIZE"].");";
    $result = mysql_query($sql);
    // In case of failure
    if (!$result) {
        die("Couldn't add the part to the catalog.<br>".mysql_error());
    }


    // Inserting data to Customer
    $sql = "INSERT INTO Customer(memberNum,telephoneNum,firstName,lastName,birthDate,branchName)
            VALUES(".$_POST["MEMBERNUM"].",'".$_POST["TELEPHONENUM"]."','".$_POST["FIRSTNAME"]."','".$_POST["LASTNAME"]."','".$_POST["BIRTHDATE"]."','".$_POST["BRANCHNAME"]."');";
    $result = mysql_query($sql);
    // In case of failure
    if (!$result) {
        die("Couldn't add the customer specified.<br>".mysql_error());
    }

    // In case of success
    echo "<b>The details have been added to the database.</b><br><br>";


}?>

<form action="<?php $_SERVER['PHP_SELF'] ?>" method="POST">
    <table border="0" cellpadding="9">
    <tr>
        <td>Member number</td>
        <td><input name="MEMBERNUM" type="text" size="10"></td>
    </tr>
    <tr>
        <td>Telephone number</td>
        <td><input name="TELEPHONENUM" type="text" size="20"></td>
    </tr>
    <tr>
        <td>First name</td>
        <td><input name="FIRSTNAME" type="text" size="10"></td>
    </tr>
    <tr>
        <td>Last name</td>
        <td><input name="LASTNAME" type="text" size="10"></td>
    </tr>
    <tr>
        <td>Birth date</td>
        <td><input name="BIRTHDATE" type="text" size="10"></td>
    </tr>
    <tr>
        <td>Branch name</td>
        <td><input name="BRANCHNAME" type="text" size="10"></td>
    </tr>
    <tr>
        <td>Address</td>
        <td><input name="ADDRESS" type="text" size="10"></td>
    </tr>
    <tr>
        <td>Size</td>
        <td><input name="SIZE" type="text" size="10"></td>
    </tr>
    <tr>
        <td colspan="2"><br><input name="submit" type="submit" value="Send"></td>
    </tr>

    </table>
</form>

<?php
    // Closing the connection
    mysql_close($con);
?>

【问题讨论】:

  • 对于初学者来说,放弃错误抑制@。此外,您应该真正了解SQL injection
  • mysql_* 函数已弃用。你至少需要切换到mysqli

标签: php html sql database search


【解决方案1】:

如果您想搜索客户并检索数据,只需执行以下操作: $search=$_POST['term'];

$sql="Select * from Branch,Customer where Customer.branchName=Branch.branchName and (firstName LIKE '%$search%' or lastName LIKE '%$search%') ";
$result = mysql_query($sql);
if ($result&&mysql_num_rows($result)>0){
    while ($row=mysql_fetch_assoc($result){
        $customername=$row['firstName'];
        $customerlastname=$row['lastName'];
    }
}

您可以在括号中添加任意数量的字段(或 memberNum LIKE '%$search%')

要从客户端提交搜索,只需一个

    <input type="text" name="term">

在你的表格中。

您确实需要阅读 cmets 中提到的有关 sql 注入的文章,但在您目前的水平上,最好先了解发生了什么,然后继续学习安全性。

【讨论】:

    【解决方案2】:

    您需要使用 WHERE 子句作为简单的解决方案来执行查询。 This article can get you started.你可以用它来抓取某些字段; LIKE will also help you if you want partial matches.

    【讨论】:

    • 请不要链接到 w3schools。尤其不谈 SQL 的话题,因为那只会引入安全问题(即 SQL 注入)
    • 如果您觉得我的链接没有学术价值,您可以随意使用 SO 的编辑功能来改进我的链接。我觉得一般概念仍然非常适用。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2014-08-23
    • 1970-01-01
    • 2015-09-10
    • 1970-01-01
    • 2015-07-22
    • 2014-02-20
    • 2014-05-26
    • 1970-01-01
    相关资源
    最近更新 更多