Friday, 14 June 2019

How to Insert Multiple Data into Mysql in PHP using TextArea Field



If you want to learn how can we insert multiple records into Mysql Database by using Single TextArea field with PHP script. Or in second word How to Insert Multiple Data from Textarea field to Mysql table in PHP script. We have already seen single insert data into database in PHP by using different HTML field, but we have first time discuss how to insert multiple data from textarea into database in PHP. In this tutorial, you can learn two things from this single post. One is you can learn How to insert multiple rows from TextArea to Mysql using PHP and second one is how can we insert multiple data into Mysql table in PHP PDO.

For learn this topic here we have take example of inserting of multiple email addresses into mysql database by using textarea field with PHP script. There are many different event has occur in which we want to insert mass email into database. So, at that time insert email one by one will take lots of time for inserting data into database. But if you have use textarea field then you can insert multiple email address at the same time.

Now question arise how to insert multiple data into mysql by using textarea field in PHP. This will be done by if you have enter multiple email address in one email in one line and past into textarea field. Below PHP script will convert line by line email into array. After this below PHP script will make insert data query for insert multiple data by executing single mysql insert query. This query will insert multiple data in single query execution. In this script we have use to main PHP function like explode() and array_unique(). This explode() function will make textarea field value into array by using "\r\n" string delimiter and array_unique() function will remove duplicate email from array. So, this script will help you to learn PHP insert multiple records into Mysql using TextArea field. Below you can find complete source code and online demo also.






Source Code


Database



CREATE TABLE `tbl_email_list` (
  `email_list_id` int(11) NOT NULL AUTO_INCREMENT,
  `email_address` varchar(250) DEFAULT NULL,
  PRIMARY KEY (`email_list_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;




index.php



<?php

//index.php

$error = '';
$output = '';

$connect = new PDO("mysql:host=localhost;dbname=testing", "root", "");

if(isset($_POST["add"]))
{
    if(empty($_POST["email_address"]))
    {
        $error = '<label class="text-danger">Email Address List is required</label>';
    }
    else
    {
        $array = explode("\r\n", $_POST["email_address"]);

        $email_array = array_unique($array);

        $query = "
        INSERT INTO tbl_email_list 
        (email_address) 
        VALUES ('".implode("'),('", $email_array)."')
        ";

        $statement = $connect->prepare($query);

        $statement->execute();

        $error = '<label class="text-success">Data Inserted Successfully</label>';
    }
}

$query = "
SELECT * FROM tbl_email_list 
ORDER BY email_list_id DESC
";

$statement = $connect->prepare($query);

$statement->execute();

if($statement->rowCount() > 0)
{
    $result = $statement->fetchAll();
    foreach($result as $row)
    {
        $output .= '
        <tr>
            <td>'.$row["email_address"].'</td>
        </tr>
        ';
    }
}
else
{
    $output .= '
        <tr>
            <td>No Data Found</td>
        </tr>
    ';
}

?>

<html>
    <head>
        <title>Insert multiple data to mysql using single textarea in PHP</title>  
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>  
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />  
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
    </head>  
    <body>
        <div class="container">    
            <div class="row content">
                <div class="col-sm-2">
                    &nbsp;
                </div>
                <div class="col-sm-8 text-left">
                    <br />
                    <h3 align="center">Insert Multiple Data to Mysql using Textarea in PHP</h3>
                    <br />
                    <div align="center"><?php echo $error; ?></div>
                    <form method="post">
                        <div class="row">
                            <label class="col-md-3 text-right">Enter Email List</label>
                            <div class="col-md-9">
                                 <textarea name="email_address" class="form-control" rows="10"></textarea>
                            </div>
                        </div>
                        <br />
                        <div align="center">
                            <input type="submit" name="add" class="btn btn-primary" value="Add" />
                        </div>
                    </form>
                    <br />
                    <h3 align="center">Email List</h3>
                    <br />
                    <table class="table table-striped table-bordered">
                        <tr>
                            <td>Email Address</td>
                        </tr>
                        <?php
                        echo $output;
                        ?>
                    </table>
                </div>
                <div class="col-sm-2">
                    &nbsp;
                </div>
            </div>
        </div>
    </body>  
</html>

0 comments:

Post a Comment