【问题标题】:How to have multiple values for a single php variable?如何为单个 php 变量设置多个值?
【发布时间】:2016-03-06 22:22:08
【问题描述】:

所以当我手动输入一些值但在php 中我遇到了一些困难时,我的查询可以在实际的phpmysql 服务器上运行。

这是我的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    | 
------------------------------------------------------------

用户 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 个参数,但我想输入多少就输入多少。

我该如何解决?

【问题讨论】:

  • 由于您没有为此发布 HTML 表单,我们不知道 $_POST['userID'] 是否被视为一个数组,从您发布的内容来看,它不是。那么,如果您正在检查它们,您会得到什么结果和错误?我怀疑你是。 POST 多维数组需要内爆等。
  • 嗯,我在做android 开发,我使用postman 来测试代码。是的,我不知道如何内爆它或它是什么
  • print_r($_POST); 当您发送多个时。
  • 使用 $result-&gt;bind_param('i', $userID); 建议您传递一个整数值,但如果您传递一组逗号分隔的 ID,这将是一个字符串
  • @RamRaider 正是这样,但我不知道该怎么做。请你修改我的代码

标签: php android mysql json


【解决方案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> 

【讨论】:

  • 或者在 (x,y,z) 中做一个用户 ID,但是是的。不错的工作。希望对他有帮助
  • 谢谢@Drew,我们都希望变得更好,对吧?对我们俩都有帮助,我不得不解决这个问题。
  • 如果我能提供帮助,请告诉我:p
猜你喜欢
  • 2021-01-01
  • 1970-01-01
  • 2019-10-30
  • 1970-01-01
  • 2016-07-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多