Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 04-12-2005, 12:05 PM   #1 (permalink)
jsims
Registered User
 
Join Date: Apr 2005
Posts: 2
jsims is on a distinguished road
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>
<?
}
?>
jsims is offline   Reply With Quote
Old 04-12-2005, 01:29 PM   #2 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,532
sde is on a distinguished road
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
sde is offline   Reply With Quote
Old 04-12-2005, 03:39 PM   #3 (permalink)
jsims
Registered User
 
Join Date: Apr 2005
Posts: 2
jsims is on a distinguished road
Angry 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','')';
jsims is offline   Reply With Quote
Old 04-12-2005, 03:42 PM   #4 (permalink)
DJMaze
Senior Contributor
 
DJMaze's Avatar
 
Join Date: Mar 2005
Posts: 745
DJMaze is on a distinguished road
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__)
DJMaze is offline   Reply With Quote
Old 04-12-2005, 03:46 PM   #5 (permalink)
DJMaze
Senior Contributor
 
DJMaze's Avatar
 
Join Date: Mar 2005
Posts: 745
DJMaze is on a distinguished road
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
DJMaze is offline   Reply With Quote
Old 04-12-2005, 03:57 PM   #6 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,532
sde is on a distinguished road
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
sde is offline   Reply With Quote
Old 04-12-2005, 04:48 PM   #7 (permalink)
nimaj
Registered User
 
Join Date: Jan 2004
Location: Poughkeepsie, NY
Posts: 18
nimaj is on a distinguished road
Send a message via AIM to nimaj Send a message via Yahoo to nimaj
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?
nimaj is offline   Reply With Quote
Old 04-12-2005, 05:07 PM   #8 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,532
sde is on a distinguished road
$_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
sde is offline   Reply With Quote
Old 04-13-2005, 02:14 AM   #9 (permalink)
DJMaze
Senior Contributor
 
DJMaze's Avatar
 
Join Date: Mar 2005
Posts: 745
DJMaze is on a distinguished road
'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.
DJMaze is offline   Reply With Quote
Old 04-13-2005, 06:44 AM   #10 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,532
sde is on a distinguished road
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
sde is offline   Reply With Quote
Old 04-13-2005, 02:36 PM   #11 (permalink)
DJMaze
Senior Contributor
 
DJMaze's Avatar
 
Join Date: Mar 2005
Posts: 745
DJMaze is on a distinguished road
Strange, anyway my bad then. Ignore my comment
DJMaze is offline   Reply With Quote
Old 04-13-2005, 02:44 PM   #12 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,532
sde is on a distinguished road
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
sde is offline   Reply With Quote
Old 04-13-2005, 08:22 PM   #13 (permalink)
DJMaze
Senior Contributor
 
DJMaze's Avatar
 
Join Date: Mar 2005
Posts: 745
DJMaze is on a distinguished road
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
DJMaze is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -8. The time now is 10:50 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0 RC8 ©2007, Crawlability, Inc.





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting