Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    Regular Coder
    Join Date
    Dec 2009
    Location
    Hong Kong
    Posts
    119
    Thanks
    8
    Thanked 0 Times in 0 Posts

    How to prevent the last comma in a loop?

    Lazy me wrote a small page that, when I input "name address phone", will output

    $name = $_POST['name'];
    $address = $_POST['address'];
    $phone = $_POST['phone'];

    (name, address, phone, )

    VALUES ('$name', '$address', '$phone', )

    How do I prevent the last comma, which will obviously cause an error later in mySQL?

    PHP Code:
    <form id="FormName" action="<?php echo $PHP_SELF;?>" method="post" name="FormName">

    <textarea name="note" rows="10" cols="60"></textarea>

    <input type="submit" name="submitButtonName" value="Go!">
    </form><P>

    <?php

    $words 
    explode(" "$_POST['note']);

    for(
    $i 0$i count($words); $i++)
            {    echo 
    "$$words[$i] = $_POST['$words[$i]']; <br />";    }

            
    echo 
    "<P>(";
    for(
    $i 0$i count($words); $i++)
            {    echo 
    "$words[$i], ";        }
    echo 
    ")";


    echo 
    "<P>VALUES (";        
    for(
    $i 0$i count($words); $i++)
            {    echo 
    "'$$words[$i]', ";        }
    echo 
    ")";

    ?>
    Last edited by fail; 01-18-2010 at 10:02 AM.

  • #2
    Regular Coder
    Join Date
    Nov 2009
    Location
    Scotland / Glasgow
    Posts
    184
    Thanks
    1
    Thanked 19 Times in 19 Posts
    PHP Code:
    if($i != count($words)){
     echo 
    "$words[$i], ";
    }else{
     echo 
    "$words[$i]";

    Hope it helps
    .:To me AS is like LEGO, Only for the big Kids :.
    - Site - Blog - Glasgow Flasher

  • #3
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,862
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    for($i = 0; $i < count($words); $i++)
    { echo "'$$words[$i]', "; }
    echo ")";
    PHP Code:
    echo '('.implode(',',$words).')'
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • Users who have thanked abduraooft for this post:

    fail (01-18-2010)

  • #4
    Regular Coder
    Join Date
    Dec 2009
    Location
    Hong Kong
    Posts
    119
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Thanks a lot! I change now to:

    PHP Code:
    <form id="FormName" action="<?php echo $PHP_SELF;?>" method="post" name="FormName">

    <textarea name="note" rows="10" cols="60"></textarea>

    <input type="submit" name="submitButtonName" value="Go!">
    </form><P>

    <?php

    $words 
    explode(" "$_POST['note']);

    for(
    $i 0$i count($words); $i++)
            {    echo 
    "$$words[$i] = $_POST['$words[$i]']; <br />";    }
            
    echo 
    '<P>('.implode(', ',$words).')'."<P>";  

    echo 
    'VALUES ('$'.implode('','$',$words).'')';  


    ?>
    Note: I use HTML &#.. special character for $ (36) and ' (39), otherwise you may get errors. However, the save here back to $ and '

    Those little php tricks are really great for learning!

  • #5
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,862
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    <form id="FormName" action="<?php echo $PHP_SELF;?>" method="post" name="FormName">
    You shouldn't depend upon the ON status of register_globals. See http://php.net/manual/en/security.globals.php and http://www.php.net/manual/en/faq.usi...gister-globals

    And your form is susceptible to XSS attacks, see http://seancoates.com/xss-woes
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #6
    Regular Coder
    Join Date
    Dec 2009
    Location
    Hong Kong
    Posts
    119
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by abduraooft View Post
    You shouldn't depend upon the ON status of register_globals. See http://php.net/manual/en/security.globals.php and http://www.php.net/manual/en/faq.usi...gister-globals

    And your form is susceptible to XSS attacks, see http://seancoates.com/xss-woes
    That may be true. But for such an attack you need to connect to my office network. That page ain't on the www.

    That's why all I write is basically unsecured, coz I don't need it. However, I am aware of risks and would implement it if I would go online.

    PS: what happened to the "Resolved" feature here?

  • #7
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,862
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    Quote Originally Posted by fail View Post
    PS: what happened to the "Resolved" feature here?
    I think it's still there, check the last one at http://www.codingforums.com/postguide.htm
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #8
    Regular Coder
    Join Date
    Dec 2009
    Location
    Hong Kong
    Posts
    119
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Oh, thanks, I overlooked that....

    BTW, to get rid of all white space but one I did this:

    PHP Code:
    $words explode(" ", (preg_replace('/\s\s+/'' ', (trim($_POST['note']))))); 

  • #9
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    I don't understand why people use PHP_SELF. It's less work to NOT add it and it's the secure way and it will still work the same. You should change
    PHP Code:
    <form id="FormName" action="<?php echo $PHP_SELF;?>" method="post" name="FormName">                      
    to
    Code:
    <form id="FormName" action="" method="post" name="FormName">
    A blank action will work in the same way as putting the current URL in the action
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • #10
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    Quote Originally Posted by fail View Post
    Oh, thanks, I overlooked that....

    BTW, to get rid of all white space but one I did this:

    PHP Code:
    $words explode(" ", (preg_replace('/\s\s+/'' ', (trim($_POST['note']))))); 
    PHP Code:
    $words preg_split'/\s/'$_POST['note'], -1PREG_SPLIT_NO_EMPTY ); 


  •  

    Posting Permissions

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