Al Kasih
Al Kasih

Reputation: 886

Post Multiple emails in text area for each row in database

I have an textarea that will be filled with email as many as possible by member.

   <textarea name="emailRefer"> </textarea> 
   <input type="submit"/>

When the user has filled email for instance 10 emails

   <textarea name="emailRefer">[email protected], [email protected], [email protected], and so on </textarea> 
   <input type="submit"/>

And if I posted it with this code:

  $ReferalEmail = $_POST['emailRefer'];
  if ($result = $mysqli->query("INSERT INTO ReferalTable (ReferalEmail)
                                               VALUES ('$ReferalEmail');")) 

The code above will insert all emails in text area in one row to the column ReferalEmail.

But now when the Member post it, what I want it is posted as an array.

What I mean as an array is each email will be inserted to one row. If there are 10 emails in text area, it will be inserted to 10 rows for each email.

How to do this, can anyone help me? Thanks in advance.

[UPDATE]

I have tried the answer bellow with this code, but it gives me one blank row in each first row.

 $ReferalEmail = $_POST['emailRefer'];
 $emails=explode(",",$ReferalEmail);
 foreach ($emails as $email)
 {
      $result = $mysqli->query("INSERT INTO ReferalTable (ReferalEmail)
                                            VALUES ('$email');")
 }

Upvotes: 0

Views: 373

Answers (2)

Ahosan Karim Asik
Ahosan Karim Asik

Reputation: 3299

try this:

$ReferalEmail = trim(preg_replace("/\s+/","",$_POST['emailRefer']));
$emails=explode(",",$ReferalEmail);
foreach ($emails as $email)
{

     if($email!=""){
        $result = $mysqli->query("INSERT INTO ReferalTable (ReferalEmail)
                                           VALUES ('$email');")
     }
}

Upvotes: 1

ojovirtual
ojovirtual

Reputation: 3362

If you are separating the emails by coma ,, you can explode the value:

$ReferalEmail = $_POST['emailRefer'];
$emails=explode(",",$ReferalEmail);
foreach ($emails as $email)
{
     $result = $mysqli->query("INSERT INTO ReferalTable (ReferalEmail)
                                           VALUES ('$email');")
}

Of course you should sanitize the input before inserting into database.

Upvotes: 2

Related Questions