我想使用单个文本区域将多个电子邮件插入数据库。
这是我的代码:
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>
我希望表格如下:
答案 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);
?>
示例: -
答案 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\r
。 nl2br
返回带有替换的字符串,但您需要一个数组才能在foreach
循环中使用它。
作为补充说明,每次添加此指令时,都会遍历数组:
<script>alert("Inserted into the database");</script>
如果您要浏览10封电子邮件,系统会连续十次收到提醒。
此外,不推荐使用mysql_query
。是时候学习PDO或MySQLi,这将使您能够使用占位符而不是不安全的直接将$_POST
数据注入SQL查询。 Placehiolders非常容易学习,它们可以帮助您构建更可靠的应用程序。