|
|
|
| Web Hosting Guide |
![]() ![]() |
Something Wrong With This Script?, Unexpected T_SRING |
Sep 22 2007, 04:28 AM
Post
#1
|
|
|
Super Member Group: [HOSTED] Posts: 875 Joined: 12-July 06 From: Ontario, Canada Member No.: 14,464 myCENTs:89.55 |
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? |
|
|
|
Sep 22 2007, 04:45 AM
Post
#2
|
|
|
Oh come on Mrs. B! 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 |
|
|
|
Sep 22 2007, 05:52 AM
Post
#3
|
|
|
Premium Member 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) |
|
|
|
Sep 22 2007, 11:18 AM
Post
#4
|
|
|
Super Member Group: [HOSTED] Posts: 876 Joined: 8-April 06 From: Lima - Peru Member No.: 12,579 myCENTs:13.21 |
QUOTE(FirefoxRocks @ Sep 21 2007, 11:28 PM) [snapback]111269[/snapback] 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, |
|
|
|
Sep 22 2007, 04:28 PM
Post
#5
|
|
|
Super Member Group: [HOSTED] Posts: 875 Joined: 12-July 06 From: Ontario, Canada Member No.: 14,464 myCENTs:89.55 |
QUOTE(TavoxPeru @ Sep 22 2007, 06:18 AM) [snapback]111285[/snapback] 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? |
|
|
|
Sep 23 2007, 07:13 AM
Post
#6
|
|
|
Super Member Group: [HOSTED] Posts: 876 Joined: 8-April 06 From: Lima - Peru Member No.: 12,579 myCENTs:13.21 |
QUOTE(FirefoxRocks @ Sep 22 2007, 11:28 AM) [snapback]111300[/snapback] 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:
For a complete explanation check the Type Casting and the mysql_real_escape_string() sections of the manual at the php website. Best regards, |
|
|
|
Sep 23 2007, 10:35 AM
Post
#7
|
|
|
Nenad Bozidarevic Group: [MODERATOR] Posts: 1,087 Joined: 7-November 05 From: Belgrade, Serbia Member No.: 9,500 myCENTs:42.34 |
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
|
|
|
|
Sep 23 2007, 02:10 PM
Post
#8
|
|
|
Absolute Newbie Group: Admin Posts: 888 Joined: 20-February 05 From: Indianapolis, Indiana, USA (Midwest) Member No.: 2,714 myCENTs:35.43 |
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 |
|
|
|
Sep 24 2007, 02:44 AM
Post
#9
|
|
|
Super Member Group: [HOSTED] Posts: 875 Joined: 12-July 06 From: Ontario, Canada Member No.: 14,464 myCENTs:89.55 |
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?
|
|
|
|
Sep 24 2007, 06:16 AM
Post
#10
|
|
|
Absolute Newbie Group: Admin Posts: 888 Joined: 20-February 05 From: Indianapolis, Indiana, USA (Midwest) Member No.: 2,714 myCENTs:35.43 |
QUOTE(FirefoxRocks @ Sep 23 2007, 10:44 PM) [snapback]111353[/snapback] 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 < and > 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 |
|
|
|
![]() ![]() |
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:
Similar Topics
| Topic Title | Replies | Topic Starter | Views | Last Action | |||
|---|---|---|---|---|---|---|---|
![]() |
8 | bluefish | 3,126 | 16th March 2010 - 02:01 PM Last post by: iG-op |
|||
![]() |
7 | Niru | 4,451 | 20th February 2010 - 07:04 AM Last post by: iG-suresh |
|||
![]() |
19 | Feelay | 9,175 | 18th February 2010 - 08:47 AM Last post by: iG-Tsholofelo |
|||
![]() |
10 | Eggie | 8,414 | 10th February 2010 - 06:47 AM Last post by: iG- |
|||
![]() |
6 | Skepticus | 187 | 7th February 2010 - 02:16 AM Last post by: 8ennett |
|||
![]() |
0 | 8ennett | 70 | 30th January 2010 - 11:10 PM Last post by: 8ennett |
|||
![]() |
25 | JohnNitro | 15,372 | 26th January 2010 - 11:30 AM Last post by: iG-mdshare |
|||
![]() |
3 | wutske | 1,199 | 18th December 2009 - 07:36 AM Last post by: iG-anil kumar |
|||
![]() |
23 | TavoxPeru | 13,110 | 6th December 2009 - 01:38 PM Last post by: iG-Alex Finney |
|||
![]() |
10 | yordan | 2,539 | 27th October 2009 - 11:31 PM Last post by: HannahI |
|||
![]() |
2 | pbolduc | 3,557 | 3rd October 2009 - 08:20 PM Last post by: iG-Andrew |
|||
![]() |
11 | soleimanian | 5,154 | 22nd September 2009 - 12:01 PM Last post by: iG- |
|||
![]() |
7 | Eggie | 2,614 | 9th September 2009 - 02:22 AM Last post by: iG-nate |
|||
![]() |
12 | m3th | 8,029 | 29th August 2009 - 12:16 AM Last post by: dmnhunter |
|||
![]() |
2 | Alex@Synergy | 4,915 | 23rd July 2009 - 02:11 PM Last post by: iG-peter |
|||
|
Lo-Fi Version | Time is now: 19th March 2010 - 12:55 PM |
© 2010 AstaHost: Free Web Hosting & Technical Discussion, Free Web Hosting. a member of xisto.
Powered by Invision Board. Skin: IPB Forum Skins
Expand / Collapse Navigation



Sep 22 2007, 04:28 AM






