(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);
?>