如何从 MySQL 中的 textareas 插入多行 [英] How to insert multiple row from textareas in MySQL

查看:58
本文介绍了如何从 MySQL 中的 textareas 插入多行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法从两个文本区域(每行)插入到 mysql,

i cant insert to mysql from two textarea (both every row),

首先这是一个 add.html

first this is a 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从 MySQL 中的 textarea 插入多行

那么,如何将 textarea 的每一行(text1 和 text2)放入 MySQL 行中?请任何人帮助我

So, how can i put every row (text1 and text2) of the textarea into a MySQL row? please help me anyone

推荐答案

您遇到了一些 html 错误以及不安全的查询执行.试试这个:HTML

You had some html errors as well as unsecure query execution. Try this: 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>";
        }
    }
}

这篇关于如何从 MySQL 中的 textareas 插入多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆