使用单个文本区域将多封电子邮件插入 mysql [英] Insert multiple email to mysql using single textarea
本文介绍了使用单个文本区域将多封电子邮件插入 mysql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用单个文本区域将多封电子邮件插入数据库.
I want to insert multiple emails into the database using single text area.
这是我的代码:
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>
我希望表格是这样的:
推荐答案
使用explode
通过"\r\n"
不要使用单引号,您需要使用双引号将字符串分解为 \r\n
我刚知道.
don't use single quotes you need to use double quotes to explode the string by \r\n
I just got to know that.
<?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>
这篇关于使用单个文本区域将多封电子邮件插入 mysql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文