2016-03-06 27 views
-1

因此,我的查詢在實際的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個參數,但我想要隨時輸入儘可能多的數據。

我該如何解決?

+0

,因爲你自定義查詢的字體到底得到了你, 沒有發佈你的HTML表單,我們不知道'$ _POST ['userID']'是否被視爲一個數組,並且看起來你做了什麼後,它不是。那麼,如果你正在檢查他們,你會得到什麼結果和錯誤?我懷疑你是誰。 POST多維數組需要被內爆等。 –

+0

嗯,我正在做'android'開發,我使用'postman'來測試代碼。是的,我不知道如何破壞它或者它是什麼 –

+0

'print_r($ _ POST);'當你發送多個時。 – AbraCadaver

回答

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

或者在(x,y,z)中做一個用戶標識,但是是的。不錯的工作。希望它能幫助他 – Drew

+0

謝謝@德魯,我們都希望能夠好起來,對嗎?可以幫助我們解決這個問題。 –

+0

如果我能幫忙,請告訴我:p – Drew

相關問題