|
 |
Enjoy an ad free experience by logging in. Not a member yet? Register.
|
|
|
|
01-18-2010, 08:57 AM
|
PM User |
#1
|
|
Regular Coder
Join Date: Dec 2009
Location: Hong Kong
Posts: 118
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..
|
|
|
|
01-18-2010, 09:00 AM
|
PM User |
#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
|
|
|
01-18-2010, 09:02 AM
|
PM User |
#3
|
|
Supreme Master coder!
Join Date: Mar 2007
Location: N/A
Posts: 14,678
Thanks: 158
Thanked 2,182 Times in 2,169 Posts
|
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)
|
|
|
|
|
Users who have thanked abduraooft for this post:
|
|
01-18-2010, 09:34 AM
|
PM User |
#4
|
|
Regular Coder
Join Date: Dec 2009
Location: Hong Kong
Posts: 118
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!
|
|
|
01-18-2010, 09:54 AM
|
PM User |
#6
|
|
Regular Coder
Join Date: Dec 2009
Location: Hong Kong
Posts: 118
Thanks: 8
Thanked 0 Times in 0 Posts
|
Quote:
Originally Posted by abduraooft
|
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?
|
|
|
01-18-2010, 09:58 AM
|
PM User |
#7
|
|
Supreme Master coder!
Join Date: Mar 2007
Location: N/A
Posts: 14,678
Thanks: 158
Thanked 2,182 Times in 2,169 Posts
|
Quote:
Originally Posted by fail
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)
|
|
|
|
01-18-2010, 10:10 AM
|
PM User |
#8
|
|
Regular Coder
Join Date: Dec 2009
Location: Hong Kong
Posts: 118
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'])))));
|
|
|
01-18-2010, 01:02 PM
|
PM User |
#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
|
|
|
01-18-2010, 01:08 PM
|
PM User |
#10
|
|
Senior Coder
Join Date: Apr 2007
Location: Philadelphia, PA, USA
Posts: 1,502
Thanks: 2
Thanked 258 Times in 254 Posts
|
Quote:
Originally Posted by fail
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'], -1, PREG_SPLIT_NO_EMPTY );
|
|
|
 |
Jump To Top of Thread
| Thread Tools |
|
|
| 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
HTML code is Off
|
|
|
All times are GMT +1. The time now is 03:31 AM.
|
Advertisement Log in to turn off these ads. |
|
|
|
|
|
|
|
|
|
|