简体   繁体   中英

How to have multiple values for a single php variable?

So my query works on the actual phpmysql server when I manually type in some values but in php I am having some difficulty.

This is my SQL table:

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    | 
------------------------------------------------------------

Basically, I want to pass in some UserID values like this 1,3,5 and it should display:

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    | 
------------------------------------------------------------

The userID values can vary depending on what the user selects so it can be 2 or even 1,2,3,4 or even 1,2,3,4,5

This is my php code:

<?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");
    }
}

?>

My code can't do the userID = 1,2,3 thing, if you know what I mean. userID can only take at most 1 parameter but I want to input as many as I like at times.

How can I fix it?

preview of form and table in browser

Ok @Lukazs Pioetrszci i got you, on the font end you need the query result on the back end you need a custom Query based on available table date

   <?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> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM