因此,当我手动输入某些值时,我的查询适用于实际的phpmysql
服务器,但在php
我遇到了一些困难。
这是我的SQL
表:
userID | forename | surname | email | age |
------------------------------------------------------------
1 | Jack | Wolf | dj@rave.com | 19 |
2 | Mark | Smith | mark@rave.com | 18 |
3 | Ben | Cas | sex@club.com | 21 |
4 | Jos | Jis | jis@jos.com | 19 |
5 | Luke | Kils | kils@kiss.com | 23 |
------------------------------------------------------------
基本上,我想传递一些UserID
这样的1,3,5
值,它应显示:
userID | forename | surname | email | age |
------------------------------------------------------------
1 | Jack | Wolf | dj@rave.com | 19 |
3 | Ben | Cas | sex@club.com | 21 |
5 | Luke | Kils | kils@kiss.com | 23 |
------------------------------------------------------------
userID值可能会因用户选择的内容而异,因此可以2
甚至1,2,3,4
甚至1,2,3,4,5
这是我的php
代码:
<?php
require "init.php";
if(!empty($_POST['userID'])){
$userID = $_POST['userID'];
$stmt = "SELECT userID, forename, surname, email, age
FROM users
WHERE userID IN (?)";
$result = $conn-> prepare($stmt);
$result->bind_param('i', $userID);
$result->execute();
$outcome=$result->get_result();
$response = array();
if(($outcome->num_rows)>0){
while($row = $outcome->fetch_assoc()){
$response[] = array
(
"userID" => $row["userID"],
"forename" => $row["forename"],
"surname" => $row["surname"],
"email" => $row["email"],
"age" => $row["age"]
);
}
echo json_encode($response);
}
else{
echo json_encode("None found");
}
}
?>
如果您知道我的意思,我的代码就无法执行userID = 1,2,3
。 userID
最多只能输入1个参数,但我希望有时输入多少参数。
我该如何解决?
答案 0 :(得分:0)
preview of form and table in browser
好的@Lukazs Pioetrszci我找到了你, 在字体结尾,您需要查询结果 在后端,您需要基于可用的表日期自定义查询
<?php
require "init.php";
$servername = "localhost";
$username = "masterHQ";
$password = "mein1234";
$mySelected = $_POST['selectedID'];
$myWho = $_POST['who'];
$clearMe = $_POST['clear'];
//$SelectTable = "SELECT * FROM `users` ORDER BY `userID` ASC";
$mysqli = new mysqli('localhost', 'masterHQ', 'mein1234', 'test_me');
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
echo "<br />";
$tally = count($myWho);
echo $tally . " count results";
foreach ($myWho as $Who) {
$fromList .= "`userID` = "." ". $Who . " ";
$tally--;
if($tally > 0){
$fromList .= " OR"." ";
}
}
$SelectThis = "SELECT * FROM `users` WHERE " . $fromList . "ORDER BY `userID` ASC ";
$joe = "SELECT * FROM `users` WHERE `userID` = 1 OR `userID` = 2 OR `userID` = 3 ORDER BY `userID` ASC" ;
echo "<br />";
if ($clearMe == 'clear'){
$SelectThis = "SELECT * FROM `users` ORDER BY `userID` ASC";
$clearMe == '';
}
echo "check selected sql query: " . $SelectThis; // more error checking of custom query
if ($stmt = $mysqli->prepare( $SelectThis )) {
/* bind parameters for markers */
echo "<br /> param pass<br />";
/* execute query */
$result=$stmt->execute();
echo "<br /> execute pass<br />"; // error check
//$outcome=$result->get_result();
/* Store the result (to get properties) */
$stmt->store_result();
/* Get the number of rows */
echo "store pass" ; // error check
/* bind result variables */
$stmt->bind_result($userID, $name, $last, $email, $age );
echo "<br /> result pass <br />"; // error check results
?>
<form action="answerdb_Lukazs.php" method="post">
<table border="1px" width="80%">
<th>Selected ID</th><th>ID</th> <th>Name</th> <th>last</th> <th>email</th><th>age</th>
<?php
while( $stmt->fetch()){
echo '<tr><td><input type="checkbox" name="who[]" value="' . $userID . '" checked> ' . $userID . '</td>';
//printf("%s is in district %s\n", $userID, $City, $District);
echo " <td>" . $userID . "</td> <td>" . $name . "</td><td> " . $last . "</td><td> " . $email . "</td><td> " . $age . "</td></tr>";
}
// echo "<br /> fetch <br />";
/* close statement */
$stmt->close();
}
else {
echo "Exit Query"; // query fail
}
?>
</tr></table>
<?php
/* close connection */
$mysqli->close();
?>
<input type="submit" value="Remove ID"/>
</form>
<form action="answerdb_Lukazs.php" method="post">
<input type="hidden" name="clear" value="clear">
<input type="submit" value="Show All"/>
</form>