|  | |  |
04-12-2005, 12:05 PM
|
#1 (permalink)
| | Registered User
Join Date: Apr 2005
Posts: 2
| Please Help Hi I am having a problem with understanding how to put data into mysql using php
Here is a script I am using.
I found it on a tutorial on the web and did exactly what it said to do. But when I try to check it. I get an error:
Parse error: syntax error, unexpected T_VARIABLE in C:\Documents and Settings\John Sims\My Documents\phpandmyql\scripts_named\12\html\test1.p hp on line 26
Can anyone help me. Please
John PHP Code:
// Set Mysql Variables
$host = 'localhost';
$user = 'root';
$pass = '';
$db = 'ex';
$table = 'comment';
// Set global variables to easier names
$uname = $_GET['uname'];
$ucomment = $_GET['ucomment'];
// Connect to Mysql, select the correct database, and run teh query which adds the data gathered from the form into the database
mysql_connect($host,$user,$pass) or die(mysql_error());
mysql_select_db($db) or die(mysql_error());
$add_all = 'INSERT INTO $table values('$uname','$ucomment','$ip','$time','')';
mysql_query($add_all) or die(mysql_error());
}
else
{
// If the form has not been submitted, display it!
?>
<form method='get' action='<? echo'$PHP_SELF'; ?>'>
Name : <input type='text' name='uname'><br><br>
Comment : <input type='text' name='ucomment'><br><br>
<input type='hidden' name='commented' value='set'>
<input type='submit' value='Post your comment'>
</form>
<?
}
?> |
| |
04-12-2005, 01:29 PM
|
#2 (permalink)
| | Moderator
Join Date: May 2002 Location: us.ca
Posts: 4,532
| Hi John, welcome to the site  ..
The problem 'MIGHT' be in your form line. It is really hard to tell because I don't know where line 26 falls on your script. Next time it would be helpful if you point out what line is 26.
Try changing this line: PHP Code:
<form method='get' action='<? echo $PHP_SELF; ?>'> now that i look at it, i am 99% sure that will correct your problem.
__________________ Mike |
| |
04-12-2005, 03:39 PM
|
#3 (permalink)
| | Registered User
Join Date: Apr 2005
Posts: 2
| That didn't help Sorry, but that didn't help. The problem is in this line
$add_all = 'INSERT INTO $table values('$uname','$ucomment','$ip','$time','')'; |
| |
04-12-2005, 03:42 PM
|
#4 (permalink)
| | Senior Contributor
Join Date: Mar 2005
Posts: 745
| TIP: Don't use <? but use the correct coding <?php
Open php.ini and change the short_open_tag directive to Off and you will understand. Code: short_open_tag = Off Don't use $PHP_SELF, instead use $_SERVER['SCRIPT_NAME'] or basename(__FILE__) |
| |
04-12-2005, 03:46 PM
|
#5 (permalink)
| | Senior Contributor
Join Date: Mar 2005
Posts: 745
| Quote: |
Originally Posted by jsims Sorry, but that didn't help. The problem is in this line
$add_all = 'INSERT INTO $table values('$uname','$ucomment','$ip','$time','')'; | The following way is the only correct way: PHP Code: $add_all = "INSERT INTO $table values('$uname','$ucomment','$ip','$time','')";
For example use the following test to see the difference PHP Code: <?php
$test = 'string';
echo $test.'<br>';
echo "$test<br>";
echo '$test<br>'; The output is: Code: string<br>
string<br>
$test<br> it's the difference between single and double quotes |
| |
04-12-2005, 03:57 PM
|
#6 (permalink)
| | Moderator
Join Date: May 2002 Location: us.ca
Posts: 4,532
| Quote: |
Originally Posted by jsims Sorry, but that didn't help. The problem is in this line
$add_all = 'INSERT INTO $table values('$uname','$ucomment','$ip','$time','')'; | oops, didn't mean to make u angry 
__________________ Mike |
| |
04-12-2005, 04:48 PM
|
#7 (permalink)
| | Registered User
Join Date: Jan 2004 Location: Poughkeepsie, NY
Posts: 18
| Quote: |
Originally Posted by DJMaze TIP: Don't use <? but use the correct coding <?php
Open php.ini and change the short_open_tag directive to Off and you will understand. Code: short_open_tag = Off Don't use $PHP_SELF, instead use $_SERVER['SCRIPT_NAME'] or basename(__FILE__) | I can understand not using short tags, but can you explain why $_SERVER['PHP_SELF'] shouldn't be used? |
| |
04-12-2005, 05:07 PM
|
#8 (permalink)
| | Moderator
Join Date: May 2002 Location: us.ca
Posts: 4,532
| $_SERVER['PHP_SELF'] is perfectly fine.
i screwed up and corrected the syntax around the variable without paying attention that $_SERVER was missing.
not sure why DJ prefers SCRIPT_NAME.
__________________ Mike |
| |
04-13-2005, 02:14 AM
|
#9 (permalink)
| | Senior Contributor
Join Date: Mar 2005
Posts: 745
| 'PHP_SELF'
The filename of the currently executing script, relative to the document root. For instance, $_SERVER['PHP_SELF'] in a script at the address http://example.com/test.php/foo.bar would be /test.php/foo.bar. The __FILE__ constant contains the full path and filename of the current (i.e. included) file. If PHP is running as a command-line processor this variable contains the script name since PHP 4.3.0. Previously it was not available. 'SCRIPT_NAME'
Contains the current script's path. This is useful for pages which need to point to themselves. The __FILE__ constant contains the full path and filename of the current (i.e. included) file. |
| |
04-13-2005, 06:44 AM
|
#10 (permalink)
| | Moderator
Join Date: May 2002 Location: us.ca
Posts: 4,532
| that's great to know DJ. so i guess if you are not writting command-line scripts i guess it still doesn't matter? and if you are, then it still doesn't matter if you are using v 4.3.x or greater?
hmm .. i just tried to run this script in CGI command-line mode, and neither variable returns anything. PHP Code:
#!/usr/bin/php
<?
echo "script name: ".$_SERVER['SCRIPT_NAME']."\n";
echo "php self: ".$_SERVER['PHP_SELF']."\n";
?> Quote: |
Originally Posted by output $ ./test.php
Content-type: text/html
X-Powered-By: PHP/4.3.10
script name:
php self: | aahh, .. a simple print_r($_SERVER) will show there is not a lot of server vars available... and a whole different set of them too. ( SCRIPT_NAME doesn't even show up. PHP_SELF does, except it has no value )
i'm testing on php version 4.3.10
i think it's safe to say it still doesn't matter whichever you use.
__________________ Mike |
| |
04-13-2005, 02:36 PM
|
#11 (permalink)
| | Senior Contributor
Join Date: Mar 2005
Posts: 745
| Strange, anyway my bad then. Ignore my comment  |
| |
04-13-2005, 02:44 PM
|
#12 (permalink)
| | Moderator
Join Date: May 2002 Location: us.ca
Posts: 4,532
| but your comments come from the manual, .. i wonder why the manual mentions command line like that. the logic makes sense, but i wonder why they don't make the script name available to you.
__________________ Mike |
| |
04-13-2005, 08:22 PM
|
#13 (permalink)
| | Senior Contributor
Join Date: Mar 2005
Posts: 745
| I don't have time to investigate it though.
basename(__FILE__) will always work unless you use a Code: <base href="http://my-website.com/" /> in a subdirectory on the web |
| | | Thread Tools | | | | Display Modes | Linear Mode |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | All times are GMT -8. The time now is 10:50 PM. |
Copyright © 2000-2008, Milano Interactive Web Hosting provided by Portal 360 Web Hosting |  | |