0

Here i have made an MySQL which has the columns of "id","name","username","email",age" . Where even i made the the PHP code to retrieve the data for the given id.

eg:if the user enter id=3 then it shows the datas corresponding to the id.

But now i want set multiple inputs so that user can type more than one id and it list the corresponding datas of the particular id.

My PHP Code:

<?php 

if($_SERVER['REQUEST_METHOD']=='GET'){

    $id  = $_GET['id'];




    require_once('dbConnect.php');

    $sql = "SELECT * FROM user WHERE id='".$id."'";

    $r = mysqli_query($con,$sql);

    $result = array();

            while($res = mysqli_fetch_array($r)){

    array_push($result,array(
        "id"=>$res['id'],
        "name"=>$res['name'],
        "username"=>$res['username'],
        "email"=>$res['email'],
         "age"=>$res['age']
        )
    );
            }
    echo json_encode(array("result"=>$result));

    mysqli_close($con);

} 

Now this URL gives the perfect result: "http://www.allwaysready.16mb.com/Sort.php?id=4"

Now how can i get the corresponding values for the multiple id's?

4
  • never going to work, since you never use $id in your query. $work is undefined. Commented Jul 25, 2016 at 17:04
  • i edited the question Commented Jul 25, 2016 at 17:06
  • and you're vulnerable to sql injection attacks Commented Jul 25, 2016 at 17:09
  • So what have you tried. SO is not a free coding service This would required chnages to your HTML and you PHP and as we annot see your HTML this is Way off topic Commented Jul 26, 2016 at 12:50

2 Answers 2

1

You can use array syntax to pass multiple IDs to your script and use MySQL's IN() to query against them all at once.

URL: http://www.allwaysready.16mb.com/Sort.php?id[]=4&id[]1&id[]=2

$ids = $_GET['id'];
$ids = array_map(function($id) {
    return (int) $id;
}, $ids);
$ids = implode(',', $ids);

$sql = "SELECT * FROM user WHERE work IN($ids);

I cast the IDs to integers because your current code is wide open to SQL injection. You really should be using paramterized queries.

Sign up to request clarification or add additional context in comments.

6 Comments

I get some error can u answer with the exact code plz
Parse error: syntax error, unexpected 'id' (T_STRING) in /home/u115908902/public_html/mysort.php on line 19
I just added a line of code that I overlooked. See if that resolves anything for you.
i have another question, in the URL we must use the '&' sign uh ?
|
0

Use the "IN" condition:

If id is a number:

SELECT * FROM user WHERE id IN (89, 25);

If id is a string, put the id's in quotes:

SELECT * FROM user WHERE id IN ('89N', '15B', '25E');

Rewrite your query in PHP to use the In Query condition, paying close attention to your column definition data types. Strings must be quoted for example.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.