Updating records

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • xelA
    New Member
    • Jan 2008
    • 6

    Updating records

    Thanks, it worked. Another problem:
    Hey folks, where is the bug in this code? When updating the date field, it is updating all the records in the table with the same value (e.g. 2008-01-07) instead of incrementing the day by one in each record.
    $schedule=posit ion_courses($ti me_of_day);
    $x=0;
    $z=0;
    $temp=0;
    //getting the initial date
    $date=set_date( 7,1,2008,true);

    foreach($schedu le as $value)
    {
    $z++;
    if($temp!=$valu e['day'])
    {
    $temp=$value['day'];
    ++$x;

    if($x==5)
    {
    $x=0; //reseting the counter to 0
    }
    }
    $day=$date['day'];
    $month=$date['month'];
    $year=$date['year'];
    $day_name=$date['day_name'];
    $full_date=$yea r.'-'.$month.'-'.$day;
    if ($z!=1) $date=set_date( $date['day'],$date['month'], $date['year'],false);

    // Updating the date field in the table
    $sql="UPDATE roomschedule SET date='$full_dat e'";

    if (!mysql_query($ sql,$con))
    {
    die('Error: ' . mysql_error());
    }
    Any help will be deeply appreciated. alexph@live.com
  • dlite922
    Recognized Expert Top Contributor
    • Dec 2007
    • 1586

    #2
    Be nice to the people who will help you, post your code using the [PHP ] and [/ PHP] around your code, to show us like this:


    [PHP]
    $schedule=posit ion_courses($ti me_of_day);
    $x=0;
    $z=0;
    $temp=0;
    //getting the initial date
    $date=set_date( 7,1,2008,true);

    foreach($schedu le as $value)
    {
    $z++;

    if($temp!=$valu e['day'])
    {
    $temp=$value['day'];

    ++$x;
    if($x==5)
    {
    $x=0; //reseting the counter to 0
    }
    }

    $day=$date['day'];
    $month=$date['month'];
    $year=$date['year'];
    $day_name=$date['day_name'];
    $full_date=$yea r.'-'.$month.'-'.$day;

    if ($z!=1)
    {
    $date=set_date( $date['day'],$date['month'], $date['year'],false);
    }

    // Updating the date field in the table
    $sql="UPDATE roomschedule SET date='$full_dat e'";

    if (!mysql_query($ sql,$con))
    {
    die('Error: ' . mysql_error());
    }
    }// <--- bracket was missing?

    [/PHP]

    now let me read it to see if i can help.....

    Comment

    • dlite922
      Recognized Expert Top Contributor
      • Dec 2007
      • 1586

      #3
      In your Update you don't set which record to update, i.e. no WHERE clause.

      UPDATE tablename SET fieldName = "fieldValue " WHERE $foo = 'bar';

      MySQL by default sets every row to that value.
      Last edited by dlite922; Jan 18 '08, 11:07 AM. Reason: spelling

      Comment

      • xelA
        New Member
        • Jan 2008
        • 6

        #4
        I did that. It is still updating all the records with same value (2008-01-07). No increment in date. What do I do? Please Help...

        Comment

        • Purple
          Recognized Expert Contributor
          • May 2007
          • 404

          #5
          Hi xelA

          Can you post your updated code and ideally don't forget the code tags.

          Purple

          Comment

          • xelA
            New Member
            • Jan 2008
            • 6

            #6
            Originally posted by Purple
            Hi xelA

            Can you post your updated code and ideally don't forget the code tags.

            Purple
            Here is my code.
            (PHP)
            <?php
            require_once 'scheduler.php' ;
            include 'generate_date. php';

            draw_schedule() ;
            function clear_existing_ schedule()
            {
            //deleting existing contents to avoid duplication
            $sql="DELETE FROM roomschedule";
            mysql_query($sq l);
            }
            function sechudle_course s()
            {
            //instantiate a course object
            $course=new course();
            $roomSchecdule= new roomSchedule();
            //getDistinctCour ses
            $courses=$cours e->getDistinctCou rses();

            //deleting existing contents to avoid duplication
            clear_existing_ schedule();
            $start=1;
            foreach($course s as $value)
            {
            $numberOfStuden ts=$course->getNumberOfStu dents($value);
            $year_of_study= $course->getYearOfStudy ($value);
            if($year_of_stu dy%2==1)$mornin g=1;else $morning=0;
            $start=$roomSch ecdule->updateRoomSche dule($value,$st art,$morning,$n umberOfStudents );
            }
            }
            function position_course s($time_of_day)
            {
            $roomSchecdule= new roomSchedule();
            $course=new course();
            $roomSchecdule->sheduleDays($t ime_of_day);
            $days=$roomSche cdule->printDays($tim e_of_day);
            $max=count($day s);
            $array_value=0;
            $courses=$cours e->getDistinctCou rses();
            for($i=0;$i<$ma x;$i++)
            {
            $course_code=$d ays[$i][0];
            $day_number=$da ys[$i][1];
            if($day_number> $array_value)
            {
            $array_value++;
            }
            $numberOfStuden ts=$course->getNumberOfStu dents($course_c ode);
            $class_room=$ro omSchecdule->get_classroom( $course_code);
            $schedule[$i]['day']=$array_value;
            $schedule[$i]['course_code']=$course_code;
            $schedule[$i]['room']= $class_room;
            $schedule[$i]['number_of_stud ents']=$numberOfStude nts;
            }
            return $schedule;

            }
            function draw_schedule()
            {
            sechudle_course s();
            for($i=0;$i<=1; $i++)
            {
            if($i==0)
            {
            $time_of_day='m orning';
            echo '<h4>MORNING SESSIONS (09:00HRS)</h4>';
            }
            else
            {
            $time_of_day='a fternoon';
            echo '<h4>AFTERNOON SESSIONS (14:00 HRS)</h4>';
            }

            $schedule=posit ion_courses($ti me_of_day);
            $x=0;
            $z=0;
            $temp=0; //a temporary variable for checking if the value of the date is greater or less
            echo '<table border="1">';
            echo '<tr><th>Day</th><th>Course Code</th><th>Venue</th><th>No of
            Students</th><th>Date</th></tr>';

            //get the starting date
            $date=set_date( 7,1,2008,true);
            foreach($schedu le as $value)
            {
            $z++;
            if($temp!=$valu e['day'])
            {
            $temp=$value['day'];
            ++$x;
            if($x==5)
            {
            $x=0; //reseting the counter to 0
            }
            }
            $day=$date['day'];
            $month=$date['month'];
            $year=$date['year'];
            $day_name=$date['day_name'];
            $full_date=$yea r.'-'.$month.'-'.$day;
            if ($z!=1) $date=set_date( $date['day'],$date['month'], $date['year'],false);
            $sql="UPDATE roomschedule SET date='$full_dat e' WHERE date='$full_dat e'";
            mysql_query($sq l);

            echo'<tr><td>'. $day_name.'</td><td>'.$value['course_code'].'</td><td>'.$value['room'].'</td><td>'.$value['number_of_stud ents'].'</td><td>'.$full_ date.'</td></tr>';
            }
            echo '</table>';
            }
            }
            ?>

            Comment

            • dlite922
              Recognized Expert Top Contributor
              • Dec 2007
              • 1586

              #7
              Originally posted by xelA
              Here is my code.
              (PHP)
              <?php
              require_once 'scheduler.php' ;
              include 'generate_date. php';

              draw_schedule() ;
              function clear_existing_ schedule()
              {
              //deleting existing contents to avoid duplication
              $sql="DELETE FROM roomschedule";
              mysql_query($sq l);
              }
              function sechudle_course s()
              {
              //instantiate a course object
              $course=new course();
              $roomSchecdule= new roomSchedule();
              //getDistinctCour ses
              $courses=$cours e->getDistinctCou rses();

              //deleting existing contents to avoid duplication
              clear_existing_ schedule();
              $start=1;
              foreach($course s as $value)
              {
              $numberOfStuden ts=$course->getNumberOfStu dents($value);
              $year_of_study= $course->getYearOfStudy ($value);
              if($year_of_stu dy%2==1)$mornin g=1;else $morning=0;
              $start=$roomSch ecdule->updateRoomSche dule($value,$st art,$morning,$n umberOfStudents );
              }
              }
              function position_course s($time_of_day)
              {
              $roomSchecdule= new roomSchedule();
              $course=new course();
              $roomSchecdule->sheduleDays($t ime_of_day);
              $days=$roomSche cdule->printDays($tim e_of_day);
              $max=count($day s);
              $array_value=0;
              $courses=$cours e->getDistinctCou rses();
              for($i=0;$i<$ma x;$i++)
              {
              $course_code=$d ays[$i][0];
              $day_number=$da ys[$i][1];
              if($day_number> $array_value)
              {
              $array_value++;
              }
              $numberOfStuden ts=$course->getNumberOfStu dents($course_c ode);
              $class_room=$ro omSchecdule->get_classroom( $course_code);
              $schedule[$i]['day']=$array_value;
              $schedule[$i]['course_code']=$course_code;
              $schedule[$i]['room']= $class_room;
              $schedule[$i]['number_of_stud ents']=$numberOfStude nts;
              }
              return $schedule;

              }
              function draw_schedule()
              {
              sechudle_course s();
              for($i=0;$i<=1; $i++)
              {
              if($i==0)
              {
              $time_of_day='m orning';
              echo '<h4>MORNING SESSIONS (09:00HRS)</h4>';
              }
              else
              {
              $time_of_day='a fternoon';
              echo '<h4>AFTERNOON SESSIONS (14:00 HRS)</h4>';
              }

              $schedule=posit ion_courses($ti me_of_day);
              $x=0;
              $z=0;
              $temp=0; //a temporary variable for checking if the value of the date is greater or less
              echo '<table border="1">';
              echo '<tr><th>Day</th><th>Course Code</th><th>Venue</th><th>No of
              Students</th><th>Date</th></tr>';

              //get the starting date
              $date=set_date( 7,1,2008,true);
              foreach($schedu le as $value)
              {
              $z++;
              if($temp!=$valu e['day'])
              {
              $temp=$value['day'];
              ++$x;
              if($x==5)
              {
              $x=0; //reseting the counter to 0
              }
              }
              $day=$date['day'];
              $month=$date['month'];
              $year=$date['year'];
              $day_name=$date['day_name'];
              $full_date=$yea r.'-'.$month.'-'.$day;
              if ($z!=1) $date=set_date( $date['day'],$date['month'], $date['year'],false);
              $sql="UPDATE roomschedule SET date='$full_dat e' WHERE date='$full_dat e'";
              mysql_query($sq l);

              echo'<tr><td>'. $day_name.'</td><td>'.$value['course_code'].'</td><td>'.$value['room'].'</td><td>'.$value['number_of_stud ents'].'</td><td>'.$full_ date.'</td></tr>';
              }
              echo '</table>';
              }
              }
              ?>
              before mysql_query($sq l), write

              die($sql);

              paste this into mysql command line (if the error is not obvious by looking at it) and mysql will show you that query is executing the way it is.

              Comment

              • xelA
                New Member
                • Jan 2008
                • 6

                #8
                Please don't be annoyed with me. I just need your help. After doing that, it is giving me the following error msg:

                UPDATE roomschedule SET date='2008-01-07' WHERE date='2008-01-07'

                If I echo full_date, it is showing me want I anticipate. The problem comes when entering the date in the table. It is failing to update the date field.

                Comment

                • Markus
                  Recognized Expert Expert
                  • Jun 2007
                  • 6092

                  #9

                  Please read the forum guidelines


                  And post the code again with the proper code tags.

                  Comment

                  • xelA
                    New Member
                    • Jan 2008
                    • 6

                    #10
                    Originally posted by markusn00b

                    Please read the forum guidelines


                    And post the code again with the proper code tags.
                    Thanks for your concern. But why should I post my code again?

                    Comment

                    • Markus
                      Recognized Expert Expert
                      • Jun 2007
                      • 6092

                      #11
                      Because people will just neglect to read your code if you haven't used code tags. Therefore, you not getting an answer.

                      Until a mod (few and far between) changed it, you'll have to repost it.

                      Or not.

                      Just remember to use proper code tags :)

                      Comment

                      • dlite922
                        Recognized Expert Top Contributor
                        • Dec 2007
                        • 1586

                        #12
                        Oh for crying out loud:

                        [PHP]
                        <?php
                        require_once 'scheduler.php' ;
                        include 'generate_date. php';

                        draw_schedule() ;
                        function clear_existing_ schedule()
                        {
                        //deleting existing contents to avoid duplication
                        $sql="DELETE FROM roomschedule";
                        mysql_query($sq l);
                        }
                        function sechudle_course s()
                        {
                        //instantiate a course object
                        $course=new course();
                        $roomSchecdule= new roomSchedule();
                        //getDistinctCour ses
                        $courses=$cours e->getDistinctCou rses();

                        //deleting existing contents to avoid duplication
                        clear_existing_ schedule();
                        $start=1;
                        foreach($course s as $value)
                        {
                        $numberOfStuden ts=$course->getNumberOfStu dents($value);
                        $year_of_study= $course->getYearOfStudy ($value);
                        if($year_of_stu dy%2==1)$mornin g=1;else $morning=0;
                        $start=$roomSch ecdule->updateRoomSche dule($value,$st art,$morning,$n umberO fStudents);
                        }
                        }
                        function position_course s($time_of_day)
                        {
                        $roomSchecdule= new roomSchedule();
                        $course=new course();
                        $roomSchecdule->sheduleDays($t ime_of_day);
                        $days=$roomSche cdule->printDays($tim e_of_day);
                        $max=count($day s);
                        $array_value=0;
                        $courses=$cours e->getDistinctCou rses();
                        for($i=0;$i<$ma x;$i++)
                        {
                        $course_code=$d ays[$i][0];
                        $day_number=$da ys[$i][1];
                        if($day_number> $array_value)
                        {
                        $array_value++;
                        }
                        $numberOfStuden ts=$course->getNumberOfStu dents($course_c ode);
                        $class_room=$ro omSchecdule->get_classroom( $course_code);
                        $schedule[$i]['day']=$array_value;
                        $schedule[$i]['course_code']=$course_code;
                        $schedule[$i]['room']= $class_room;
                        $schedule[$i]['number_of_stud ents']=$numberOfStude nts;
                        }
                        return $schedule;

                        }
                        function draw_schedule()
                        {
                        sechudle_course s();
                        for($i=0;$i<=1; $i++)
                        {
                        if($i==0)
                        {
                        $time_of_day='m orning';
                        echo '<h4>MORNING SESSIONS (09:00HRS)</h4>';
                        }
                        else
                        {
                        $time_of_day='a fternoon';
                        echo '<h4>AFTERNOON SESSIONS (14:00 HRS)</h4>';
                        }

                        $schedule=posit ion_courses($ti me_of_day);
                        $x=0;
                        $z=0;
                        $temp=0; //a temporary variable for checking if the value of the date is greater or less
                        echo '<table border="1">';
                        echo '<tr><th>Day</th><th>Course Code</th><th>Venue</th><th>No of
                        Students</th><th>Date</th></tr>';

                        //get the starting date
                        $date=set_date( 7,1,2008,true);
                        foreach($schedu le as $value)
                        {
                        $z++;
                        if($temp!=$valu e['day'])
                        {
                        $temp=$value['day'];
                        ++$x;
                        if($x==5)
                        {
                        $x=0; //reseting the counter to 0
                        }
                        }
                        $day=$date['day'];
                        $month=$date['month'];
                        $year=$date['year'];
                        $day_name=$date['day_name'];
                        $full_date=$yea r.'-'.$month.'-'.$day;
                        if ($z!=1) $date=set_date( $date['day'],$date['month'], $date['year'],false);
                        $sql="UPDATE roomschedule SET date='$full_dat e' WHERE date='$full_dat e'";
                        mysql_query($sq l);

                        echo'<tr><td>'. $day_name.'</td><td>'.$value['course_code'].'</td><td>'.$value['room'].'</td><td>'.$value['number_of_stud ents'].'</td><td>'.$full_ date.'</td></tr>';
                        }
                        echo '</table>';
                        }
                        }
                        ?>


                        [/PHP]

                        Comment

                        Working...