如何保存每行文本区域输入行的长文本



如何从文本区输入每行保存长文本我有一个带有文本区域的表单,我想在mysql中每行保存一个长文本行我不知道

$handle = fopen("inputfile.txt", "r");
if ($handle) {
while (($line = fgets($handle)) !== false) {
// process the line read.
}
fclose($handle);
}

您需要了解mysql如何与数据库交互。根据文本区域的大小,您可能需要使用VARCHAR数据类型。因此,如果from中的字段最多包含250个字符,则文本区域列的数据类型将为VARCHAR(250(。

您可以对一个文件进行POST请求,其中包含以下内容:

$post = $_POST;
//set other fields here, I recommend sanitizing your inputs.
...
$textarea = $_POST['text_area'];
$servername = "HOST";   
$username = "username"; 
$password = "password";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO MyGuests (...other columns you have, textarea)
VALUES (..., $textarea)";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}

我强烈建议查看以下两个链接:
如何通过php过滤输入(在sql执行之前使用(
php MySQL插入数据

如果我查看一下您的表单数据,会更容易。我将使用我自己的表格数据来尝试回答您的问题
form.php

<form action="processing.php" method="POST">
<textarea required name="records" class="form-control" rows="8" cols="4" placeholder="Enter records separated by new line"></textarea>
<button type="submit" name="addRecords" class="btn btn-warning">Add Records</button>
</form>

然后processing.php

if (isset($_POST['addRecords'])) {
$record = $_POST['records'];
//explode records based on new line n
$records = explode("n", $record);
foreach ($records as $new) {
$data = $new;
//Here you'll write your sql code to insert records in the database

}
}

最新更新