2

In postman,

I post the array like

enter image description here

key => Image[] / value => 1
key => Image[] / value => 2
key => Image[] / value => 3

I want when I put an array which user's card images, receive the card's Idx and Name.

So I make query like this,

$query = "SELECT DISTINCT
                    Idx AS Idx, 
                    Name AS Name 
                    FROM Card
                    WHERE Card.Image IN ('$_POST[Image]')";

But, It receive no data. So I remove '' so

WHERE Card.Image IN ('$_POST[Image]')" 
=> WHERE Card.Image IN ($_POST[Image])";

Then return Unknown column 'Array' in 'where clause'

I don't know what is problem of this.

when I post array like /key name[]/value 1 I know it can receive $_POST[name].

How I can post array and receive of it.

Please help me.

5
  • add your post code. It seems that you aren't posting the images correctly. To set more than one file/image at the same time you'll need a preventdefault and an ajax function and insert one by one (this is the best secure and reliable option). Commented Mar 14, 2018 at 8:43
  • Card.Image-> is Image column have numbers? Commented Mar 14, 2018 at 8:45
  • @JoelBonetR I just using postman so I don't add this but add the picture Commented Mar 14, 2018 at 8:56
  • oh ok then, it's ok Commented Mar 14, 2018 at 8:58
  • @PolarisNation did you checked my last edited answer. please check and try and let me know worked or not? Commented Mar 17, 2018 at 6:44

2 Answers 2

1

I hope Image column contains numbers.

Use implode() to correct your IN query

$string=$_POST['Image'];
$array= implode(',', $string);
$query = "SELECT DISTINCT Idx AS Idx, Name AS Name FROM Card WHERE Card.Image IN ($array)";

Note:-

1.Query will become:-SELECT DISTINCT Idx AS Idx, Name AS Name FROM Card WHERE Card.Image IN (1,2,3). Now it will execute fine.

2.Try to use prepared statements of mysqli_* or PDO to prevent your code from SQL-INJECTION. You current code is wide-open for it.

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

1 Comment

Thanks you saved my time. the solution was settled.
0
$string=$_POST[Image];
$array=array_map('intval', explode(',', $string));
$array = implode("','",$array);
$query = "SELECT DISTINCT Idx AS Idx, Name AS Name FROM Card WHERE 
           Card.Image IN ('".$array."')";

2 Comments

While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value.
I try this code but could not get any response of it

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.