php - Split comma-separated input box values and then store in database


I have a text input box where you can type tags seperated by commas. I want to store these tags separately in a table but I have no idea how to do that. I know I have to seperate the tags after they are posted so I now have this

$array = explode(',', $_POST['tag']);

Is this a good start and how do I have to go on after that?




Creating array from your input field as in your code

$array = explode(',', $_POST['tag']);

Create a mysql multi insert query using foreach loop simillar to this:

    $id=mysqli_insert_id($con);//get your project id here
    foreach($array as $tag_name){
       //modify below to add $id along with $tag_name
       $sql.="('{$id}','{$tag_name}'),"; // you need to remove last comma else it will throw mysql error  

    //rtrim to remove last ',' from string. 

    $sql="INSERT INTO tags_tbl (project_id,tag) VALUES {$sql};"`


now usemysqli_query($con,$sql) to insert values in database

People are also looking for solutions to the problem: php - Add custom title to excel columns using PHPEXCEL


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.