Jump to content
  • Announcements

    • boen_robot

      Guidelines and Netiquette   03/28/2017

      Posting Problems:   Having problems posting your topic? Read through this: To join, you agree to our terms and conditions and fill out and submit a registration form. An activation email will be sent to your email adress, so you'll need to verify your account. After that the account has to be validated by one of the moderators. This will mean that it can take up to a day to be activated. A couple of things to remember to ensure approval: Don't use an email address in one of those $2 four character .com domains eg. xyds.com. These will be deleted and the domain added to the banned list. Don't use an email address that is within a domain with a bad reputation for spam. A Google search is run on every email address and email domain. Don't sign up with an email address that doesn't exist, doesn't work or requires the sender to answer a quiz before their email can get to you. Put your country and or state and city in the signup form. Blank forms will go to the botton of the "to do" list. And make sure that your email address and your country match, saying you're from Alabama and using a .ru email address is not going to get you activated. After a membership is activated the first few posts will be monitored. Posting spam or unapproved topics described in the agreement results in an immediate ban. The email provider and the IP addresses associated with the account will be banned and all posts will be deleted. These strict measures have been deemed necessary to hinder spam. Sorry for any inconvenience this causes, but it's not liable to change. If, after reading this, you still can't post and don't understand why, contact one of the Moderators listed here.   Topic Guidelines   Including the following information can expedite an accurate response from board members: Must be a Specific Problem or Question related to web design and development Include Code in Question (wrap with   for small blocks of code and for longer blocks   ) Include Code Author Include Extra Notes/Modifications/Attempts Include web link to page/file when possible Content Guidelines   You may not post, upload, link to, or email any Content that contains, promotes, gives instruction about, or provides prohibited Content. Prohibited Content includes any Content that breaks any local, state, county, national or international law. Prohibited Content also includes: No direct or indirect advertising or websites, forums, products, services No hijacking of posts (do not post your question in someone elses) Content that infringes upon any rights [ex. MP3s and ROMs] (including, but not limited to, copyrights and trademarks) Abusive, threatening, defamatory, racist, or obscene Viruses or any other harmful computer software False Information or libel Spam, chain letters, or Pyramid schemes Gambling or Illicit drugs Terrorism Hacking or cheating for internet/online games Warez, Roms, CD-Keys, Cracks, Passwords, or Serial Numbers Pornography, nudity, or sexual material of any kind Excessive profanity Invasive of privacy or impersonation of any person/entity Hacking materials or information Posting Tips   There are more BBcodes than there are buttons for on the reply menu. To get the full list, click "BB Code Help" underneath the clickable smilie face menu. Use   for small snippets of code Use   for lengthy snippets of code Use   if your snippet is HTML (optional) Use   if your snippet is SQL (optional) Rules of Conduct   Be nice. There's no need for calling someone stupid if they ask an 'easy' question. Keep your avatars and signatures absolutely child friendly. We have a younger audience on this forum. Keep your language appropriate for the same reason above. Do not PM moderators for help on the forum. Post on the topic, or create a new one.   Spam:   Recently, as you have all without doubt noticed, we have had lots of spam and advertisement on the forum. Therefore, we'd like to alert you as to what to do when you have found any of the aforementioned annoying messages: it. Immediately. Give a clear reason, please, if the advertising is not evident. DO NOT POST! Report, let the post stay as is, and we will get to it, meanwhile if you continue to post as normal in the other threads, it won't be on the top so long. Refrain from PMing the member. This won't help at all, as they are most likely spambots anyway. Thank you.       Images in signatures:   After thinking of users on dial-up, we have decided to enforce the following rules regarding signatures. Please pay heed to them. Respecting these rules is respecting the members on this forum with dial-up. Signature rules: No animated images AT ALL. No matter the amount of animation. Maximum image widthxheight: 300x150 Maximum image (file) size: 15kb Use calm colors. Do not use highly contrasting images in your signature, as this can get really annoying when seeing several posts from one member in the same thread. The same prohibited content goes for images as for posts. Lastly, use common sense. No lengthy signatures please. Save us some scrolling. Thank you.       Links in signatures:   Please understand that w3schools.com only exists because of voluntary work and is barely supported by the advertising littered throughout the tutorials and the forum. So, please, stop advertising other sites. DO not post links that drive traffic away from the w3schools domain - especially to a site that offers similar if not identical information. Please help support the site by keeping individuals on it. Thank you. Here are some guidelines as to what you can put in your signature: w3schools links --> allowed w3.org links --> allowed browser links --> allowed html editor links --> allowed personal sites --> allowed tutorial sites competing with w3schools --> NOT allowed sites completely irrelevant to webprogramming and this forum --> NOT allowed   Thanks for understanding, and for taking the time to read this. ~W3Schools Modstaff~
Rollins

need some help with debugging, htmlentities an escapestrings.

Recommended Posts

hey all! can someone please give me some advice on debugging and security matters....... i am working on a script to insert data ina DB and it got me stuck on a error wich i think is being caused by the escape string or the $sql query. tried changing the order of events....without the escapestring.....and some random things but it keeps giving me: ERROR: 1064You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(dt, username, email, pwd)values('Saturday 7 April 2012 14:11:42', '1', '2', '8'' at line 1 the code that's bugging me.....

//request data from form(method=post)$username = htmlentities($_REQUEST['username']);$email = htmlentities($_REQUEST['email']);$password = htmlentities($_REQUEST['password']);$datetime = date("l j F Y H:i:s"); //create date time //connect dbmysql_connect("$db_host", "$db_username", "$db_password")or die("cannot connect");mysql_select_db("$db_name")or die("cannot select DB"); //clean data$res_username = mysql_real_escape_string ($username);$res_email = mysql_real_escape_string ($email);$res_password = mysql_real_escape_string ($password); if (empty ($res_username) or empty($res_email) or empty($res_password) or empty($datetime)){		  die ('error!');		  } //insert data$sql="insert into $tbl_name(dt, username, email, pwd)values('$datetime', '$res_username', '$res_email', '$res_password')";$result=mysql_query($sql);if (!$result){  die('ERROR: ' . mysql_errno()  .mysql_error());} mysql_close();

what's going wrong here? can someone give me advice on how to deal with issues like this? is this a good way to add db security? do i need to request htmlentities AND a mysql escape string? thanks Roll!

Edited by Rollins

Share this post


Link to post
Share on other sites

what does $query print?

Share this post


Link to post
Share on other sites

you mean the $sql string where i insert the date? insert into (dt, username, email, pwd)values('Saturday 7 April 2012 15:58:58', '1', '2', '8')

Share this post


Link to post
Share on other sites

ERROR: 1064You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(dt, username, email, pwd)values('Saturday 7 April 2012 14:11:42', '1', '2', '8'' at line 1 .....should the end not be '8')' ?

Share this post


Link to post
Share on other sites

I think there should be spaces around the word "values" and the field list, i.e.

$sql="insert into $tbl_name (dt, username, email, pwd) values ('$datetime', '$res_username', '$res_email', '$res_password')";

Share this post


Link to post
Share on other sites

i wish it was that simple.....tried spacing it! actually it really was a simple thing......

$sql="INSERT INTO $tbl_name(username, password, email, datetime) VALUES ('$res_username', '$res_password', '$res_email', '$datetime')";

$sql="INSERT INTO $table_name(username, password, email, datetime) VALUES ('$res_username', '$res_password', '$res_email', '$datetime')";

i screwed up the table name....sorry to waste your time. i'll be looking out for a debug checklist..

Edited by Rollins

Share this post


Link to post
Share on other sites
you mean the $sql string where i insert the date? insert into (dt, username, email, pwd)values('Saturday 7 April 2012 15:58:58', '1', '2', '8')
set your php.ini error level to E_ALL and display error is enabled. so that you would catch it easily here that tablename is missing and is not geting evaluated.

Share this post


Link to post
Share on other sites

thanks for your advice....ill see what i can find on that how to use it. something els has got my attention now.... the data from the form is is sanitized by the htmlentities function first and then by mysql_real_escape_string (). now when a data is passed with slashes and ampersands it does not do what i expected it would do and that is alter the data.... no \ is added to the ' for instance should the data go through the escape string first and then strip the html entities?

Share this post


Link to post
Share on other sites

mysql_real_escape_string() will be the always last one which will you use before using it in query. escaping is for the chafraters which have speical meanings to database. so it is just escaped when it executed by database engine. once it is executed it will be the same character. it does not change the value at last. once the query executed there is no meaning of escaping.thus you cant see the escapiing characters in database.

Edited by birbal

Share this post


Link to post
Share on other sites

aaah oke? so.....

$res_username = mysql_real_escape_string ($username);

does not send '$username' but it sends the entire 'mysql_real_escape_string ($username)' to the DB wich filters the data to save data to insert?

Share this post


Link to post
Share on other sites

Neither.mysql_real_escape_string() takes $username, and turns it into something which can be places safely between apostrophes in an SQL query. This something is, in this case, stored in $res_username.Suppose $username contained an apostrophe, like "O'Reilly". If you just place that in an SQL query, like:

"INSERT INTO users (username) VALUES ('$username')"

the result would be

INSERT INTO users (username) VALUES ('O'Reilly')

which results in a syntax error.mysql_real_escape_string() handles this character, among others, by "escaping" it, so that

"INSERT INTO users (username) VALUES ('$res_username')"

would look like

INSERT INTO users (username) VALUES ('O\'Reilly')

which is a valid SQL query. One which results in "O'Reilly" being inserted into the DB.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×