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 .

 

Link to comment
Share on other sites

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 .

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.