SitePoint Sponsor

User Tag List

Results 1 to 16 of 16

Hybrid View

  1. #1
    SitePoint Zealot WebFreakz's Avatar
    Join Date
    Dec 2006
    Posts
    126
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy 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>&nbsp;</td>
            <td><input type=submit name=s1 value=Add! class=sub></td>
    </tr>
    </table>
    
    </form>

  2. #2
    SitePoint Evangelist ClickHeRe's Avatar
    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
      
    }
    and use mysql_real_escape_string( ) instead of addslashes()
    David

  3. #3
    SitePoint Zealot WebFreakz's Avatar
    Join Date
    Dec 2006
    Posts
    126
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ClickHeRe View Post
    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
      
    }
    and use mysql_real_escape_string( ) instead of addslashes()
    Ok which variable would i use to specify the email when entering into the db? I am confused. $v??

  4. #4
    SitePoint Wizard
    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.

  5. #5
    SitePoint Zealot WebFreakz's Avatar
    Join Date
    Dec 2006
    Posts
    126
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by elduderino View Post
    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.
    like this $message = $fielddata; ???

  6. #6
    SitePoint Evangelist ClickHeRe's Avatar
    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

  7. #7
    SitePoint Zealot WebFreakz's Avatar
    Join Date
    Dec 2006
    Posts
    126
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ClickHeRe View Post
    $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...
    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>&nbsp;</td>
            <td><input type=submit name=s1 value=Add! class=sub></td>
    </tr>
    </table>
    
    </form>

  8. #8
    SitePoint Wizard
    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];

    put this at the start. It assigns the posted string to the $message variable

  9. #9
    SitePoint Zealot WebFreakz's Avatar
    Join Date
    Dec 2006
    Posts
    126
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  10. #10
    SitePoint Wizard
    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')"
    you dont want the $_POST bit in there at all

    and you'll need the bit of code i gave you in the post before at the top of your script

  11. #11
    SitePoint Zealot WebFreakz's Avatar
    Join Date
    Dec 2006
    Posts
    126
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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 get an echo of Array

    I am confused why I can not pass the variables onto $array. Can anyone help me?

  12. #12
    SitePoint Evangelist ClickHeRe's Avatar
    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;
    use print_r() to print the contents of an array else you only get Array which is the object name.

    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

  13. #13
    SitePoint Zealot WebFreakz's Avatar
    Join Date
    Dec 2006
    Posts
    126
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ClickHeRe View Post
    Code:
    if(isset($_POST[s1])) { 
    
     
    $fielddata = $_POST['s1'];
    
    $array = explode( ',', $fielddata);
    print_r( $array );
    exit;
    use print_r() to print the contents of an array else you only get Array which is the object name.

    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.
    I got this code printed out???
    Array ( [0] => Add! )

  14. #14
    SitePoint Zealot WebFreakz's Avatar
    Join Date
    Dec 2006
    Posts
    126
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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>&nbsp;</td>
            <td><input type=submit name=s1 value=Add! class=sub></td>
    </tr>
    </table>
    
    </form>
    Last edited by WebFreakz; Feb 22, 2007 at 13:06. Reason: posted the full page of code with my comments I need help

  15. #15
    SitePoint Wizard
    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>'

  16. #16
    SitePoint Zealot WebFreakz's Avatar
    Join Date
    Dec 2006
    Posts
    126
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Same thing happens. Can anyone help me??


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
  •