0

I have a database called test. In it there is a table called people. People has 4 fields: fname, lname, age, city. I have a page with a form where people can enter in data.

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

When the submit button is clicked it will send the data to another page named 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');
?>

I get an error

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

I have had this problem before with my isset function but aside from that working I'm wondering if the rest of the code looks fine (assuming isset worked perfectly)

4

4 回答 4

2

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

if (isset ($POST_fname) {

应该

if (isset ( ..... ) ) {
于 2013-07-18T20:13:26.897 回答
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');
?>
于 2013-07-18T20:15:34.917 回答
0

忘记了 if 末尾的 (。请查看错误输出中所述的第 6 行。

于 2013-07-18T20:13:37.727 回答
0

(isset ($POST_fname) be carfull isset() 是一个表达式,它有 2 个你打开但没有关闭的 parenteses"(" ")"。

在每一个“如果”上这样做

if(isset(anything))

还有什么问题,来这里!

于 2013-07-18T20:16:52.850 回答