MYSQL NOT IN Query not working unless hard coded - php

I'm fairly new to Javascript and PHP so I have no idea what I'm doing for the most part. I'm trying to make a Room Booking System.
$booking_id = '';
if(isset($_POST['edit_button_booking'])){
$booking_id = $_POST['edit_id'];
}
echo "<script>alert('$booking_id')</script>";
$today_date = date('Y-m-d');
$room_query = "SELECT * FROM rooms";
$room_run = $conn->query($room_query);
if($room_run->num_rows>0){
$options = mysqli_fetch_all($room_run, MYSQLI_ASSOC);
}
$start_from = 8;
$end_from = 21;
$start_to = $start_from+1;
$end_to = $end_from+1;
$from_booked = [];
$to_booked = [];
if(isset($_POST['value'])){
$room_id = $_POST['value'];
$specify_query = "SELECT * FROM rooms WHERE room_id = '$room_id'";
$specify_run = $conn->query($specify_query);
$row = mysqli_fetch_assoc($specify_run);
$room_name = $row['room_name'];
$disable_query = "SELECT booking_id, room_id, booking_date, booking_start, booking_end FROM booking WHERE booking_date = '$today_date' AND room_id = '$room_id' AND booking_id NOT IN (SELECT booking_id FROM booking WHERE booking_id = '$booking_id')";
$disable = $conn->query($disable_query);
if($disable->num_rows > 0){
while($row = $disable->fetch_assoc()){
$from_booked_hour = date_parse($row['booking_start'])['hour'];
$to_booked_hour = date_parse($row['booking_end'])['hour'];
$from_booked = array_merge($from_booked, range($from_booked_hour, $to_booked_hour-1));
$to_booked = array_merge($to_booked, range($from_booked_hour+1, $to_booked_hour));
}
}
}
This is my code. What this code is supposed to do is to display the booked time slots for the room except for the chosen time slot.
And yet, it keeps on displaying both booked times when I want the 9 -12 time slot to be omitted from the time table
The alert that I used show that $booking_id has the value '000053' which is the id of the booking that I want not shown in the time table.
If I were to hard code it to $booking_id = '000053' or $booking_id = 53, it works.
$booking_id = '';
if(isset($_POST['edit_button_booking'])){
$booking_id = $_POST['edit_id'];
}
$booking_id = 53;
echo "<script>alert('$booking_id')</script>";
$today_date = date('Y-m-d');
$room_query = "SELECT * FROM rooms";
$room_run = $conn->query($room_query);
if($room_run->num_rows>0){
$options = mysqli_fetch_all($room_run, MYSQLI_ASSOC);
}
$start_from = 8;
$end_from = 21;
$start_to = $start_from+1;
$end_to = $end_from+1;
$from_booked = [];
$to_booked = [];
if(isset($_POST['value'])){
$room_id = $_POST['value'];
$specify_query = "SELECT * FROM rooms WHERE room_id = '$room_id'";
$specify_run = $conn->query($specify_query);
$row = mysqli_fetch_assoc($specify_run);
$room_name = $row['room_name'];
$disable_query = "SELECT booking_id, room_id, booking_date, booking_start, booking_end FROM booking WHERE booking_date = '$today_date' AND room_id = '$room_id' AND booking_id NOT IN (SELECT booking_id FROM booking WHERE booking_id = '$booking_id')";
$disable = $conn->query($disable_query);
if($disable->num_rows > 0){
while($row = $disable->fetch_assoc()){
$from_booked_hour = date_parse($row['booking_start'])['hour'];
$to_booked_hour = date_parse($row['booking_end'])['hour'];
$from_booked = array_merge($from_booked, range($from_booked_hour, $to_booked_hour-1));
$to_booked = array_merge($to_booked, range($from_booked_hour+1, $to_booked_hour));
}
}
}
I don't know what is causing this issue. Any help will be appreciated.
The var_dump of both queries are as follows:
Generated SQL:
string(156) "SELECT booking_id, room_id, booking_date, booking_start, booking_end FROM booking WHERE booking_date = '2022-07-22' AND room_id = '1' AND booking_id != '53'"
Hard Coded:
string(156) "SELECT booking_id, room_id, booking_date, booking_start, booking_end FROM booking WHERE booking_date = '2022-07-22' AND room_id = '1' AND booking_id != '53'"
UPDATE:
The var_dump()'s I posted earlier were outside of the if(isset($_POST['value'])) block since I thought the variable $booking_id can be accessed in the if block as well. I tried to use var_dump() inside the if block and it turned out that the $booking_id was turning up blank.
string(154) "SELECT booking_id, room_id, booking_date, booking_start, booking_end FROM booking WHERE booking_date = '2022-07-22' AND room_id = '1' AND booking_id != ''"
This was the result of the var_dump() running inside of the if block. I don't understand why it is doing this though. My $today_date variable was defined outside of the if block as well and that's working.

Related

php mysql query is not doing sum properly

I am doing sum with the below query but it is not giving result properly.
If there are four items and it is showing the result like:
1.000 2.000 3.000 4.000 and it should be like 10.000
I don't know where I am mistaken please help.
<?php
$order_temp = mysql_query("select * from temp_cart
where item_id = '".$mitem_idC."' and ses_mem=113 order by id");
while ($torder = mysql_fetch_array($order_temp)) {
$prITTC = $torder['item_id'];
$qtyT = $torder['qty'];
$chTP = mysql_query("
select * from temp_choices
where item_id = '".$prITTC."'
AND ses_mem = 113
AND status = 1
");
while($chGET = mysql_fetch_array($chTP)){
$fID = $chGET['id'];
$field = $chGET['choice_id'];
$order_tempCHP = mysql_query("
select sum(price) as total, id, ename, choice_id, item_id, price
from choice_price
WHERE
id IN('$field')
");
while ($torderCP = mysql_fetch_assoc($order_tempCHP)){
$totalCH = $torderCP['total'];
$tsl = $totalCH+($qtyT*$prIDTC);
$altsl = number_format($tsl, 3, '.', '');
echo $altsl;
} }
}
?>
according to my question above after trying and trying i found the solution and resolved my problem according to below code:
Thanks to #John Kugelman at MySQL query using an array
$order_temp = mysql_query("select * from temp_cart
where item_id = '".$mitem_idC."' and ses_mem=113 order by id");
while ($torder = mysql_fetch_array($order_temp)) {
$prITTD = $torder['id'];
$prITTC = $torder['item_id'];
$chTPaa = mysql_query("
select choice_id
FROM temp_choices
WHERE item_id = '$prITTC'
AND ses_mem = 113
AND status = 1
group by choice_id
");
while ($chGETaa = mysql_fetch_assoc($chTPaa)){
$temp[] = $chGETaa['choice_id'];
}
$thelist = implode(",",$temp);
$order_tempCHP = mysql_query("
select sum(price) as total
from choice_price
WHERE
id IN ($thelist)
AND
item_id = '".$prITTC."'
");
while($torderCP = mysql_fetch_assoc($order_tempCHP)){
$totalCH = $torderCP['total'];
$tsl = $totalCH+($qtyT*$prIDTC);
$altsl = number_format($tsl, 3, '.', '');
echo $altsl;
} }

Using multiple while loops getting only 1 record

I am just starting to learn php, so bear with me...
I am trying to get a list of employees from a list of supervisors.
I am only getting the last supervisor ..
I am guessing that it is because I have the code in the wrong places..
Here is the code I have so far..
//Get List of Supervisors
$result_supervisors = mysql_query("SELECT emp_no as semp_no, full_name as sfull_name,employee_email as s_email FROM employees where supervisor = '1' ORDER BY emp_no");
// Initializes Supervisor Array
$supervisors = array();
while($row = mysql_fetch_array($result_supervisors, MYSQL_ASSOC))
{
$supervisor = $row['semp_no'];
$supervisor_name = $row['sfull_name'];
$supervisor_email = $row['s_email'];
//add row to supervisors array
$supervisors[] = array($supervisor, $supervisor_name, $supervisor_email);
}
//Get employee for the supervisor
$empquery = "SELECT emp_no, full_name, review_date,employee_email as e_email FROM employees where review_date < curdate() and employment_status !='2' and emp_supervisor = ". $supervisor ;
$empresult = mysql_query($empquery);
while($emprow = mysql_fetch_array($empresult))
{
if ($emprow['cnt'] == '0')
{
// no records for supervisor
}
else
{
$emp_no = $emprow['emp_no'];
$full_name = $emprow['full_name'];
$e_email = $emprow['e_email'];
$review_date = $emprow['review_date'];
//add row to employees array
$employees[] = array($supervisor, $supervisor_name, $supervisor_email,$emp_no,$full_name,$e_email,$review_date);
}
}
print_r($employees);
I am trying to put the results into an html table and email it to the supervisor for the end result..
But getting more than one supervisor is my biggest issue..
Is there an easy way to put the data into an html table.. in case someone knows why only one supervisor is being done...
Any help that can help a newbie?
KD
Change your first query:
$result_supervisors = mysql_query("SELECT emp_no as semp_no, full_name as sfull_name,employee_email as s_email FROM employees ORDER BY emp_no");
Try this code :
//Get List of Supervisors
$result_supervisors = mysql_query("SELECT emp_no as semp_no, full_name as sfull_name,employee_email as s_email FROM employees where supervisor = '1' ORDER BY emp_no");
// Initializes Supervisor Array
$supervisors = array();
$employees = array();
while($row = mysql_fetch_array($result_supervisors, MYSQL_ASSOC))
{
$supervisor = $row['semp_no'];
$supervisor_name = $row['sfull_name'];
$supervisor_email = $row['s_email'];
//add row to supervisors array
$supervisors[] = array($supervisor, $supervisor_name, $supervisor_email);
//Get employee for the supervisor
$empquery = "SELECT emp_no, full_name, review_date,employee_email as e_email FROM employees where review_date < curdate() and employment_status !='2' and emp_supervisor = ". $supervisor ;
$empresult = mysql_query($empquery);
while($emprow = mysql_fetch_array($empresult))
{
if ($emprow['cnt'] == '0')
{
// no records for supervisor
}
else
{
$emp_no = $emprow['emp_no'];
$full_name = $emprow['full_name'];
$e_email = $emprow['e_email'];
$review_date = $emprow['review_date'];
//add row to employees array
$employees[] = array($supervisor, $supervisor_name, $supervisor_email,$emp_no,$full_name,$e_email,$review_date);
}
}
}
print_r($employees);

Query error not getting numbers

Here is my query, I am trying to get numbers from another table using a number from another table here is my query ...
$order_id = $template_vars['{order_name}'];
// Query to find the product id for the current order and then set it to a variable
$query="SELECT product_id FROM ps_order_detail WHERE id_order = $order_id";
$result = mysql_query($query);
$row = mysql_fetch_row($result);
$Product_id = $row['0'];
// get all the custom part numbers and set them to the variables
$customnumbers ="SELECT API, SWAIM, JOHN_CRANE, SNOW_WELL, MIDAS, QUINN, WILSON, WEATHERFORD, HF, BLACK_GOLD, EDI, SO_CAL_PUMPS, WEST_RIVER
FROM ps_product_part_number WHERE Product_ID = $Product_id";
$secondresult = mysql_query($customnumbers);
$secondrow = mysql_fetch_row($secondresult);
$API = $secondrow['0'];
$SWAIM = $secondrow['1'];
$JOHN_CRANE = $secondrow['2'];
$SNOW_WELL = $secondrow['3'];
$MIDAS = $secondrow['4'];
$QUINN = $secondrow['5'];
$WILSON = $secondrow['6'];
$WEATHERFORD = $secondrow['7'];
$HF = $secondrow['8'];
$BLACK_GOLD = $secondrow['9'];
$EDI = $secondrow['10'];
$SO_CAL_PUMPS = $secondrow['11'];
$WEST_RIVER = $secondrow['12'];
How about doing it in one step with a join?
SELECT ppn.*
FROM ps_product_part_number ppn
join ps_order_detail od on od.product_id = ppn.Product_ID
WHERE od.id_order = $order_id

PHP Optimizing a check

I'm trying to optimize this check I have. I need to check table called lines and see if any row has matching Earned and Maxearned values (only rows with Position 1,2,3,4). If they do, I need to grab Earned from that row, write it in a different table called bank and remove that row from table called lines. This is what I have:
$sql3 = "SELECT * FROM `lines` WHERE Position <= 4 AND Linenum = '$linenum' AND Earned = Maxearned";
$result3 = mysql_query($sql3);
if (mysql_num_rows($result3) != 0)
{
while ($row3 = mysql_fetch_array($result3))
{
$users[] = $row3['User'];
}
foreach ($users as $user)
{
$sql6 = "SELECT * FROM `lines` WHERE Position <= 4 AND Linenum = '$linenum' AND Earned = Maxearned AND User = '$user'";
$result4 = mysql_query($sql6);
while ($row4 = mysql_fetch_array($result4))
{
$earned = $row4['Earned'];
}
$today = date("Y-m-d");
$method = "Queue money";
$type = "Earned";
$status = "Completed";
$sql4 = "INSERT INTO bank (User,Amount,Method,Transdate,Type,Status) VALUES ('$user','$earned','$method','$today','$type','$status')";
$sql5 = "DELETE FROM `lines` WHERE Position <= 4 AND Linenum = '$linenum' AND Earned = Maxearned AND User = '$user'";
}
$sql7 = "UPDATE `lines` SET Position = Position - 1 WHERE Linenum = '$linenum'";
}
I'm trying to avoid having to run a different query ($sql6 and the while after that) to grab the value of Earned column. Is there a way to do this? I've tried everything and this is pretty much the best I came up with.
You can do something like this:
mysql_query("SET AUTOCOMMIT=0");
mysql_query("START TRANSACTION");
$sql3 = "SELECT * FROM `lines` WHERE Position <= 4 AND Linenum = '$linenum' AND Earned = Maxearned";
$result3 = mysql_query($sql3);
if (mysql_num_rows($result3) != 0)
{
while ($row3 = mysql_fetch_array($result3))
{
$users[] = $row3['User'];
}
$users_to_compare = "(" . rtrim(implode(",", $users),',') . ")";
$sql4 = "SELECT * FROM `lines` WHERE Position <= 4 AND Linenum = '$linenum' AND Earned = Maxearned AND User IN $users_to_compare";
$result4 = mysql_query($sql4);
while ($row4 = mysql_fetch_array($result4))
{
$earned = $row4['Earned'];
$today = date("Y-m-d");
$method = "Queue money";
$type = "Earned";
$status = "Completed";
$sql5 = "INSERT INTO bank (User,Amount,Method,Transdate,Type,Status) VALUES ('{$row4['User']}','$earned','$method','$today','$type','$status')";
$result5 = mysql_query($sql5);
}
$sql6 = "DELETE FROM `lines` WHERE Position <= 4 AND Linenum = '$linenum' AND Earned = Maxearned AND User IN $users_to_compare";
$result6 = mysql_query($sql6);
$sql7 = "UPDATE `lines` SET Position = Position - 1 WHERE Linenum = '$linenum'";
$result7 = mysql_query($sql7);
if ($result5 && $result5 && $result7) {
mysql_query("COMMIT");
} else {
mysql_query("ROLLBACK");
}
}
Going one step further you can also use Batch INSERT for you insert queries
Note: Dont forget that mysql_* versions are depreceated you should use mysqli_*

Order MySQL inbox by newest conversation first?

I've a MySQL/PHP code for a private messaging system that I built. It works great although I'm fairly new to it all so struggling to get the message threads to display by newest first. Is there any chance you could offer advice? The current code is as follows:
$result = '';
$nowTime = time();
$getmessages = mysql_query("SELECT * FROM messages WHERE msg_to = '$session_memberid' GROUP BY msg_from ORDER BY ID DESC");
while($iamessages = mysql_fetch_array($getmessages))
{
$msg_id = $iamessages['ID'];
$msg_from = $iamessages['msg_from'];
$msg_conversation = $iamessages['conversation'];
$getmsgdata = mysql_query("SELECT * FROM messages WHERE msg_to = '$session_memberid' AND msg_from = '$msg_from' ORDER BY ID DESC LIMIT 1");
while($imsd = mysql_fetch_array($getmsgdata))
{
$msg_message = $imsd['msg_message'];
$msg_time = $imsd['time'];
$msg_read = $imsd['msg_read'];
}
$msg_conversation = suitcrypt($msg_conversation);
if ( $msg_read == 'no' ) { $msgclass = "messagepostunread"; } else { $msgclass = "messagepost"; }
$getfromdata = mysql_query("SELECT FullName, Username, status FROM members WHERE ID = '$msg_from'");
while($ifd = mysql_fetch_array($getfromdata))
{
$msg_from_name = $ifd['FullName'];
$msg_from_username = $ifd['Username'];
$msg_from_status = $ifd['status'];
}
$getfromdata1 = mysql_query("SELECT Link FROM images WHERE MemberID = '$msg_from' AND is_primary = 'yes'");
while($ifd1 = mysql_fetch_array($getfromdata1)) {
$msg_from_img = $ifd1['Link'];
}
$timepass = timing($msg_time);
if ( $timepass == 'data' ) {
$timepass = date("dS M", $msg_time);
}
if ( ( $msg_from_status == 'full' ) || ( $msg_from_status == 'active' ) ) {
$result .= "
<div class=\"$msgclass\" onclick=\"showconversation('$msg_conversation');\">
<img src=\"m/members_image/$msg_from/thumb-$msg_from_img\" class=\"feedpic\" width=\"55\" height=\"55\" /><div class=\"messageposttext\">$msg_from_name:<div class=\"inboxfeedreply\">Reply ยท $timepass</div><br />$msg_message</div>
</div>
<div class=\"splittermessages\"></div>
";
}
Within each table entry in the messages table there is a 'time' stamp. Here's an example of the time entries: 1367680391. What's the best way to order the threads by newest reply first?
First off I think you should group by $msg_conversation and find the last date on each conversation. With the code below you have the conversatons ordered by the last message each conversation/thread had.
$result = '';
$nowTime = time();
$getmessages = mysql_query("SELECT conversation, max(date) FROM messages WHERE msg_to = '$session_memberid' GROUP BY conversation ORDER BY max(date)");
while($iamessages = mysql_fetch_array($getmessages))
{
$msg_conversation = $iamessages['conversation'];
...
Further on you can get the messages for each conversation by date descending.
I got someone to help and the answer was to update the query to the following:
$getmessages = mysql_query("SELECT * FROM messages WHERE msg_to = '$session_memberid' GROUP BY msg_from ORDER BY MAX(time) DESC");

Categories