Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-18-2010, 08:57 AM   PM User | #1
fail
Regular Coder

 
Join Date: Dec 2009
Location: Hong Kong
Posts: 118
Thanks: 8
Thanked 0 Times in 0 Posts
fail is an unknown quantity at this point
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..
fail is offline   Reply With Quote
Old 01-18-2010, 09:00 AM   PM User | #2
[Paul Ferrie ]
Regular Coder

 
Join Date: Nov 2009
Location: Scotland / Glasgow
Posts: 184
Thanks: 1
Thanked 19 Times in 19 Posts
[Paul Ferrie ] can only hope to improve
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
[Paul Ferrie ] is offline   Reply With Quote
Old 01-18-2010, 09:02 AM   PM User | #3
abduraooft
Supreme Master coder!

 
abduraooft's Avatar
 
Join Date: Mar 2007
Location: N/A
Posts: 14,678
Thanks: 158
Thanked 2,182 Times in 2,169 Posts
abduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really nice
Quote:
for($i = 0; $i < count($words); $i++)
{ echo "'$$words[$i]', "; }
echo ")";
PHP Code:
echo '('.implode(',',$words).')'
__________________
Quote:
The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)
abduraooft is offline   Reply With Quote
Users who have thanked abduraooft for this post:
fail (01-18-2010)
Old 01-18-2010, 09:34 AM   PM User | #4
fail
Regular Coder

 
Join Date: Dec 2009
Location: Hong Kong
Posts: 118
Thanks: 8
Thanked 0 Times in 0 Posts
fail is an unknown quantity at this point
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!
fail is offline   Reply With Quote
Old 01-18-2010, 09:44 AM   PM User | #5
abduraooft
Supreme Master coder!

 
abduraooft's Avatar
 
Join Date: Mar 2007
Location: N/A
Posts: 14,678
Thanks: 158
Thanked 2,182 Times in 2,169 Posts
abduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really nice
Quote:
<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
__________________
Quote:
The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)
abduraooft is offline   Reply With Quote
Old 01-18-2010, 09:54 AM   PM User | #6
fail
Regular Coder

 
Join Date: Dec 2009
Location: Hong Kong
Posts: 118
Thanks: 8
Thanked 0 Times in 0 Posts
fail is an unknown quantity at this point
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?
fail is offline   Reply With Quote
Old 01-18-2010, 09:58 AM   PM User | #7
abduraooft
Supreme Master coder!

 
abduraooft's Avatar
 
Join Date: Mar 2007
Location: N/A
Posts: 14,678
Thanks: 158
Thanked 2,182 Times in 2,169 Posts
abduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really nice
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
__________________
Quote:
The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)
abduraooft is offline   Reply With Quote
Old 01-18-2010, 10:10 AM   PM User | #8
fail
Regular Coder

 
Join Date: Dec 2009
Location: Hong Kong
Posts: 118
Thanks: 8
Thanked 0 Times in 0 Posts
fail is an unknown quantity at this point
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']))))); 
fail is offline   Reply With Quote
Old 01-18-2010, 01:02 PM   PM User | #9
JAY6390
Regular Coder

 
Join Date: Dec 2009
Location: UK
Posts: 495
Thanks: 0
Thanked 58 Times in 58 Posts
JAY6390 is on a distinguished road
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 :)
JAY6390 is offline   Reply With Quote
Old 01-18-2010, 01:08 PM   PM User | #10
kbluhm
Senior Coder

 
kbluhm's Avatar
 
Join Date: Apr 2007
Location: Philadelphia, PA, USA
Posts: 1,502
Thanks: 2
Thanked 258 Times in 254 Posts
kbluhm will become famous soon enough
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 ); 
__________________
ZCE
kbluhm is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:31 AM.


Advertisement
Log in to turn off these ads.