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

Thread: POST variables in mysql query

  1. #1
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default POST variables in mysql query

    I am inserting POST, GET, and SESSION variables into mysql queries like this:

    Code:
    $q2 = "UPDATE `contest_answers`
    SET `answer` = '$q_2_ans'
    WHERE `ans_id` = {$_POST['ans_2_id']} LIMIT 1 "
    Is this the "best practice" or is there a better way to do it?

    (validation has been done on the POST variable)

    Thanks!

    Jason

  2. #2
    Join Date
    Feb 2008
    Location
    Cebu City Philippines
    Posts
    1,160
    Thanks
    17
    Thanked 277 Times in 275 Posts

    Default

    Ensure you've escaped special characters already:
    PHP Code:
    mysql_real_escape_string($_POST['ans_2_id']) 
    Learn how to code at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph!

  3. #3
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default

    Yes, good advice. I had escaped the special characters.

    Use of the curly brackets in the query was my main concern.

    Is there a "best practice" for putting POST, GET, or SESSION variables into a mysql query?

    Thanks!

    J

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Personally, I like sprintf():
    Code:
    mysql_query(sprintf('UPDATE contest_answers SET answer = '%s' where ans_id = %d',
                       $q_2_ans,
                       $_POST['ans_2_id']))
      or die(mysql_error());
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  5. #5
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Personally, I like sprintf():
    Code:
    mysql_query(sprintf('UPDATE contest_answers SET answer = \'%s\' where ans_id = %d',
                       $q_2_ans,
                       $_POST['ans_2_id']))
      or die(mysql_error());
    Last edited by Twey; 10-24-2008 at 11:10 AM. Reason: Needs escaping.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  6. #6
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default

    I had not thought of that. What is the advantage to using sprintf?

    Thanks Twey.

    Jason

  7. #7
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Well for a start you've got a little validation for free — there's no way that %d can be a string, for example. Conjointly, it increases the readability of the query by showing the query separately, without all the clumsy parameter-preparing code getting in the way.

    Of course, if you have them available, PDO prepared statements are preferable by far, since they take care of all escaping, delimiting, and other assorted nonsense for you, as well as providing you with many devices by use of which you can greatly neaten your code.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  8. #8
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default

    Cool! Thanks a lot Twey. PDO looks great. You know of any good tutorials?

    J

  9. #9
    Join Date
    Jun 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Along the same line, I am trying to create a function to allow me to update a database when a user selects a quantity in a form.

    Here's my function so far:

    PHP Code:
    <?php

    $QTY 
    $_POST['qty'];
    Print 
    $QTY;
    echo 
    '<P>';  // Test to see if qty is being passed (It is)

    function updateDbase () {

    // Connects to your Database
    $db DB::connect('mysql://username:password@localhost/my_inventory');
    if (
    DB::isError($db)) { die ("Can't connect: " $db->getMessage()); }

    // Set up automatic error handling
    $db->setErrorHandling(PEAR_ERROR_DIE);

    $db->query("UPDATE inventory SET InStock = InStock - ( ) where inventory . PartNumber =123456");

    mysql_close();

     }
    ?>
    My problem is getting the variable into the () in the query. Apparently the code needs to be a little more complex than this? When I plug a number into the (), the database updates correctly so I know everything is working up to that point.

    Thanks for any help. I am learning PHP and am not a master of MySql yet. ;-)
    Last edited by Snookerman; 06-03-2009 at 06:44 PM. Reason: [php] tags

  10. #10
    Join Date
    Feb 2008
    Location
    Cebu City Philippines
    Posts
    1,160
    Thanks
    17
    Thanked 277 Times in 275 Posts

    Default

    It doesn't appear to be "MySql" query for me.

    I'm uncertain if I totally understand you, but try:
    Code:
    $db->query("UPDATE inventory SET InStock = InStock - ( ) where inventory . PartNumber =".$QTY."\"");
    Learn how to code at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph!

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
  •