Запись массива из textarea в MySQL
есть следующий код:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title></title>
<meta content="width=device-width, initial-scale=1.0" name="viewport" >
</head>
<body>
<form action="string_load_sql.php" method="post">
<textarea name="text"></textarea>
<input type="submit" name="send" value="Send">
</form>
</body>
</html>
string_load_sql.php файл
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "msg_oper";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if (isset($_POST["text"])) {
$text = explode("\n",(trim($_POST["text"])));
foreach ( $text AS $text_string ) {
$sql = "INSERT INTO `oper_find` (num) VALUES ('".$text_string."')";
echo($text_string);
}
}
for($x=0;$x<100;$x++)
{
if ($conn->query($sql) === TRUE) {
echo "OK!";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
$conn->close();
?>
данные ведение в textarea выводятся построчно "echo($text)". Как их записать в базу данных sql, так же построчно?
Ответы (2 шт):
Автор решения: sterx
→ Ссылка
foreach(explode("\n",$_POST['text']) as $str){
//тут пишем $str в БД
}
Автор решения: VK13
→ Ссылка
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "msg_oper";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if (isset($_POST["text"])) {
$text = explode("\n",(trim($_POST["text"])));
for($x=0;$x<1;$x++)
{
foreach ( $text AS $text_string ) {
$sql = "INSERT INTO `oper_find` (num) VALUES ('".$text_string."')";
echo($text_string);
if ($conn->query($sql) === TRUE) {
echo "OK!";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
}
}
$conn->close();
?>