Page 1 of 2 12 LastLast
Results 1 to 10 of 15

Thread: Proper Use of Quotes with POST?

  1. #1
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    642
    Thanks
    284
    Thanked 15 Times in 15 Posts

    Default Proper Use of Quotes with POST?

    Is it OK to use double quotes within the POST variable instead of the single quotes? Like this...

    Code:
    $sql = "UPDATE booking SET country = '$_POST["country"]',... "
    Or do I have to do it like this?

    Code:
    $sql = "UPDATE booking SET country = ' "$_POST['country']" ',... "
    Seems like a preponderance of quotes. Thanks, e

  2. #2
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    You could use either double or single quotes, but you really should break out of the statement before entering in variables. Something like the following:

    Code:
    $sql = "UPDATE booking SET country = '". $_POST["country"] ."',... "
    Hope this helps.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  3. The Following User Says Thank You to thetestingsite For This Useful Post:

    kuau (06-01-2008)

  4. #3
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Actually, that's a really bad idea in the first place. Without converting the data to something you know isn't dangerous, a user could actually inject malicious code into your database.

    Try something like this:

    PHP Code:
    $country mysql_real_escape_string($_POST['country']);
    $sql "blah blah $country blah"
    Now, as for different kinds of quotes, here's how they work:
    ": this will parse the string it contains; variables ($var) will be used, not the literal text "$var"; \n \r \etc will be converted; if you want to avoid any of those, you need to escape the character, like \$var = "$var", not "val". (you will need to escape double quotes like \" within the string).
    ': nothing is converted and single quotes are better because they parse faster, unless you need something converted like above. (you will need to only escape single quotes like \' within the string).
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  5. The Following User Says Thank You to djr33 For This Useful Post:

    kuau (06-01-2008)

  6. #4
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    642
    Thanks
    284
    Thanked 15 Times in 15 Posts

    Default

    Dear TTS and Daniel: Thanks for the info re the quotes. In the situation I described (country), the value comes from a drop-down list in the form. Doesn't that mean that the user cannot enter their own values? If so, would it be necessary to protect against SQL injection? Or do you do it only for fields in which the user can type text? Does mysql_real_escape_string do the same thing as addslashes? Thanks , e

  7. #5
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Anything sent from clientside can be dangerous. There's nothing that guarantees it's an item from the list. (Instead of escaping the values you could just compare with an array of the possible allowed values, but that's more work, unless you need it to match one.)
    Basically, all you need to do is create a mirror form like your page, on any webserver, switch the select to a text field and type in any sort of hack you'd like for your database.

    Basically, if the user sends it, don't trust it, and escape!!
    Last edited by djr33; 06-03-2008 at 08:25 AM.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  8. The Following User Says Thank You to djr33 For This Useful Post:

    kuau (06-04-2008)

  9. #6
    Join Date
    Jan 2007
    Posts
    629
    Thanks
    10
    Thanked 28 Times in 28 Posts

    Default

    Quote Originally Posted by djr33 View Post
    Anything sent from clientside can be dangerous. There's nothing that guarantees it's an item from the list. (Instead of escaping the values you could just compare with an array of the possible allowed values, but that's more work, unless you need it to match one.)
    Basically, all you need to do is create a mirror form like your page, on any webserver, switch the select to a text field and type in any sort of hack you'd like for your database.

    Basically, if the user sends it, don't trust it, and escape!!
    I'm sure you can download some questionable extensions for web browsers that can manipulate things like form field types. HTML is processed client side, so it can be changed by the user. Only the result is sent back to the server. Daniel is definitely right. But, keep in mind that not everyone will be out to get you. When creating a website, you have to imagine that everyone will be searching for security holes, but in reality, almost no one will be.
    --Jas
    function GreatMinds(){ return "Think Like Jas"; }
    I'm gone for a while, but in the meantime: Try using my FTP script | Fight Bot Form Submissions

  10. The Following User Says Thank You to Jas For This Useful Post:

    kuau (06-04-2008)

  11. #7
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    642
    Thanks
    284
    Thanked 15 Times in 15 Posts

    Default

    Are you saying that for every single variable on a form I should do this before I do anything else with them?

    Code:
    $variable = mysql_real_escape_string($_POST['variable']);
    Are you supposed to put backticks around every reference to a fieldname and a table name? eg. INSERT INTO `customers` (`id`, `name`, etc

    Thanks! e
    Last edited by kuau; 06-04-2008 at 03:41 AM. Reason: forgot code tags

  12. #8
    Join Date
    Jan 2007
    Posts
    629
    Thanks
    10
    Thanked 28 Times in 28 Posts

    Default

    Quote Originally Posted by kuau View Post
    Are you saying that for every single variable on a form I should do this before I do anything else with them?

    Code:
    $variable = mysql_real_escape_string($_POST['variable']);
    That, and possibly more. It all depends on how secure you want your code to be.
    Are you supposed to put backticks around every reference to a fieldname and a table name? eg. INSERT INTO `customers` (`id`, `name`, etc

    Thanks! e
    Convention seems to be to use back ticks, but as far as I know you don't need them. I only recently started using them myself.

    On the insert statement, though, it should be:
    Code:
    INSERT INTO `table` VALUES ("value","value","value");
    Values are always in double or single quotes (usually double).

    Queries would look like this:
    Code:
    SELECT `id` FROM `table` WHERE `user` = "cool"
    --Jas
    function GreatMinds(){ return "Think Like Jas"; }
    I'm gone for a while, but in the meantime: Try using my FTP script | Fight Bot Form Submissions

  13. The Following User Says Thank You to Jas For This Useful Post:

    kuau (06-04-2008)

  14. #9
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    642
    Thanks
    284
    Thanked 15 Times in 15 Posts

    Default

    So is this wrong??:

    Code:
    $sql = " INSERT INTO users        
    		 ( `user_id`, `name`, `email`, `status`, `visdate`, `visit`, `confdate`, `pickdate`, `dropdate`, `remind`, `act`, `survey` ) 
    	       VALUES            
    		 ( '', '$name', '$emailaddress', 'I', '$today', '1', '0000-00-00', '$pickdate', '$dropdate', '0', '0', '0' ) ";
    That's the part that works! This is the part that doesn't and I can't see why and am ready to shoot myself:

    Code:
    if ($source == "cp") 
    	{
      	$sql = " UPDATE `users` 
    	          SET status = 'B', confdate = '$today', pickdate = '$pickdate', dropdate = '$dropdate'
    	          WHERE email = $emailaddress  ";
    	}					 
    	else   // if source from price check pages
    	{
    	$sql = " UPDATE `users`
    	          SET status = 'I', visit = (visit + '1'), visdate = '$today',  pickdate = '$pickdate', dropdate = '$dropdate', remind = '0', act = '0', survey = '0'
    	          WHERE email = '$emailaddress'  ";
    	}			 
    	$result = @mysql_query($sql,$connection) or die("Couldn't execute $sql query.");
    Last edited by kuau; 06-04-2008 at 03:56 AM. Reason: remove tabs

  15. #10
    Join Date
    Jan 2007
    Posts
    629
    Thanks
    10
    Thanked 28 Times in 28 Posts

    Default

    Oh! Ignore me. I didn't know that you were specifying the columns. Yes, that is the correct way to do it-- better then how I do it actually.

    Code:
    " UPDATE `users` 
    					   SET status = 'B', confdate = '$today', pickdate = '$pickdate', dropdate = '$dropdate'
    					   WHERE email = $emailaddress  ";
    You left the quotes out from around $emailaddress

    Edit: and depending on context, shouldn't this: visit = (visit + '1') be this: visit = (visit + 1) ?
    --Jas
    function GreatMinds(){ return "Think Like Jas"; }
    I'm gone for a while, but in the meantime: Try using my FTP script | Fight Bot Form Submissions

  16. The Following User Says Thank You to Jas For This Useful Post:

    kuau (06-04-2008)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •