1
我目前有一個帶有textarea的網頁設置,該文本應該用於輸入變量,然後在隨後的mysqli查詢中使用。查詢會在數據庫中進行幾次連接,然後將結果列在表中。我怎麼能夠在textarea中輸入多個條目來多次執行查詢並在表中列出所有結果?如何在php中使用多個變量進行mysqli連接查詢
- 另外,是否有可能讓textarea條目根據文本格式轉到不同的變量:即如果條目是字母/數字 - > $ variable1,如果條目是dbname:(letters/numbers ) - > $ 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>