[英]How to insert multiple row from textareas in MySQL
我無法從兩個 textarea(每行)插入到 mysql,
首先這是一個 add.html
<form id="form1" name="form1" method="post" action="func/insert.php">
<table>
<tr>
<td>text 1</td>
<td>:</td>
<td><textarea class="custom" rows="10" cols="80" name="text1" id="text1" required></textarea>
</td>
</tr>
<tr>
<td>text 2</td>
<td>:</td>
<td><textarea class="custom" rows="10" cols="80" name="text2" id="text2" required>
</textarea></td>
</tr>
</table>
</from>
然后插入.php
<?php
include"connection.php";
if(isset($_POST['text1'])){
if(strpos($_POST['text1'], "\n")){
$entrytext1 = explode("\n",$_POST['text1']);
}
else{
$entrytext1 = array($_POST['text1']);
}
foreach ($entrytext1 as $linetext1){
$sql="INSERT INTO data(text1,text2)VALUES('$linetext1','$_POST[text2]')";
$result=mysql_query($sql);
if ($result){
echo"<script>alert(\"Success ...\");window.location='../index.php'</script>";
}
else{
echo"<script>alert(\"Failed ...\");self.history.back()</script>";
}
}
}
?>
好吧,我從How to insert multiple rows from a textarea in MySQL 中得到了參考
那么,如何將 textarea 的每一行(text1 和 text2)放入 MySQL 行中? 請幫助我任何人
您有一些 html 錯誤以及不安全的查詢執行。 試試這個: HTML
<form id="form1" name="form1" method="post" action="func/insert.php">
<table>
<tr>
<td>text 1</td>
<td>:</td>
<td><textarea class="custom" rows="10" cols="80" name="text1" id="text1" required></textarea></td>
</tr>
<tr>
<td>text 2</td>
<td>:</td>
<td><textarea class="custom" rows="10" cols="80" name="text2" id="text2" required>
</textarea></td>
</tr>
</table>
</form>
PHP
include"connection.php";
if(isset($_POST['text1'])){
if(strpos($_POST['text1'], "\n")){
$entrytext1 = explode("\n",$_POST['text1']);
}
else{
$entrytext1 = array($_POST['text1']);
}
foreach ($entrytext1 as $linetext1){
$stmt = $mysqli->prepare("INSERT INTO data(text1,text2)VALUES(:text1, :text2)");
$result = $stmt->execute(array('text1' => $linetext1, 'text2' => $_POST['text2']));
if ($result){
echo"<script>alert(\"Success ...\");window.location='../index.php'</script>";
}
else{
echo"<script>alert(\"Failed ...\");self.history.back()</script>";
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.