如何使用php搜索我的数据库

时间:2013-06-09 22:06:08

标签: php html sql database search

(问)我有一个包含表单的网页,我希望能够使用用户在这些表单中插入的信息(提交后)以运行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);
?>

2 个答案:

答案 0 :(得分:0)

如果您要搜索客户并检索数据,请执行以下操作:     $搜索= $ _ POST [&#39;术语&#39;];

$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&#39;%$ search%&#39;)

从客户端提交搜索只需

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

在您的表单中。

您确实需要阅读有关sql注入的评论中提到的文章,但在您当前的级别,更好地了解正在发生的事情,然后继续学习安全性。

答案 1 :(得分:-1)

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