Hello,
I've got a html form with a text box and a Submit button. The text box is called text_box_1. I want to key in a few email addresses in this text box(one email address per line) & hit the Submit button. When I hit the Submit button, I want to query a Mysql database with these email addresses individually.

Okay, I know I'm not explaining myself clearly so please click on this link http://www.checker.freeproxy.ru/checker/ to understand the process I'm trying to create. In the case of the website above, the user enters a few ip addresses(1 ip address per line) & hits the Check Proxy! button. The code then processes each ip address individually. I'm trying to do the same thing.

My question is, how do I write the query to process each line from the text box individually? I know how to connect to the Mysql database & I know how to display the results so we can skip that part.

My half completed query is below:-

<?php
$text_Box_1 = $_POST['text_Box_1'];

$query="SELECT * FROM table WHERE email_field LIKE I'm lost beyond this point";
?>

Thank you for your help. Much appreciated.

    hm..
    there MUST be an easier way.. But you could do a [man]nl2br[/man] on your submitted values, and [man]explode[/man] by "<br />" to get the individual adresses in an array. Then you could query the datbase using a foreach() loop.

    Or.. you could do the nl3br, and [man]strreplace[/man] the <br> with a comma, resulting in a comma separated list, which I think you can use to:

    select * from TABLE where COLUM in (.. list here ..)

    But you should check the mysql.org website and look in the select documentation for the exact syntax, as I hardly ever use this form.

      Why convert line breaks to HTML entities, only to alter the string again without ever using the HTML entities?

      I would say something like this:

      $emails = explode("\n", $_POST['email_textbox']);
      $list = '';
      
      foreach($emails as $email)
          $list .= '\'' . mysql_real_escape_string(trim($email)) . '\', ';
      $list = rtrim($list, ', ');
      
      $query = 'SELECT `column1`, `column2` FROM `myTable` WHERE `email` IN (' . $list . ')';

        Thanks a lot for your guidance, bradgrafelman. Okay, I'll work on your example. Have a good day.

          bradgrafelman wrote:

          Why convert line breaks to HTML entities, only to alter the string again without ever using the HTML entities?

          As I said.. There probably is a more efficient way to do it 😃

          Why I did it that way: I am never sure about line endings, whether to use \n, or \r\n. I guess I am not sure how the server handles the difference between the unix and windows approach to carriage return & linefeed

            Yeah, line endings can be a pain to deal with. In this situation, it's even worse, because the data is sent from the user so you don't know what a true "line break" is. If the client is on a Mac, it's \r; if they're on Unix, it's \n, if they're on Windows, it's \r\n. My example above will only work for Unix and Windows clients.

            I suppose a universal solution in this case would be to use [man]preg_split/man instead of explode():

            $emails = preg_split('/\r\n|\r|\n/', $_POST['email_textbox']);

            Also, note that I made an error in my code above and have since corrected it. I used [man]mysql_real_escape_string/man too early (line breaks would be escaped, making explode() useless).

              Write a Reply...