Home » Php » mysql – PHP implode to insert into two column of SQL

mysql – PHP implode to insert into two column of SQL

Posted by: admin February 25, 2020 Leave a comment

Questions:

this is my first question on stackoverflow :).

i have two text field email_address & email_number.

i want to insert into database tbl_email_list each row equal to each line in textfield email_address & email_number i have below code its not working please help me.
i have tried many things but nothing working, Please help me if its possible to insert two textfeild each line into each row of the database,

<?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);

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

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

        $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>';
}

?>

I have tried only for one textfeild its working,
This code is working for me for one column only:

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>';
    }
}

HTML CODE:

<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>
                                 <textarea name="email_number" 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>
How to&Answers: