如何为单个php变量创建多个值?
问题描述:
因此,我的查询在实际的phpmysql
服务器上工作时,我手动键入一些值,但在php
我有一些困难。如何为单个php变量创建多个值?
这是我SQL
表:
userID | forename | surname | email | age |
------------------------------------------------------------
1 | Jack | Wolf | [email protected] | 19 |
2 | Mark | Smith | [email protected] | 18 |
3 | Ben | Cas | [email protected] | 21 |
4 | Jos | Jis | [email protected] | 19 |
5 | Luke | Kils | [email protected] | 23 |
------------------------------------------------------------
基本上,我想在一些UserID
值传递这样1,3,5
,它应该显示:
userID | forename | surname | email | age |
------------------------------------------------------------
1 | Jack | Wolf | [email protected] | 19 |
3 | Ben | Cas | [email protected] | 21 |
5 | Luke | Kils | [email protected] | 23 |
------------------------------------------------------------
的用户ID值可以根据改变什么用户选择它可以是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个参数,但我想要随时输入尽可能多的数据。
我该如何解决?
答
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>
,因为你自定义查询的字体到底得到了你, 没有发布你的HTML表单,我们不知道'$ _POST ['userID']'是否被视为一个数组,并且看起来你做了什么后,它不是。那么,如果你正在检查他们,你会得到什么结果和错误?我怀疑你是谁。 POST多维数组需要被内爆等。 –
嗯,我正在做'android'开发,我使用'postman'来测试代码。是的,我不知道如何破坏它或者它是什么 –
'print_r($ _ POST);'当你发送多个时。 – AbraCadaver