SitePoint Sponsor |
|
User Tag List
Results 1 to 16 of 16
-
Feb 22, 2007, 05:59 #1
Seperating info from a text field and inserting into db using foreach????
Hi I am trying to figure out a way to take email address out of a text field box and insert them as new records into a database.
I created a page with a text field and a add button.
As an example, I want to be able to paste more than one email address into the text field box. The email addresses will be seperated by commas like this (user123@blah.com, user234@blah.com, user345@blah.com, user456@blah.com and so on.) When I press the add button I want to be able to extract the emails and add them into the database as thier own entries as new email adresses. I was trying to configure a script using foreach but I can not figure out exactly how to do this.
I do not know how to extract each email individually from the text field and how to list them as a variable and enter them into the db...
Can anyone help me with this PLEASE. I am lost.
This is what I had come up with so far.
Code:if(isset($_POST[s1])) { foreach (??? ????) { $q1 = "Insert into mail set email = '" . xss_clean(addslashes($_POST['email'])) . "'"; mysql_query($q1); if(mysql_error()) { echo mysql_error(); } else { echo "<br><center><font face=verdana size=2 color=red><b>You have sucessfully added the subscribers!"; exit(); } } } //get the subscribers number $q1 = "select count(*) from mail where news = 'y' "; $r1 = mysql_query($q1) or die(mysql_error()); $a1 = mysql_fetch_array($r1); ?> <script> function CheckNews() { if(document.f1.message.value=="") { window.alert('Enter the new emails!'); document.f1.message.focus(); return false; } } </script> <form method=post name=f1 onsubmit="return CheckNews();"> <table align=center width=350> <caption align=center class=BlackLink>You have <?=$a1[0]?> subscribers at your mailing list.<br> Would you like to add more subscribers?</caption> <tr> <td colspan=2 class=TableHead align=center>Add more subscribers!</td> </tr> <tr> <td valign=top>Add Emails to the DB:</td> <td><textarea name=message rows=10 cols=32></textarea></td> </tr> <tr> <td> </td> <td><input type=submit name=s1 value=Add! class=sub></td> </tr> </table> </form>
-
Feb 22, 2007, 06:07 #2
- Join Date
- Mar 2005
- Location
- Ottawa, Canada
- Posts
- 580
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Code:$array = explode( ',', $fielddata); foreach ( $array as $v ) { // remove the whitespace $v2 = trim( $v ); // You can also add other checks, for example using a regex // you could check if the address is in the correct format // insert into DB here }
David
-
Feb 22, 2007, 06:23 #3
- Join Date
- Oct 2005
- Location
- London
- Posts
- 1,678
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
hi,
use the explode function (see first line in clickhere's code) it turns a sring in to an array. You first specify which character you want to split the string on ( a comma in this case) then which string you want to explode.
You'll just need to get your string in to the $fielddata variable then you'll be away with the code above.
-
Feb 22, 2007, 06:25 #4
-
Feb 22, 2007, 06:27 #5
-
Feb 22, 2007, 06:28 #6
- Join Date
- Mar 2005
- Location
- Ottawa, Canada
- Posts
- 580
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
$v2 as it's the checked and stripped version of the address
If you add more checking in between $v2 and the actual adding, make sure you use the correct one. Simply echo $v2; will let you see the value before actually doing the insert.
$fielddata = isset( $_POST['myfield'] ) ? $_POST['myfield'] : '';
etc...David
-
Feb 22, 2007, 06:37 #7
Ok when i try it I inser a blank space into the db and it only enters doesnt enter anything after the comma. I am confused I might have put it together wrong i posted it below...
Code:$message = $fielddata; if(isset($_POST[s1])) $array = explode( ',', $fielddata); foreach ( $array as $v ) { // remove the whitespace $v2 = trim( $v ); // You can also add other checks, for example using a regex // you could check if the address is in the correct format // insert into DB here $q1 = "Insert into mail set email = '" . xss_clean(addslashes($_POST['$v2'])) . "'"; mysql_query($q1); if(mysql_error()) { echo mysql_error(); } else { echo "<br><center><font face=verdana size=2 color=red><b>You have sucessfully added the subscribers!"; exit(); } } //get the subscribers number $q1 = "select count(*) from mail where news = 'y' "; $r1 = mysql_query($q1) or die(mysql_error()); $a1 = mysql_fetch_array($r1); ?> <script> function CheckNews() { if(document.f1.message.value=="") { window.alert('Enter the new emails!'); document.f1.message.focus(); return false; } } </script> <form method=post name=f1 onsubmit="return CheckNews();"> <table align=center width=350> <caption align=center class=BlackLink>You have <?=$a1[0]?> subscribers at your mailing list.<br> Would you like to add more subscribers?</caption> <tr> <td colspan=2 class=TableHead align=center>Add more subscribers!</td> </tr> <tr> <td valign=top>Add Emails to the DB:</td> <td><textarea name=message rows=10 cols=32></textarea></td> </tr> <tr> <td> </td> <td><input type=submit name=s1 value=Add! class=sub></td> </tr> </table> </form>
-
Feb 22, 2007, 06:41 #8
- Join Date
- Oct 2005
- Location
- London
- Posts
- 1,678
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
PHP Code:if(isset($_POST[s1])) {
$message = $_POST[s1];
}
-
Feb 22, 2007, 07:00 #9
I can't seem to get it to work. I am lost. Please someone look at what i posted above maybe you can tell me what I did wrong.
-
Feb 22, 2007, 07:11 #10
- Join Date
- Oct 2005
- Location
- London
- Posts
- 1,678
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
right well the whole query should be like this:
PHP Code:"INSERT INTO mail (email) VALUES ('$v2')";
and you'll need the bit of code i gave you in the post before at the top of your script
-
Feb 22, 2007, 07:20 #11
Ok I see something wrong. I do pass the variables from the text field to $fielddata, but i cannot seem to pass them from fielddatta to $array.
Code:if(isset($_POST[s1])) { $fielddata = $_POST['message']; $array = explode( ',', $fielddata); echo $array; exit;
I am confused why I can not pass the variables onto $array. Can anyone help me?
-
Feb 22, 2007, 07:23 #12
- Join Date
- Mar 2005
- Location
- Ottawa, Canada
- Posts
- 580
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Code:if(isset($_POST[s1])) { $fielddata = $_POST['s1']; $array = explode( ',', $fielddata); prinrt_r( $array ); exit;
Also, use echo statements to keep track of your values before you insert them into the DB. (debug purpose, when it's all clean and works, you remove or comment them out using // or /* */ )
It's a matter of following the variable trail which you seem to have difficulty with at the moment.David
-
Feb 22, 2007, 07:28 #13
-
Feb 22, 2007, 07:41 #14
Still stuck if someone can please look this code over and read my comments maybe you will see what i don't. Any help is and will be appreciated. Thank you.
Code:<? require_once("../cc.php"); require_once("access.php"); include_once("LeftStyles.php"); if(isset($_POST['s1'])) { $fielddata = $_POST['message']; //echo $fileddata; shows the proper variables! $array = explode( ',', $fielddata); //print_r($array); shoes the proper variables! foreach ($array AS $v) { echo $v//Only shows the first varible all others are lost!! STUCK HERE. exit; // remove the whitespace $v2 = trim( $v ); // insert into DB here $q1 = "INSERT INTO mail (email) VALUES ('$v2')"; mysql_query($q1); if(mysql_error()) { echo mysql_error(); } else { echo "<br><center><font face=verdana size=2 color=red><b>You have sucessfully added the subscribers!"; exit(); } } } //get the subscribers number $q1 = "select count(*) from mail where news = 'y' "; $r1 = mysql_query($q1) or die(mysql_error()); $a1 = mysql_fetch_array($r1); if($a1[0] == '0') { echo "<br><center><font face=verdana size=2 color=red><b>You have no subscribers in your mailing list!"; exit(); } ?> <script> function CheckNews() { if(document.f1.message.value=="") { window.alert('Enter the new emails!'); document.f1.message.focus(); return false; } } </script> <form method=post name=f1 onsubmit="return CheckNews();"> <table align=center width=350> <caption align=center class=BlackLink>You have <?=$a1[0]?> subscribers at your mailing list.<br> Would you like to add more subscribers?</caption> <tr> <td colspan=2 class=TableHead align=center>Add more subscribers!</td> </tr> <tr> <td valign=top>Add Emails to the DB:</td> <td><textarea name=message rows=10 cols=32></textarea></td> </tr> <tr> <td> </td> <td><input type=submit name=s1 value=Add! class=sub></td> </tr> </table> </form>
Last edited by WebFreakz; Feb 22, 2007 at 14:06. Reason: posted the full page of code with my comments I need help
-
Feb 22, 2007, 10:28 #15
- Join Date
- Oct 2005
- Location
- London
- Posts
- 1,678
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
hi,
try and echo out your $v variable in your for each loop
PHP Code:echo '<p>' .$v. '</p>';
-
Feb 22, 2007, 14:04 #16
Same thing happens. Can anyone help me??
Bookmarks