Thursday, February 6, 2020

How to Insert Multiple Values From Multiple TextArea Field into Mysql in PHP

How to Insert Multiple Values From Multiple TextArea Field into Mysql in PHP

If you want to learn how we can insert multiple records into the MySQL database using the Single TextArea field with PHP script. Or second, how to insert multiple data from the textarea field into the mysql table in a PHP script. We have already noticed that in PHP, unique data is inserted into the database using different HTML fields. However, it was explained for the first time how PHP can insert multiple data from the text area into the database. In this tutorial you can learn two things from this post. On the one hand, you can learn how to insert multiple rows of TextArea into MySQL using PHP, and on the other hand, you can use PHP PDO to insert multiple data into the MySQL table.

To learn this topic here, we have an example of inserting multiple email addresses into the MySQL database using the text box with PHP script. There were many different events when we wanted to insert bulk emails into the database. Therefore, it takes a long time at this point to insert one email after the other to insert data into the database. However, if you used the text box, you can enter multiple email addresses at the same time.

Now the question arises how multiple data can be inserted with the text field in PHP in mysql. This happens when you have entered several email addresses in one line and then in the text field. The following PHP script converts the email line by line into an array. The PHP script then runs a data insert query to insert multiple data by executing a single query to insert mysql. This query inserts multiple data into the execution of a single query. In this script, we use the main PHP function as explode () and array_unique (). This exploit () function converts the value of the text area field to an array using the string delimiter "\ r \ n", and the array_unique () function removes the duplicate email from the array. For this reason, this script will help you learn PHP to insert multiple records into MySQL using the TextArea field. Below you will find the complete source code and the online demo.


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>How to Insert Multiple Values From Multiple TextArea Field into Mysql 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">How to Insert Multiple Values From Multiple TextArea Field into Mysql 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

Please don't enter any spam link in the comment box.