Jump to content

Insert multiple data into mysql with explode?


IreneLing

Recommended Posts

Hi all .

In my scripts , there is a textarea that allow user to enter multiple phone number , message and date .

If there are 3 phone numbers in textarea , how can I insert 3 of them into sql with the same data of message and date?

Such as :

 

phone number : 0102255888,0235544777,0896655444

message:hello all

date:10/10/2011

 

and when it's insert into table , it will become:

 

1. 0102255888 | hello all | 10/10/2011

2. 0235544777 | hello all | 10/10/2011

3. 0896655444 | hello all | 10/10/2011

 

Here is the code I tried , but it will just save the last phone number .

 

           $cellphonenumber = explode(',', $_POST['cellphonenumber']);
           $message = $_POST['inputtext'];
           $date = $_POST['datetime'];



foreach($cellphonenumber as $value)
{

$sql="INSERT INTO esp( Recipient , Message , Date)
VALUES
('$value','$message','$date')";

}


        mysql_query($sql) or die ("Error: ".mysql_error());

        echo "Database updated.";

 

Any hints or advices ?

 

Thanks for every reply .

 

You would have to move the mysql_query() into the foreach loop as well. That's why it's only executing the last query, because it's only executing the one query(the last in the loop).

 

Thank you so much MasterACE14 , it works now , really thanks to your help .

 

Thanks again and have a nice day .

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.