jordankoal
jordankoal

Reputation: 73

Display information from MySQL Database on page using PHP

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)

Upvotes: 0

Views: 1581

Answers (4)

user4035
user4035

Reputation: 23729

You forgot to close the ) everywhere after isset and didn't put semicolons after "AND". Here is the fixed file:

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

Upvotes: 1

Mattews Bueno
Mattews Bueno

Reputation: 13

(isset ($POST_fname) be carfull isset() is a expression that have 2 parenteses"(" ")" you opened, but din't close.

on every 'if' do this

if(isset(anything))

any other problem, come here !

Upvotes: 0

David Level
David Level

Reputation: 353

Forgot the ( at end of the if. look at the line 6 as said in the error output.

Upvotes: 0

Ryan
Ryan

Reputation: 28187

You have a syntax error - missing closing parenthesis:

if (isset ($POST_fname) {

Should be

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

Upvotes: 2

Related Questions