Mysql
 sql >> Database >  >> RDS >> Mysql

Come inserire più righe da textareas in MySQL

Hai avuto alcuni errori html e l'esecuzione di query non sicura. Prova questo: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>";
        }
    }
}