Jump to content

hard to find error


rghollenbeck

Recommended Posts

I get the following error:

Parse error: syntax error, unexpected ';' in /hermes/web06/b1946/moo.rghollenbeck/index.php on line 35

 

But here is the only php code on the whole page:

 

 

. . .more html code above

<?php 

// PHP version: 5.2.12
// MySQL Version: 5.0.77

$link = mysql_connect('path', 'username', 'pword'); 
if (!$link) { 
die('Could not connect: ' . mysql_error()); 
} 
mysql_select_db(guestlist); 

$Users_IP_address = $_SERVER["REMOTE_ADDR"] ;
echo "<input type='hidden' name='ip' value=";
echo $Users_IP_address;
echo ">";

$first_name=mysql_query("SELECT 'fname' FROM tblMain; WHERE 'ip'=" . $Users_IP_address);

// pseudocode section:
// If $first_name !null{
// echo "Hello " . $first_name;
// }
// end pseudocode section:
// something like that
?>

more html code below. . .

 

Thank you.

Link to comment
Share on other sites

I don't know how that semicolon got in there.  I see it in the notepad  copy I used to paste to here, but it is not in my code.

 

I don't have this in my code:

$first_name=mysql_query("SELECT 'fname' FROM tblMain; WHERE 'ip'=" . $Users_IP_address);

 

My line looks like this:

$first_name = mysql_query("SELECT 'fname' FROM 'tblMain' WHERE 'ip' =" . $Users_IP_address . ");"

I don't know where or when I picked up that extra semicolon.

 

As to posting all the code, it is totally not necessary.  I have another page that works perfectly.  It is a form that gathers first name, last name, email, phone number, URL, great grandmother's maiden name (not really), sexual preferences (not really), etc., and grabs the IP address of the user--all without any troubles.  It stores that data in a MySQL database called guestlist.  When a new guest arrives at my home page, I want the program to search my database against the IP address of the new guest to find a match.  If it finds that match, and Bob's name matches that IP address, it will say, "Hi Bob!"  If Susan's name is a match, it will say, "Hi Susan!"  Right now, it just says, "Error."

 

Link to comment
Share on other sites

Oh, yeah, I forgot to mention.  The error was NOT at line 35.  Line 35 simply said,

?>

 

It was the end of all the PHP code.  If I inserted a line feed immediately before the "?>" I would simply get the line number to change on the error report.  So I think the real error is somewhere between "<?php" and "?>" 

 

Brilliant, eh? 

 

;)

 

The problem is somewhere between "<?php" and "?>".  That's the whole code.

 

The code is pretty short.  I thought I ought to be able to spot it myself.  But I didn't spot that extra semicolon.  But that semicolon wasn't the problem because it is not in my program; it is (somehow) in my NotePad scratch copy which I used to assemble my post.

 

Link to comment
Share on other sites

Given that the code you posted in the first post is not the code in your file, that produces the error, it would take seeing the actual code that produces the error for any one to be able to help you find what is producing the error.

 

And you have single-quotes around your table and column names in your query. That makes them strings instead of table and column names and will produce a sql syntax error.

Link to comment
Share on other sites

Okay, the following is the only PHP code in my file.  It is a direct copy and paste from the actual file. The rest is straight HTML, no javascript or anything else.  I realize that using the IP address is not a reliable way to address the actual person because many people on the same network may have the same IP address.  But I'm just learning to combine PHP with MySQL and it is a worthy exercise.  Even if I decide to not use the code, I need to learn to be able to reliably pass data to and from the database and to and from the web page.  This is just my latest attempt.  Here's the code:

 

<?php 

// connect to database
$link = mysql_connect('the_path_to_MySQL', 'actualUserName', '**a real password***'); 
if (!$link) { 
die('Could not connect: ' . mysql_error()); 
} 
echo "connected";

mysql_select_db(guestlist);

// get IP address from the guest
$Users_IP_address = $_SERVER["REMOTE_ADDR"] ;
echo "<input type='hidden' name='ip' value=" . $Users_IP_address . ">";

// find out if user has a record.
$first_name = mysql_query("SELECT 'fname' FROM 'tblMain' WHERE 'ip' =" . $Users_IP_address . ");"

echo "<p>Hello " . $first_name . "</p>";

?>

 

 

Thank you.

Link to comment
Share on other sites

You're missing the terminating semi-colon on this line:

<?php
$first_name = mysql_query("SELECT 'fname' FROM 'tblMain' WHERE 'ip' =" . $Users_IP_address . ");"
?>

 

BTW, that line won't get you the value of the first_name, it's only returning the pointer to the data returned. You need to fetch the data.

This should be written as

<?php
$q = "SELECT fname FROM tblMain WHERE ip = '" . $Users_IP_address . "'";
$rs = mysql_query($q) or die("Problem in the query: $q<br>" . mysql_error());
$rw = mysql_fetch_assoc($rs);
$first_name = $rw['fname'];
?>

 

Ken

Link to comment
Share on other sites

Thanks.  I will try this in a minute.  But I wanted to mention that your approach to setting up the variables and using the crazy combination of single and double quotes reminds me of the days when I used VBA in MS-Access.  It was the same headache of finding the missing semicolon or deciding whether I should type

"'"

or

'"'

.  They look the same, but one is double, single, and double quotes, and the other is single, double, and single quotes.  Maybe this is why I'm bald.  I think I pulled out all my hair during those years.

 

I'll get back to this post after I try your suggestion.  Hopefully I will be able to click on the "solved" button.

Link to comment
Share on other sites

Thank you.  I modified your code as follows:

 

<?php
$link = mysql_connect('path_to_MySQL ', 'username', 'users_Password'); 
if (!$link) { 
die('Could not connect: ' . mysql_error()); 
} 
mysql_select_db(guestlist);
$Users_IP_address = $_SERVER["REMOTE_ADDR"] ;
$q = "SELECT fname FROM tblMain WHERE ip = '" . $Users_IP_address . "'";
$rs = mysql_query($q) or die("Problem in the query: $q<br>" . mysql_error());
$rw = mysql_fetch_assoc($rs);
$first_name = $rw['fname'];
if (!is_null($first_name))
   echo "<p>Hello " . $first_name . ",</p>";
else {
   echo "Hello stranger! Would you like to <a href='guestform.php'>become better acquainted?</a>";
// at this point I'll greet the new user and offer him/her a chance
// to fill out a form that populates the database with a new record
// with contact information, etc., including their IP address.
}
?>

 

This works.

 

Now, in the case of Null, I'll place an optional link to the form that has has all the contact information and sends it to the database.  If the guest wishes to remain anonymous, he or she doesn't need to fill out the form.

 

Many thanks to everybody who read and attempted to help.  We got it working.

Link to comment
Share on other sites

You might want to use a cookie instead of IP address. You could even store the user's name in the cookie and not even make a database call. Of course, they could change that name or any other data in the cookie. Another solution would be to store a hash in the cookie which uniquely identifies that user. Load their details based on that. IP just isn't reliable enough; some people have dynamic IP addresses which change frequently, some people have shared IP addresses, and some might not even report the correct IP address or none at all.

Link to comment
Share on other sites

RE: Reply #11;

 

Yes, I am aware of cookies.  Thank you. You are correct.  However, I have another reason for wanting to learn this.  Let's say, for example, I want to make a survey or poll.  I want to prevent some nut job from voting 20 times for his/her favorite answer.  If I simply used cookies, the voter could delete the cookie and vote multiple times.

 

:)

 

I'm on a home computer in a home office with three computers and two printers on the same router.  If I go to a different computer I notice it has the same IP address.  A cookie would solve that problem because each computer would have a different set of cookies.  But that doesn't solve the problem of multiple votes in the case of a poll or survey where the voter can simply delete the cookie.

 

Also, many users have their browser set to not accept cookies.

 

I don't know what to do about the dynamic IP addresses.  I guess both solutions have both positive and negative aspects.

Link to comment
Share on other sites

First, you wouldn't at all rely on a cookie to prevent duplicate votes. You also wouldn't rely on IP address for that either. You would rely on the user login. That's it...and the user login should not be based on IP, which is how you're currently fetching users.

 

It's not that both have positive and negative aspects, it's that both have different uses.

 

..and this:

 

many users have their browser set to not accept cookies.

 

Is a major exaggeration. While we're guessing I would say 'some' at most. I would also say it depends entirely on your average visitor profile.

 

 

Link to comment
Share on other sites

Okay, so I have a different proposal.  My database already has almost all the fields I would need to setup a user login.  I would need to add at least two more fields: userid and password. NO!  Those aren't fields.  I would have to allow them to log into the database and they would have to have only limited permissions.  I would have to set up some kind of query that allowed a new submission or vote only if they've never voted on the current poll.  I'll need to learn more about either the MySQL engine or about the current versions of both MySQL and PHP  to learn more about user rights, etc.  I suppose some of these issues can be handled programatically with PHP, and that some can be handled with user rights in MySQL.  I'll download the current manuals and study them.

 

Thank you for your ideas.

Link to comment
Share on other sites

RE: Reply #16 on: Today at 06:02:18 PM

 

That's good to know.  So I will be the only user in the database and the guests will be records in the db.  Then I add two new columns: username and password?  Would I have the password column encrypted?  Would I have another column called old_password in case the user needs to change his/her password?  Maybe a secret question and answer column too.  I don't know about that.  I can never remember my secret questions/answers when I'm asked.  Maybe I should set up some kind of "Captcha."

 

I'm accustomed to MS-Access.  Joined tables are disabled in MySQL at my web provider.  But I guess I don't need permanent Access-style joins because I can hard code joins using queries.  For this purpose, however, I only think I'll need one table.

 

Thank you.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.