使用单个textarea将多个电子邮件插入mysql

时间:2018-01-11 04:46:03

标签: php mysql

我想使用单个文本区域将多个电子邮件插入数据库。

这是我的代码:

PHP

error_reporting(E_ERROR | E_WARNING | E_PARSE);

$dbhost = "localhost";
$dbname = "emails_test";
$dbuser = "root";
$dbpass = "";

$conn = mysql_connect($dbhost,$dbuser,$dbpass);
if (!$conn) { die('Could not connect: ' . mysql_error()); }
mysql_select_db($dbname, $conn);

if(isset($_POST['submit'])) {
    //$email = nl2br($_POST['email']);
    $email = explode("\r\n", $_POST['email']);

    foreach($email as $emails) {
        $query = mysql_query("INSERT INTO emails (email) VALUES ('$emails')");
        if($query) { echo "Inserted into the database"; } else { echo"Fail, please try again"; }
    }
}

HTML

<body>
    <form name="form1" method="POST">
        <textarea rows="5" name="email" cols="50" ></textarea><br>
        <input type="submit" name="submit" value="submit">
    </form>
</body>

我希望表格如下:[Pic Table in MySQL]

4 个答案:

答案 0 :(得分:2)

使用explode"\r\n"

将字符串输入数组 不要使用单引号,你需要使用双引号来按\r\n分解字符串我才知道这一点。

<?php
if(isset($_POST['submit'])) {
    //$email = nl2br($_POST['email']);
    $email = explode("\r\n", $_POST['email']);

    foreach($email as $emails) {
        $query = mysql_query("INSERT INTO emails (email) VALUES ('$emails')");
        if($query) {
            echo "Inserted into the database";
        } else {
            echo "Fail, please try again";
        }
    }
}
?>
<body>
    <form name="form1" method="POST">
        <textarea rows="5" name="email" cols="50" ></textarea>
        <br />
        <input type="submit" name="submit" value="submit">
    </form>
</body>

答案 1 :(得分:1)

您可以尝试这种方式

<body>
    <form name="form1" method="POST">
        <textarea rows="5" name="email" cols="50" ></textarea>
        <br />
        <input type="submit" name="submit" value="submit">
    </form>
</body>


Note :- use "Enter" to put all email (one by one)

插入数据库

  <?php

$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);

// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

if(isset($_POST["submit"])) {

        $str = $_POST["email"];
        $email  = preg_split('/\r\n|\n|\r/', $str);


    foreach($email as $emails) {

        $query = mysqli_query($conn, "INSERT INTO emails (email) VALUES ('".$emails."')");

        if($query) { ?>

            <script>alert("Inserted into the database");</script>

            <?php } else { ?>

                <script>alert("Fail, please try again");</script>
    <?php } } }

    mysqli_close($conn);

    ?> 

示例: -

enter image description here

答案 2 :(得分:0)

在您的代码中执行此操作...

<强> HTML

<body>
    <form name="form1" method="POST">
        <textarea rows="5" name="email" cols="50" ></textarea><br>
        <small>Enter email IDs separated by comma(,)</small>
        <input type="submit" name="submit" value="submit"><br>
    </form>
</body>

<强> PHP

if(isset($_POST['submit'])) {
    $email = explode(',',$_POST['email']);//convert into $email array
    $value = '';
    foreach($email as $r) $value .= '("'.$r.'"),';//concatenate email for insert
    $value = rtrim($value,',').';' //remove the last comma ;
    $query = mysqli_query('INSERT INTO emails (email) VALUES '.$value);// insert all email in database in single query in different rows
    if($query) echo 'Inserted into the database';
    else echo 'Fail, please try again";
}

你会得到你的输出......

答案 3 :(得分:0)

如果您只是替换此

,您的代码将按预期工作
$email = nl2br($_POST['email']);

用这个

$email = preg_split('/\r\n|\n|\r/', $_POST['email']);

问题是,您刚刚用\n\r替换了所有<br>\n\rnl2br返回带有替换的字符串,但您需要一个数组才能在foreach循环中使用它。

作为补充说明,每次添加此指令时,都会遍历数组:

<script>alert("Inserted into the database");</script>

如果您要浏览10封电子邮件,系统会连续十次收到提醒。

此外,不推荐使用mysql_query。是时候学习PDO或MySQLi,这将使您能够使用占位符而不是不安全的直接将$_POST数据注入SQL查询。 Placehiolders非常容易学习,它们可以帮助您构建更可靠的应用程序。