所以我遇到了一个关于文本区域的问题。我的目标是拥有一个带有文本区域的表单,用户可以在其中逐行输入字母数字名称,然后从数据库中提取该信息并将其显示到表格中。
例如:
tt1
tt2
tt3
提交后,它将返回与这 3 个名称关联的所有数据。
我可以获取文本区域,解析它并获取要插入到 SQL 查询中的原始值,但我在输出结果时遇到了困难。
我现在的代码如下:
index.html
<form method="POST" action="getreport.php">
<div class="form-group">
<label for="textarea">Textarea</label>
<textarea class="form-control" name="textarea" id="textarea" class="textarea" rows="5" cols="50"></textarea>
</div>
<button type="submit" class="btn btn-primary" >Submit</button>
</form>
getreport.php
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "server";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$text = trim($_POST['textarea']);
$textAr = preg_split('/[\n\r]+/', $text); //<--- preg_split is where the magic happens
$textAr = array_filter($textAr, 'trim'); // remove any extra \r characters left behind
foreach ($textAr as $line) {
// processing here.
$sql = "SELECT * from guestlist WHERE guestname='$line'";
echo "$sql"; //just checking query output for now
}
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) { //<---- take this while loop out
echo '<table class="table table-striped table-bordered table-hover">';
echo "<tr><th>Hostname</th><th>Guestname:</th><th>date</th><th>owner</th></tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr><td>";
echo $row['hostname'];
echo "</td><td>";
echo $row['guestname'];
echo "</td><td>";
echo $row['date'];
echo "</td><td>";
echo $row['owner'];
echo "</td></tr>";
}
echo "</table>";
} //<-----as well as the closing bracket
} else {
echo "0 results";
}
$conn->close();
?>
对此的任何帮助或指导将不胜感激。
谢谢
最佳答案
不确定你解决问题的方法是否正确,而且正如有人建议的那样,你应该担心 SQL 注入(inject)。据说,这可能是一种解决方案:
$text = trim($_POST['textarea']);
$textAr = str_replace("/n",",", $text);
$sql = "SELECT * from guestlist WHERE FIND_IN_SET(guestname,'$line')>0";
$result = $conn->query($sql);
另一种方法可能是每次都迭代
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "server";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$text = trim($_POST['textarea']);
$textAr = explode("/n", $text);
$textAr = array_filter($textAr, 'trim'); // remove any extra \r characters left behind
foreach ($textAr as $line) {
// processing here.
$sql = "SELECT * from guestlist WHERE guestname='$line'";
echo "$sql"; //just checking query output for now
$result = $conn->query($sql);
// <---------------- ITERATE INSIDE THE FOREACH
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo '<table class="table table-striped table-bordered table-hover">';
echo "<tr><th>Hostname</th><th>Guestname:</th><th>date</th><th>owner</th></tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr><td>";
echo $row['hostname'];
echo "</td><td>";
echo $row['guestname'];
echo "</td><td>";
echo $row['date'];
echo "</td><td>";
echo $row['owner'];
echo "</td></tr>";
}
echo "</table>";
}
} else {
echo "0 results";
}
} // Close the foreach
$conn->close();
?>
关于javascript - 检索文本区域框中每行的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44742045/