使用PHP在页面上显示MySQL数据库中的信息

时间:2013-07-18 20:11:13

标签: php mysql database web-deployment

我有一个名为test的数据库。在其中有一个名为people的表。人们有4个领域:fname,lname,年龄,城市。我有一个页面,其中包含人们可以输入数据的表单。

<?php
include('header.php');
?>

<body>
<form action="getinformation.php" method="post" id="getinformation">
<div id="header">
<h1><strong>Search For Data</h1></strong>
</div>
<div id="main">
<table border="0" width="75%">
        <tr>
            <td align="right" width="10%">First Name: </td>
            <td><input type="text" name="fname" id="fname" size="20" /></td>
        </tr>
        <tr>
        <td align="right" width="10%">Last Name: </td>
            <td><input type="text" name="lname" id="lname" size="20" /></td>
        </tr>
        <tr>
        <td align="right" width="10%">Age: </td>
            <td><input type="text" name="age" id="age" size="3" /></td>
        </tr>
        <tr>
        <td align="right" width="10%">City: </td>
            <td><input type="text" name="city" id="city" size="20" /></td>
        </tr>
</table>
<input type="submit" />
</div>
</body>

<?php
include('footer.php');
?>

单击提交按钮时,它会将数据发送到另一个名为getinformation.php的页面。

<?php
require_once('model.php');

$query = "SELECT * FROM people WHERE";

if (isset ($POST_fname) {
$fname = $_POST['fname'];
$query = $query . " fname = " . $fname . " AND" }
if (isset ($POST_lname) {
$lname = $_POST['lname']; 
$query = $query . " lname = " . $lname . " AND" }
if (isset ($POST_age) {
$age = $_POST['age']; 
$query = $query . " age = " . $age . " AND" }
if (isset ($POST_city) {
$city = $_POST['city']; 
$query = $query . " city = " . $city . " AND" }

$query = rtrim($query, " AND");
?>
<div id=/"header/">
<h1><strong>This is the information you requested</h1></strong>
</div>
<div id=/"main/">
<?php
$statement = $db->prepare($query);
$statement->execute();
$products = $statement->fethAll();
$statement->closeCursor();
foreach ($products as $product) {
    echo $product['fname'] . " " . $product['lname'] . " | " . $product['age'] . " | " . $product['city'] . '<br />';
?>
</div>
<?php
include('footer.php');
?>

我收到错误

Parse error: syntax error, unexpected '{' in C:\Program Files\wamp\www\testwebpage\Model\getinformation.php on line 6

我之前使用我的isset函数遇到过这个问题但除了那个工作之外我还在想其余的代码是否正常(假设isset工作正常)

4 个答案:

答案 0 :(得分:2)

您有语法错误 - 缺少右括号:

if (isset ($POST_fname) {

应该是

if (isset ( ..... ) ) {

答案 1 :(得分:1)

你忘记关闭了)在isset之后的任何地方,并没有在“AND”后面加分号。这是固定文件:

<?php
require_once('model.php');

$query = "SELECT * FROM people WHERE";

if (isset ($POST_fname)) {
    $fname = $_POST['fname'];
    $query = $query . " fname = '" . $fname . "' AND";
}
if (isset ($POST_lname)) {
    $lname = $_POST['lname'];
    $query = $query . " lname = '" . $lname . "' AND";
}

if (isset ($POST_age)) {
        $age = $_POST['age'];
        $query = $query . " age = '" . $age . "' AND";
}

if (isset ($POST_city)) {
    $city = $_POST['city'];
    $query = $query . " city = '" . $city . "' AND";
}

$query = rtrim($query, " AND");
?>
<div id=/"header/">
<h1><strong>This is the information you requested</h1></strong>
</div>
<div id=/"main/">
<?php
$statement = $db->prepare($query);
$statement->execute();
$products = $statement->fethAll();
$statement->closeCursor();
foreach ($products as $product) {
    echo $product['fname'] . " " . $product['lname'] . " | " . $product['age'] . " | " . $product['city'] . '<br />';
}
?>
</div>
<?php
include('footer.php');
?>

答案 2 :(得分:0)

忘记了(在if的末尾,按照错误输出中的说明查看第6行。

答案 3 :(得分:0)

(isset($ POST_fname) carfull isset()是一个有2个parenteses的表达式&#34;(&#34;&#34;)&#34;你打开了,但是没有关闭。

每一个&#39; if&#39;这样做

if(isset(anything))

任何其他问题,来这里!