我目前有一个带有textarea的网页设置,应该用来输入一个变量,然后在后续的mysqli查询中使用。查询在数据库中执行一些连接,然后在表中列出结果。如何在textarea中输入多个条目以多次执行查询并在表格中列出所有结果?
- 此外,是否可以让textarea条目根据文本格式转到不同的变量:即,如果条目是字母/数字 - >> $ variable1,如果条目是dbname :(字母/数字) - > $ variable2所以我可以在不同的列中匹配不同的变量来指定多个“WHERE”子句。
代码如下:
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Header</title>
<link rel="stylesheet" href="css/foundation.css" />
<script src="js/vendor/modernizr.js"></script>
</head>
<style>
body {
background-color: ;
}
</style>
<body>
<style>
h1 {
text-align: center;
}
h3 {
text-align: center;
}
h5 {
text-align: center;
}
#panel {
min-height: 800px;
}
#search {
display: inline-block;
}
</style>
<div class="row">
<div class="large-12 columns">
<h1>Header</h1>
<hr/>
</div>
</div>
<div class="row">
<div class="large-12 columns">
<div id="panel" class="panel">
<div id="search" class="large-4 columns">
<form align="center" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="post">
<label>Insert Data
<textarea rows="25" name="variable"><?php echo $variable ?></textarea>
</label>
<input align="left" type="submit" name="query" value="Results" class="small radius button"></input>
</form>
</div>
<table align="center" role="grid">
<thead>
<tr>
<th>1</th>
<th>2</th>
<th>3</th>
<th>4</th>
</tr>
</thead>
<tbody>
<?php
$variable = test_input($_POST['variable']);
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
if(isset($_POST['query'])){
$mysqli = new mysqli("127.0.0.1", "root", "password", "dbname");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
echo $mysqli->host_info . "\n";
$result = $mysqli->query("SELECT a.1, a.2, b.3, g.4 FROM alpha a LEFT OUTER JOIN beta b ON a.1 = b.1 LEFT OUTER JOIN gamma g ON a.1 = g.1 WHERE a.1='$variable';");
if (!$result) {
echo "Database Query Failed: (" . $mysqli->errno . ") " . $mysqli->error;
}
$i = 0;
while ($row = $result->fetch_array()) {
$class = ($i == 0) ? "" : "alt";
echo "<tr class=\"".$class."\">";
echo "<tr>";
echo "<td>" .$row["1"]. "</td>";
echo "<td>" .$row["2"]. "</td>";
echo "<td>" .$row["3"]. "</td>";
echo "<td>" .$row["4"]. "</td>";
echo "</tr>";
$i = ($i==0) ? 1:0;
}
$mysqli->close();
}
?>
</tbody>
</table>
</div>
</div>
</div>
<script src="js/vendor/jquery.js"></script>
<script src="js/foundation.min.js"></script>
<script>
$(document).foundation();
</script>
</body>
答案 0 :(得分:0)
通过循环
计算出来$variable = explode("\n", $_POST['variable']);