0

I am trying to use an array in the where clause of a MySQL query but the query is not displaying the posted array correctly.

I post the array and the console displays:

[RoomOverrideRoomSelect] => Array
(
[0] => 1
[1] => 2
[2] => 3
)

I then process the array like:

$MultiRoomID = $_POST['RoomOverrideRoomSelect'];
$arr_Rooms = array($MultiRoomID);
$str_Rooms=implode(",", $arr_Rooms);

And my query:

"SELECT 
RecordID, ConfRoomID, ConfRoomName   
FROM ConfRooms2017 
WHERE HotelID = '". $_SESSION['hotelid']. "' 
AND ConfRoomID IN ('$str_Rooms')";

If I echo the query I get:

SELECT 
RecordID, ConfRoomID, ConfRoomName   
FROM ConfRooms2017 
WHERE HotelID = 'EXBHX' 
AND ConfRoomID IN ('Array')

What it should be is:

SELECT 
RecordID, ConfRoomID, ConfRoomName   
FROM ConfRooms2017 
WHERE HotelID = 'EXBHX' 
AND ConfRoomID IN (1,2,3)

Can anyone see where I am going wrong?

Dharman
  • 30,962
  • 25
  • 85
  • 135
DCJones
  • 3,121
  • 5
  • 31
  • 53
  • @Dharman Yes there were a couple of spelling errors which you corrected, thanks. I have looked at the so called 'duplicate' question and it is completely different so why mark my question as such. – DCJones Nov 19 '19 at 11:34
  • Please explain how is it different. – Dharman Nov 19 '19 at 11:35
  • 1
    Please kindly pay attention to the following information: you should never ever put a PHP variable into SQL query. It will lead to innumerable problems, from syntax errors to SQL injections. You just faced one of such issues yourself. Instead, you should always use a placeholder mark to represent a variable in the query, and bind a variable separately. With every query you run. So the linked post explains how to do that with the array you have. – Your Common Sense Nov 19 '19 at 11:38

0 Answers0