Welcome Guest ( Log In | Register )



 
Reply to this topicStart new topic
> Something Wrong With This Script?, Unexpected T_SRING
FirefoxRocks
post Sep 22 2007, 04:28 AM
Post #1


Super Member
Group Icon

Group: [HOSTED]
Posts: 696
Joined: 12-July 06
From: Ontario, Canada
Member No.: 14,464



Here is the code that I have:
CODE
<?php
$con = mysql_connect("localhost","user","password");
if (!$con)
  {die('<p>Could not connect: ' . mysql_error() . '</p>');}
mysql_select_db("database", $con);
$ip=$_SERVER['REMOTE_ADDR'];
echo "Adding MXP info...";
mysql_query (INSERT INTO mxp (date, user, victim, turns, side, gold, lost, killed, mxp, points_b, points_a, type, power, ip) VALUES ('$_POST[date]','$_POST[user]','$_POST[victim]','$_POST[turns]','$_POST[side]','$_POST[gold]','$_POST[lost]','$_POST[killed]','$_POST[mxp]','$_POST[points_b]','$_POST[points_a]','$_POST[battle]','$_POST[power]','$ip');
echo "<h3>Your MXP information record has been added to the database.</h3>";
?>


Here is the error I am receiving:
QUOTE
Parse error: syntax error, unexpected T_STRING in /home/portal/public_html/xkingdom/post_mxp.php on line 13


Is there a semicolon I missed somewhere? What is wrong?
Go to the top of the page
 
+Quote Post
Sten
post Sep 22 2007, 04:45 AM
Post #2


Oh come on Mrs. B!
Group Icon

Group: Members
Posts: 648
Joined: 6-June 07
From: Tasmania, Australia
Member No.: 22,422



ok well first, im not what youd call any good at php and i dont no anything about mysql... but... there isnt 13 lines of code, lol

but, just looking at it with the semicolons, do you need to put a semicolon after the bracket where it ends with "ip)"

does a semicolon need to go there maybe possibly probably not? LOL

just thought id say, although i can guarantee im wrong biggrin.gif
Go to the top of the page
 
+Quote Post
Habble
post Sep 22 2007, 05:52 AM
Post #3


Premium Member
Group Icon

Group: [HOSTED]
Posts: 286
Joined: 17-June 07
From: Tasmania
Member No.: 22,699



CODE
<?php
$con = mysql_connect("localhost","user","password");
if (!$con)
{
  die('<p>Could not connect: ' . mysql_error() . '</p>');
}
mysql_select_db("database", $con);
$ip = $_SERVER['REMOTE_ADDR'];
echo "Adding MXP info...";
mysql_query ("INSERT INTO mxp (date, user, victim, turns, side, gold, lost, killed, mxp, points_b, points_a, type, power, ip) VALUES ('$_POST[date]','$_POST[user]','$_POST[victim]','$_POST[turns]','$_POST[side]','$_POST[gold]','$_POST[lost]','$_POST[killed]','$_POST[mxp]','$_POST[points_b]','$_POST[points_a]','$_POST[battle]','$_POST[power]','$ip');");
echo "<h3>Your MXP information record has been added to the database.</h3>";
?>



Try That. You'd forgotten to put quotation marks around the query, and had forgotten to end the parentheses (You only ended the VALUES set)
Go to the top of the page
 
+Quote Post
TavoxPeru
post Sep 22 2007, 11:18 AM
Post #4


Super Member
Group Icon

Group: [HOSTED]
Posts: 763
Joined: 8-April 06
From: Lima - Peru
Member No.: 12,579



QUOTE(FirefoxRocks @ Sep 21 2007, 11:28 PM) *
Here is the code that I have:
CODE
<?php
$con = mysql_connect("localhost","user","password");
if (!$con)
  {die('<p>Could not connect: ' . mysql_error() . '</p>');}
mysql_select_db("database", $con);
$ip=$_SERVER['REMOTE_ADDR'];
echo "Adding MXP info...";
mysql_query (INSERT INTO mxp (date, user, victim, turns, side, gold, lost, killed, mxp, points_b, points_a, type, power, ip) VALUES ('$_POST[date]','$_POST[user]','$_POST[victim]','$_POST[turns]','$_POST[side]','$_POST[gold]','$_POST[lost]','$_POST[killed]','$_POST[mxp]','$_POST[points_b]','$_POST[points_a]','$_POST[battle]','$_POST[power]','$ip');
echo "<h3>Your MXP information record has been added to the database.</h3>";
?>


Here is the error I am receiving:
Is there a semicolon I missed somewhere? What is wrong?

You forgot the quotation marks in your query and i recommend to cast your data to the correct type of your table columns.

Best regards,
Go to the top of the page
 
+Quote Post
FirefoxRocks
post Sep 22 2007, 04:28 PM
Post #5


Super Member
Group Icon

Group: [HOSTED]
Posts: 696
Joined: 12-July 06
From: Ontario, Canada
Member No.: 14,464



QUOTE(TavoxPeru @ Sep 22 2007, 06:18 AM) *
You forgot the quotation marks in your query and i recommend to cast your data to the correct type of your table columns.

Best regards,


Cast the data to the correct type of your table columns? What does that mean?
Go to the top of the page
 
+Quote Post
TavoxPeru
post Sep 23 2007, 07:13 AM
Post #6


Super Member
Group Icon

Group: [HOSTED]
Posts: 763
Joined: 8-April 06
From: Lima - Peru
Member No.: 12,579



QUOTE(FirefoxRocks @ Sep 22 2007, 11:28 AM) *
Cast the data to the correct type of your table columns? What does that mean?

This mean to force a variable to be evaluated as a certain type, for example, if one of your table column is an integer -tinyint, smallint, int, longint- you can force that your submited data evaluates as an integer by casting it:

CODE
<?php
$integer_value = (int) $_POST["integer_value"];
?>

The casts allowed are:
  • (int), (integer) - cast to integer
  • (bool), (boolean) - cast to boolean
  • (float), (double), (real) - cast to float
  • (string) - cast to string
  • (array) - cast to array
  • (object) - cast to object
For string variables you can achieve the same behavior simply by enclosing it in double quotes, and also is recommended that you use the mysql_real_escape_string for security reasons.

For a complete explanation check the Type Casting and the mysql_real_escape_string() sections of the manual at the php website.

Best regards,
Go to the top of the page
 
+Quote Post
pyost
post Sep 23 2007, 10:35 AM
Post #7


Nenad Bozidarevic
Group Icon

Group: [MODERATOR]
Posts: 1,013
Joined: 7-November 05
From: Belgrade, Serbia
Member No.: 9,500



As I am not sure what will happen if you try to cast a non-numeric string into an integer (i.e. whether it will produce an error or return zero), I would advise you to use intval instead. It will always return an integer - number zero if the input is invalid wink.gif
Go to the top of the page
 
+Quote Post
vujsa
post Sep 23 2007, 02:10 PM
Post #8


Absolute Newbie
Group Icon

Group: Admin
Posts: 888
Joined: 20-February 05
From: Indianapolis, Indiana, USA (Midwest)
Member No.: 2,714



Yeah, these are difficult query strings to get working. Anytime your values are from an array (in this case, the $_POST superglobal) and you use a non-numeric key, you'll have trouble since you'll have so many quotes that'll be impossible to escaped.

Here is how I usually get it to work:
CODE
mysql_query ("INSERT INTO mxp ("
        . "\n 'date',"
        . "\n 'user',"
        . "\n 'victim',"
        . "\n 'turns',"
        . "\n 'side',"
        . "\n 'gold',"
        . "\n 'lost',"
        . "\n 'killed',"
        . "\n 'mxp',"
        . "\n 'points_b',"
        . "\n 'points_a',"
        . "\n 'type',"
        . "\n 'power',"
        . "\n 'ip',"
        . "\n ) VALUES ("
        . "\n '". $_POST['date'] . "',"
        . "\n '". $_POST['user'] . "',"
        . "\n '". $_POST['victim'] . "',"
        . "\n '". $_POST['turns'] . "',"
        . "\n '". $_POST['side'] . "',"
        . "\n '". $_POST['gold'] . "',"
        . "\n '". $_POST['lost'] . "',"
        . "\n '". $_POST['killed'] . "',"
        . "\n '". $_POST['mxp'] . "',"
        . "\n '". $_POST['points_b'] . "',"
        . "\n '". $_POST['points_a'] . "',"
        . "\n '". $_POST['battle'] . "',"
        . "\n '". $_POST['power'] . "',"
        . "\n '$ip'"
        . "\n  ) ");

That is how I usually write such queries but I guess you could do it linear like this:
CODE
mysql_query ("INSERT INTO mxp ( 'date', 'user', 'victim', 'turns', 'side', 'gold', 'lost', 'killed', 'mxp', 'points_b', 'points_a', 'type', 'power', 'ip', ) VALUES ( '". $_POST['date'] . "', '". $_POST['user'] . "', '". $_POST['victim'] . "', '". $_POST['turns'] . "', '". $_POST['side'] . "', '". $_POST['gold'] . "', '". $_POST['lost'] . "', '". $_POST['killed'] . "', '". $_POST['mxp'] . "', '". $_POST['points_b'] . "', '". $_POST['points_a'] . "', '". $_POST['battle'] . "', '". $_POST['power'] . "', '$ip'  ) ");


I prefer the column form since it is easier to see everything at once.

Notice how I used the concatenation character "period" to put string and non-string values together. There is also a concatenation function in MySQL that you can use.

Remember, there are three quotes you can use in queries:
(`)(')(")
The slanted single quote is good inside of MySQL queries but don't affect PHP so you could, I believe, also do it like this:
CODE
mysql_query (
    "INSERT INTO mxp ("
        . "\n `date`,"
        . "\n `user`,"
        . "\n `victim`,"
        . "\n `turns`,"
        . "\n `side`,"
        . "\n `gold`,"
        . "\n `lost`,"
        . "\n `killed`,"
        . "\n `mxp`,"
        . "\n `points_b`,"
        . "\n `points_a`,"
        . "\n `type`,"
        . "\n `power`,"
        . "\n `ip`,"
        . "\n ) VALUES ("
        . "\n `$_POST['date']`,"
        . "\n `$_POST['user']`,"
        . "\n `$_POST['victim']`,"
        . "\n `$_POST['turns']`,"
        . "\n `$_POST['side']`,"
        . "\n `$_POST['gold']`,"
        . "\n `$_POST['lost']`,"
        . "\n `$_POST['killed']`,"
        . "\n `$_POST['mxp']`,"
        . "\n `$_POST['points_b']`,"
        . "\n `$_POST['points_a']`,"
        . "\n `$_POST['battle']`,"
        . "\n `$_POST['power']`,"
        . "\n `$ip`"
    . "\n )"
);


Or in linear form:
CODE
mysql_query (     "INSERT INTO mxp ( `date`, `user`, `victim`, `turns`, `side`, `gold`, `lost`, `killed`, `mxp`, `points_b`, `points_a`, `type`, `power`, `ip`, ) VALUES ( `$_POST['date']`, `$_POST['user']`, `$_POST['victim']`, `$_POST['turns']`, `$_POST['side']`, `$_POST['gold']`, `$_POST['lost']`, `$_POST['killed']`, `$_POST['mxp']`, `$_POST['points_b']`, `$_POST['points_a']`, `$_POST['battle']`, `$_POST['power']`, `$ip`)" );


Just remember, you should use the single quotes around your array key name if it isn't a numeric value. You can't escape the single quotes that you use for the array key either. You can, I suppose, escape the single quote used in the query since PHP would as a result ignore it but it would then be available for MySQL to see. like so:
CODE
mysql_query (     "INSERT INTO mxp ( \'date\', \'user\', \'victim\', \'turns\', \'side\', \'gold\', \'lost\', \'killed\', \'mxp\', \'points_b\', \'points_a\', \'type\', \'power\', \'ip`, ) VALUES ( \'$_POST['date']\', \'$_POST['user']\', \'$_POST['victim']\', \'$_POST['turns']\', \'$_POST['side']\', \'$_POST['gold']\', \'$_POST['lost']\', \'$_POST['killed']\', \'$_POST['mxp']\', \'$_POST['points_b']\', \'$_POST['points_a']\', \'$_POST['battle']\', \'$_POST['power']\', \'$ip\')" );


The only method I am sure will work, is the first one I showed you. You might give the others a try sometime. I don't feel like writing a whole script just to test each option.

However, I do have another concern with your script!
Your script is attempting to directly input any data from your form to your database. This is not a very good method. If the user has a malicious intent, they could inject code into your database creating a serious security risk to your website. Prior to insertion into the database, you really should screen the data.

For example, you could convert HTML Entities into something a little less problematic if it contains malicious code:
CODE
$date = htmlentities($_POST['date']);
$user = htmlentities($_POST['user']);
$victim = htmlentities($_POST['victim']);
$turns = htmlentities($_POST['turns']);
$side = htmlentities($_POST['side']);
$gold = htmlentities($_POST['gold']);
$lost = htmlentities($_POST['lost']);
$killed = htmlentities($_POST['killed']);
$mxp = htmlentities($_POST['mxp']);
$points_b = htmlentities($_POST['points_b']);
$points_a = htmlentities($_POST['points_a']);
$battle = htmlentities($_POST['battle']);
$power = htmlentities($_POST['power']);


In your case, this suggestion actually makes your query a lot easier to write.

Hope this helps,
vujsa
Go to the top of the page
 
+Quote Post
FirefoxRocks
post Sep 24 2007, 02:44 AM
Post #9


Super Member
Group Icon

Group: [HOSTED]
Posts: 696
Joined: 12-July 06
From: Ontario, Canada
Member No.: 14,464



So if I use the variables in the script immediately above, what will happen to the HTML entities when they are inserted into the MySQL database?

Go to the top of the page
 
+Quote Post
vujsa
post Sep 24 2007, 06:16 AM
Post #10


Absolute Newbie
Group Icon

Group: Admin
Posts: 888
Joined: 20-February 05
From: Indianapolis, Indiana, USA (Midwest)
Member No.: 2,714



QUOTE(FirefoxRocks @ Sep 23 2007, 10:44 PM) *
So if I use the variables in the script immediately above, what will happen to the HTML entities when they are inserted into the MySQL database?

Basically, < and > become &lt; and &gt;

You can use html_entity_decode() to revert back to actual HTML tags. It is something to consider doing I think.
But, if you are expecting HTML in one of the input fields, then you could skip the htmlentities() function and just insert the data. But, you should investigate some security protocols for this as well. What hackers tend to do is use the eval() function along with a long string which is actually an include(), require, or file_get_contents() command to load script from their server to manipulate your database or file system.
This usually results in an upload to your website where they can show their hacker friends what they did but they could run a database query to add an Admin account for their username, add a file system program to you system which allows them to browse and manipulate your files which could result in total deletion or replacement.

they usually look something like this:
CODE
eval(char(118)char(117)char(106)char(115)char(97))

In this case, it just says vujsa but it could have been malicious.

vujsa
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. Trainable Anti-spam Filter Script(3)
  2. Php Script To Download File From Another Site(9)
  3. Need Help With A PHP - MySQL Registration Script(13)
  4. What Would Make A Good Registration Script?(4)
  5. Auto Responder Script(6)
  6. Blog Script?(5)
  7. Installed A PR Checker Script - But Not Working Correctly(6)
  8. How To Delete File Using PHP Shell Script(3)
  9. Online Multiplayer Chess Script(2)
  10. Automated File Structure Creation Script(3)
  11. Authentication Script(1)
  12. Login Script(5)
  13. Please Help (php Join Script)(5)
  14. Automatic/remote Php Script Execution(9)
  15. Unexpected Error(2)
  1. Automated Product Suggestion Script(2)
  2. Run A Script When Expires A Session(6)
  3. Php Script Help(1)
  4. SQL Doesn't Connect In PHP Script(19)
  5. Warning: Mysql_result(): Supplied Argument Is Not A Valid Mysql Result Resource In ...(4)
  6. Password Recovery Script(6)
  7. Login Script(8)
  8. Free Forum Hosting Type Script Help!(2)
  9. Script Request(2)
  10. Writing And Testing My Own Login Script [solved](20)
  11. Make A Script Run Even If No User Is Online(6)
  12. Php Login Script(0)
  13. Myspacetv Download Php Script Help(6)