Forum Moderators: coopster

Message Too Old, No Replies

Querying multiple MySQL using elseif statements

         

cricket

2:06 am on Dec 6, 2004 (gmt 0)

10+ Year Member



Hello!

I am creating a website for class that requires querying a MySQL database using a select statement that joins more than 1 table.

I'm getting the following error message:

Parse error: parse error, unexpected ';', expecting '{' in /usr/home/cduong/public_html/output_fns.php on line 579

Line 579 is actually above my query script. I'm not sure why because I was able to pull up the page without any problems until I tried writing [the query script].

The code is:
function display_results_form()
{
?>
<tr colspan="3">
<td align="left" colspan="4"><h1>Guest Search Results</h1></td>
</tr>

<?php
//create medium variable names see pages 20 and 226
$searchtype=$HTTP_POST_VARS['searchtype'];
$searchterm=$HTTP_POST_VARS['searchterm'];

$searchterm= trim($searchterm);

if (!$searchterm ¦¦!$searchterm')
{
echo 'You have not entered a search term. <br />';
echo 'Please go back and try again.';
exit;
}

$searchtype = addslashes ($searchtype);
$searchterm = addslashes ($searchterm);

mysql_select_db ('dbname');

// page 42

if ($searchtype == 'first') {
$sql = "SELECT first, last, city, state FROM guests WHERE first=$searchterm";
echo $sql;
$result = mysql_query($sql);
} elseif($searchtype == 'last') {
$sql = "SELECT first, last, city, state FROM guests WHERE last=$searchterm";
echo $sql;
$result = mysql_query($sql);
} elseif($searchtype == 'city') {
$sql = "SELECT first, last, city, state FROM guests WHERE city=$searchterm";
echo $sql;
$result = mysql_query($sql);
} elseif($searchtype == 'state') {
$sql = "SELECT first, last, city, state FROM guests WHERE state=$searchterm";
echo $sql;
$result = mysql_query($sql);
} elseif($searchtype == 'ceremony') {
$sql = "SELECT guests.first, guests.last, guests.city, guests.state
FROM guests, event, rsvp
WHERE event.event_id = rsvp.event_id and guests.guest_id = rsvp.guest_id and event.ceremony=$searchterm";
echo $sql;
$result = mysql_query($sql);
}

$num_results = mysql_num_rows($result);

echo '<p>Number of guests found: '.$num_results.'</p>

for($i=0; $i <$num_results; $i++)
{
$row = mysql_fetch_array($result);
echo '<p><strong>'.(i+1).'. Name: ';
echo htmlspecialchars(stripslashes($row['first']));
echo htmlspecialchars(stripslashes($row['last']));
echo '<br /> \City: ';
echo htmlspecialchars(stripslashes($row['city']));
echo '<br />State: ';
echo htmlspecialchars(stripslashes($row['state']));
echo '</p>';
}

<?php
}

I'd appreciate any insights. Thank you!

ergophobe

4:41 am on Dec 6, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I'm not sure where line 579 is, but

if (!$searchterm ¦¦!$searchterm')

has an unmatched quote mark. That would give you a parse error.

tomda

5:57 am on Dec 6, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You definitely need to close this line by addind '; at the end

echo '<p>Number of guests found: '.$num_results.'</p>

Another, it is preferable that elseif statement finish with a else


IF ...... ELSEIF ...... ELSEIF ..... ELSE

Lastly, note that you do not need to write the all query again and again. You may write it this way


$sql = "SELECT first, last, city, state FROM guests";

if ($searchtype == 'first') {
$sql .= " WHERE first=$searchterm";
} elseif($searchtype == 'last') {
$sql .= " WHERE last=$searchterm";
} elseif($searchtype == 'city') {
$sql .= " WHERE city=$searchterm";
} elseif($searchtype == 'state') {
$sql .= " WHERE state=$searchterm";
} elseif($searchtype == 'ceremony') {
$sql .= ", event, rsvp
WHERE event.event_id = rsvp.event_id and guests.guest_id = rsvp.guest_id and event.ceremony=$searchterm";
} else {echo "Forgot the else statement";}

echo $sql;
$result = mysql_query($sql);

dreamcatcher

3:49 pm on Dec 6, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Another way of writing your code would be to use a switch statement. :)

cricket

7:35 pm on Dec 6, 2004 (gmt 0)

10+ Year Member



Thank you all for the suggestions! They worked, but now I'm getting a new error message:

"Parse error: parse error, unexpected $ in [file] on line 71"

But line 71 calls a function:

54 $num_results = mysql_num_rows($result);
55
56 echo '<p>Number of guests found: '.$num_results.'</p>';
57
58 for ($i=0; $i <$num_results; $i++)
59 {
60$row = mysql_fetch_array($result);
61echo '<p><strong>'.(i+1).'. Name: ';
62echo htmlspecialchars(stripslashes($row['first']));
63echo stripslashes($row['last']);
64echo '</strong><br /> \City: ';
65echo stripslashes($row['city']);
66echo '<br />State: ';
67echo stripslashes($row['state']);
68echo '</p>';
69 }
70
71 do_html_footer();
72?>

alcheme

5:23 am on Jan 7, 2005 (gmt 0)

10+ Year Member



HI Cricket!

What does the form Code look like for your search form?

<form... >

Fields / Code

</form>

If you solved your problem could you paste your solution withe the <form> </form> here for us?

THANKS!

Shane

cricket

7:24 pm on Jan 9, 2005 (gmt 0)

10+ Year Member



Hi Shane,

It's been about a month since I worked on it so I don't remember exactly what I did, but it's now working. I believe it either had to do with a missing semicolon or not using single or double quotes correctly.

Anyhow, here's the form as requested:

<form action="results_guests.php" method="post">

<tr>
<th>Search Guests</th>
</tr>

<tr>
<td>Choose Catagory:</td>
<td>
<select name="searchtype" id="searchtype">
<option value="first">First Name</option>
<option value="last">Last Name</option>
<option value="city">City</option>
<option value="state">State</option>
<option value="ceremony">Ceremony</option></select>
</td>
</tr>

<tr>
<td>Enter Search Term:</td>
<td><input name="searchterm" id="searchterm" type="text"></td>
</tr>

<tr>
<td><input type="submit" value="Search"></td>
</tr>

</form>