Parse error: parse error, unexpected $ LAST LINE?

Hi guys… I’ve been getting this error message:
Parse error: parse error, unexpected $ … on line # and the line number is the last line of my coding!

This is probably the toughest, meatiest page for my school final project. I finish this and I will receive my UW Certificate in Web Technology!
There’s probably one semicolon or some tiny yet crucial detail I’ve missed somewhere. I think I have the concept pretty much in hand… but GARGH!!

Thanks for checking out my issue!

[code]<?php # Pet Satisfaction Survey ITA343 Final Project addpet.php
// This is the add pet data page for the site.
// Set the page title and include the HTML header.
$page_title = ‘Add Pet Data’;
include_once (’./includes/header.html’);

// since these following values are just values collected from radio buttons or a pulldown
// the liklihood of them getting compromised is quite low. Therefore, no need to encode or escape_data-ize.

$animal = $_POST[‘animal’];
$health_sat = $_POST[‘health_sat’];
$clean_sat = $_POST[‘clean_sat’];
$afford_sat = $_POST[‘afford_sat’];
$enjoy_sat = $_POST[‘enjoy_sat’];
$care_sat = $_POST[‘care_sat’];
$repeat_sat = $_POST[‘repeat_sat’];

if (isset($_POST[‘submitted’])) { // Handle the form.

require_once ('./includes/db_connect.php'); // Connect to the database.

// Check for a pet name.
if (eregi ('^[[:alpha:].' -]{2,15}$', stripslashes(trim($_POST['pet_name'])))) {
	$petname = escape_data($_POST['pet_name']);
} else {
	$petname = FALSE;
	echo '<p><font color="red" size="+1">Please enter your pets name!</font></p>';

// Check for pet's years owned
if (is_numeric($_POST['years_owned'])) {
	$yo = escape_data($_POST['years_owned']);
} else {
	$yo = FALSE;
	echo '<p>Please enter years owned for this pet</p>';


// Check for pet's age
if (is_numeric($_POST['age'])) {
	$age = escape_data($_POST['age']);
} else {
	$age = FALSE;
	echo '<p>Please enter pets age less than 99 years!</p>';


if ($petname && $yo && $age) { // If everything's OK.
		// Add the pet data.
		$query = "INSERT INTO PETS (pet_name, animal, years_owned, age, health_sat, clean_sat, afford_sat, enjoy_sat, care_sat, repeat_sat) VALUES ('$petname', '$animal', '$yo', '$age', '$health_sat', '$clean_sat', '$afford_sat', '$enjoy_sat', '$care_sat', '$repeat_sat')";		
		$result = mysql_query ($query) or trigger_error("Query: $queryn<br />MySQL Error: " . mysql_error());

		if (mysql_affected_rows() == 1) { // If it ran OK.
		} else { // If it did not run OK.
			 // If one of the data tests failed.
	echo '<p><font color="red" size="+1">Please try again.</font></p>';		

mysql_close(); // Close the database connection.

} // End of the main Submit conditional.

Pet Information

Pet's Name:

Animal: <php // This function makes a pull-down menus for some animals. // Adding new animal types is easily done here, by just adding to the array below. // In case a user wants to register their sloth or platypus...

function animal_pulldown() {

// Make the animals array.
$animals = array (1 => 'dog', 'cat', 'ferret', 'bird', 'horse', 'pony', 'small mammal', 'pig', 'fish', 'ostrich', 'alpaca', 'bear', 'lion', 'turtle', 'frog', 'snake', 'lizard', 'insect', 'monkey');

// Make the animal pull-down menu.
echo '<select name="animal">';
foreach ($animals as $key => $value) {
	echo "<option value="$key">$value</option>n";
echo '</select>';


Years Owned:

Age (or highest age reached) (you may indicate with decimals, ie 5.5, if you wish):

// This is where the users opinions of their satisfaction of each pet is collected
We will now ask you to RATE your satisfaction of this pet you owned. On a Scale from 1 to 5 (with 1 indicating the lowest satisfaction, and 5 indicating the highest satisfaction) please tell us your satisfaction for each topic:

Your Satisfaction with this particular pet's Overall Health: 1 2 3 4 5

Your Satisfaction with this particular pet's Overall Cleanliness: 1

2 3 4 5

Your Satisfaction with this particular pet's Overall Affordability: 1

2 3 4 5

Your rating of Enjoyment you had with this particular pet: 1

2 3 4 5

Your Satisfaction with this particular pet's Overall Ease of Care: 1

2 3 4 5

All things considered, would you own this same type of pet again?: YES

<div align="center"><input type="submit" name="submit" value="Add" /></div>
<input type="hidden" name="submitted" value="TRUE" />
<?php include ('./includes/footer.html'); ?>[/code]

Make sure your indentation is correct. When I used a text editor and corrected the indentation I discovered that you’re missing a closing bracket } somewhere (not sure which if statement you forgot to close). But that’s the reason it’s saying it ran into the last line of the script before it was expecting it to.

thank you Z, I was afraid it was something like that… but then again, I guess I’d rather fix a little detail like that.
I use TextEdit, a free download I got somewhere that I like. I also have DreamWeaver4 but using that to code php feels like overkill.

I’ll give that fine-tooth-comb check a good going over. Thanks for your help!

No problem :slight_smile: I would recommend against using one of those.

Hi Zyppora, yep there was indeed a missing close bracket. That really helped me out. Now, I can move on and address more important code issues that need to be changed, but that was an important hurdle to get over.
My project is one that is a joint MySQL/PHP application. How many aren’t, though, right? Anyway, I’m using a neat pulldown php code - a list of select pulldown options.

[code]function animal_pulldown() {

// Make the animals array.
$animal = array (1 => 'dog', 'cat', 'ferret', 'bird', 'horse', 'pony', 'small mammal', 'pig', 'fish', 'ostrich', 'alpaca', 'bear', 'lion', 'turtle', 'frog', 'snake', 'lizard', 'insect', 'monkey');

// Make the animal pull-down menu.
echo '<select name="animal">';
foreach ($animal as $key => $value) {
	echo "<option value="$key">$value</option>n";
echo '</select>';


Then, within the page I just call the function:



<?php animal_pulldown(); ?>


However, I want to know how to code this so that what’s actually INSERTED to the database is the animal name, not its numeric key. So, as it is now, if the user selects ‘bird’, what’s written to the DB as $animal is “4”. I want “bird” as $animal, not 4. Is that a simple fix with what code I have here?

Obviously $animal = array (1 => ‘dog’, … needs to change. I just don’t know how to alter
foreach ($animal as $key => $value) {
echo “$valuen”;

Coding relatively happily with TextEdit

I should check TextEdit out. I’m currently using TextPad, which works for me because it allows me to use certain instructions like compiling and running Java applications written in it. Unfortunately TextPad is not free, but I’m using the trial version (which doesn’t expire btw).

As for your issue, you should know that html forms pass their values by their ‘value’ attribute:

foreach ($animal as $key => $value) {
  echo "<option value="$key">$value</option>n";

As you can see, the option’s value is $key, which is the number you’re receiving on the PHP end. There’s two ways to solve this:

  1. You can change the value attribute to $value. This will also allow you to remove the ‘$key =>’ part from your foreach instruction:
foreach ($animal as $value) {
  echo "<option value="$value">$value</option>n";
  1. You should make the list of animals available on the other PHP end and use the following to convert the number into the value:
$myanimal = $animal_array[intval($_POST[$animal])];

I used intval() to make sure I’m not getting any strings or other unexpected values in $animal. However, you should also check your input to make sure it doesn’t contain other unexpected values (like 0), lest you may receive PHP warnings again :) (or maybe it’s a good thing to run into those to see what they’re about).

Thank you Zyppora, I got it to work just as hoped with your suggestion and perspective. I’m just about to finish up my University of Washington Web Technology certificate, just maybe by the end of the month. While I probably won’t become the next best PHP programmer/developer, my skills will certainly open other cool doors for me. Thanks for your help and for the boards here!
Don in Seattle

Hope you’ll make your degree.

Seattle’s a great city btw, visited last August/September and had a blast :) Weather seemed perfectly tuned too, sunny most days.

Sponsor our Newsletter | Privacy Policy | Terms of Service