Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8909733
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T03:30:25+00:00 2026-06-15T03:30:25+00:00

I’m trying to update a range of dates with a Shift_ID but the Shift_ID

  • 0

I’m trying to update a range of dates with a Shift_ID but the Shift_ID that is entered depends upon what day of the week it is. So, for example, if I have a range of dates $from = "2012-12-01" $to = "2012-12-28" I’m trying to make it so that I can select a check box (for example: value="Fri" name = "offdays[]") to indicate what day is an offdays. If an offdays is matched with a day in the set range, then the Shift_ID = "6" otherwise it is a work day and Shift_ID = "3"

Hopefully this will all make sense in a minute, I’m doing my best to explain it as well as give you some useful variables.

So here is my code:

if(isset($_POST['submit']))
    {
    if(isset($_POST['offdays']))
        {
        //$off is set through the config settings//
        $shift = mysqli_real_escape_string($_POST['shift']);
        $from = mysqli_real_escape_string($_POST['from']);
        $to = mysqli_real_escape_string($_POST['to']);
        $emp_id = mysqli_real_escape_string($_POST['emp_id']);

        foreach($_POST['offdays'] as $checkbox)
            {
            echo "Checkbox: " . $checkbox . "<br>";//error checking
            $sql = ("SELECT Date FROM schedule WHERE (Date BETWEEN '$from' AND '$to') AND (Emp_ID = '$emp_id')");

            if(!$result_date_query = $mysqli->query($sql))
                {
                echo "INSERT ERROR 1 HERE";
                }
                echo "SELECT SQL: " . $sql . "<br>";//error checking

                while($row = $result_date_query->fetch_assoc())
                    {
                    echo "Date: " . $row['Date'] . "<br>";//error checking
                    $date = date('D',strtotime($row['Date']));

                    if($date == $checkbox)
                        {
                        echo "MATCHED! Date: " . $date . " Checkbox: " . $checkbox . "<br>";//error checking
                        $sql = ("UPDATE schedule SET Shift_ID = '$off' WHERE Date = '" . $row['Date'] . "' AND Emp_ID = '$emp_id'");

                        if(!$result_update_offdays_query = $mysqli->query($sql))
                            {
                            echo "INSERT ERROR 2 HERE";
                            }
                            echo "UPDATE DAYS OFF SQL: " . $sql . "<br><br>";//error checking
                        }
                    else
                        {
                        echo "NOT MATCHED! Date: " . $date . " Checkbox: " . $checkbox . "<br>";//error checking
                        $sql = ("UPDATE schedule SET Shift_ID = '$shift' WHERE Date = '" . $row['Date'] . "' AND Emp_ID = '$emp_id'");

                        if(!$result_update_shift_query = $mysqli->query($sql))
                            {
                            echo "INSERT ERROR 3 HERE";
                            }
                            echo "UPDATE SHIFT SQL: " . $sql . "<br><br>";//error checking
                        }               
                    }
                }       
            }
        }

When I look at my printed echo statements, everything is perfect! When a date lands on a Friday, it shows that it’s entering Shift_ID = "6" and any other day shows Shift_ID = "3". The problem is the tables don’t reflect what my statements are telling me, they all just get updated to Shift_ID = "3".

In the end I want to be able to select more than one offdays, but when I select more than one, it overwrites my previous day during the next loop, which makes sense.

I’ve got no idea what is going on, if anyone can give me a clue, I would really appreciate it.

UPDATE: When I add exit; after the days off update, like this:

echo "UPDATE DAYS OFF SQL: " . $sql . "<br><br>";//error
exit;

it does update the day off to Shift_ID = "6", so it must be something happening after that.

EDIT: It appears as though the problem was with user permissions. I can’t explain why, but after deleting the user and creating a new one with full permissions, things started to work. I chose @andho’s answer as he helped me get to the answer in the chat forum and also added a way to clean up my code.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T03:30:27+00:00Added an answer on June 15, 2026 at 3:30 am

    This doesn’t solve your issue, but simplifies the solution!

    if your offdays variable is as follows: $offdays = array('Fri', 'Sun');

    You can first set all dates in range to $shift in one query:

    UPDATE Schedule SET Shift_ID = '$shift' WHERE Date BETWEEN '$from' AND '$to' AND Emp_ID = '$emp_id'
    

    Then you can loop through the foreach ($offdays as $offday) { and update those offdays:

    UPDATE Schedule SET Shift_ID = '$off' WHERE Date BETWEEN '$from' AND '$to' AND Emp_ID = '$emp_id' AND DATE_FORMAT(Date, '%a') = '$offday'
    

    That should update all the $offday's in the range to $off.

    This will reduce your loops and queries, which gives leaner code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a small JavaScript validation script that validates inputs based on Regex. I
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to create an if statement in PHP that prevents a single post
I am trying to loop through a bunch of documents I have to put
I am trying to understand how to use SyndicationItem to display feed which is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.