Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Funkcja foreach i przekazywane danych do mysql
Forum PHP.pl > Forum > PHP
przemokrosno
Witam
Napisałem skrypt który zwraca zwraca sukces ale mimo wszystko nic się nie wykonuje tzn nie dodaje rekordów do bazy danych. Jeśli dodam je ręcznie to wyświetla prawidłowo. Jeśli zamienię funkcję INSERT na DELETE to również działa bez problemu
Męczy mnie to już trzeci dzień i nie mogę znaleźć żadnego błędu. Dlatego bardzo proszę o pomoc Jeśli ktoś potrafi wskazać mi błąd to będę bardzo wdzięczny.
  •  if (!isset($_POST['class'], $_POST['toyear'], $_POST['tomonth'], $_POST['today'])) {
  •      if (!isset($_GET['id']))  
  •     exit;
  •  }
  •  
  •  page_header();
  •  page_menu();
  •  
  •  if (!isset($_GET['id'])) {
  •  
  •      $absentdate = $_POST['toyear'] . '-' . $_POST['tomonth'] . '-' . $_POST['today'];
  •      
  •      $classid = intval($_POST['class']);
  •      $classinfo = classGetClassById($classid);
  •  } else {
  •      $absentdate = date('Y-m-d');
  •      $classid = intval($_GET['id']);
  •      $classinfo = classGetClassById($classid);
  •  }
  •  
  •  $confquery = 'SELECT * FROM `CONFIG_DB_TABLE` WHERE `id`=1 ';
  •  $confresult = mysql_query($confquery) or die(mysql_error());
  •  $confrow = mysql_fetch_array($confresult);
  •  $max=$confrow['lessonmax'];
  •  
  • // Wstawinie nieobecności ------------------------------------------------------------------------
  •  if ($_POST['todo'] == 'Wstaw') {
  • ?>
  • <div id="content">
  • <h2>Wstawianie nieobecności w klasie <?=$classinfo['name']?>, <?php echo $absentdate; ?></h2>
  • <div id="breadcrumb"><a href="<?=$conf_settings['site_url']?>">Strona główna</a> -> <a href="/frekwencja/">Frekwencja</a> -> <em>Wstawianie nieobecności</em></div>
  •  
  • <?php if ($success) echo "<strong class="success">$success</strong>"; ?>
  •  
  • <div id="subnav">
  • <ul>
  •  <li>
  •   <form method="post" action="do.php" class="search">
  •    <label for="toyear">
  •     <img src="/images/options/search.png"/>
  •     &nbsp;Przejdź do:
  •    </label>
  •   <?php dateboxes(true, false); ?>
  •   <label for="class">
  •    Klasy:
  •   </label>
  •   <?php classSelectBox(); ?>
  •    <input type="submit" name="submit" value="Przejdź" />
  •   </form>
  •  </li>
  • </ul>
  • </div>
  • <div style="clear: both;"></div>
  •  
  • <p>Umieść znacznik obok każdego nieobecnego ucznia.</p>
  •  
  • <?php
  • $studentlist = classGetStudentsById($classid);
  •  
  • if (empty($studentlist)) {
  • ?>
  • W tej klasie nie ma wpisanych uczniów.  Aby zarządzać frekwencją, musisz <a href="/klasy/dodajstudent.php">dodać uczniów do tej klasy</a>.
  • <?php
  • } else {
  • ?>
  •  
  • <form method="post" action="wstaw.php">
  • <table cellpadding="5" class="classlist">
  • <tr><th rowspan="2">ID ucznia</th><th rowspan="2">Imię</th><th rowspan="2">Nazwisko</th><th rowspan="2">Nieobecny?</th><th colspan="<?=$max?>">Lekcje</th></tr>
  •  <tr>  <?php
  •  for($j=0; $j < $max; $j++) {
  •   echo "<th>";
  •   $n=$j+1;  
  •   echo $n.'</th>';
  •    }
  •    echo '</tr>';
  •    $i = 0;
  •    foreach ($studentlist as $student)
  •    {
  •      $i++;
  •      $studentinfo = studentGetStudentById(intval($student['studentid']));
  •      if ($i % 2)
  •       echo '<tr class="even">';
  •      else
  •       echo '<tr class="odd">';
  •      
  •      echo '<td>'. $student['studentid'] .'</td>';
  •      echo '<td>' . $studentinfo['firstname'] . '</td>';
  •      echo '<td>' . $studentinfo['lastname'] . '</td>';
  •      
  •      $absent = getStudentAttendance($absentdate,  $classid, intval($student['studentid']));
  •      $lesson = explode('|',$_POST['lesson']);
  •      if ($absent == true) {
  •            $absvalue = 'checked="checked" ';
  •      }
  •      
  •      echo '<td><input type="checkbox" name="student['.$student['studentid'].']" '.$absvalue.'/></td>';
  •      
  •   for($j=0; $j < $max; $j++) {
  •      if ($lesson[$j] == 1){
  •   $lesson_value[$j] = 'checked="checked"';
  •   }  ?>
  •    
  •   <td><input type="checkbox" name="<?php if(isset($lesson[$j])) echo $lesson[$j];?>" <?php echo $lesson_value[$j];?> ></td>
  •    <?php  
  •   if ($lesson[$j] == true){
  •   $lesson[$j] = 1;
  •   } else {
  •   $lesson[$j] = 0;
  •   }
  •   }
  •      echo '</tr>';
  •      echo "n";
  •    }
  •    echo '</table>';
  • }
  • ?>
  • <input type="hidden" name="absentdate" value="<?=$absentdate?>" />
  • <input type="hidden" name="classid" value="<?=$classid?>" />
  • <input type="hidden" name="classid" value="<?=$studentid?>" />
  • <input type="hidden" name="lesson" value="<?=$lesson = implode('|',$_POST['lesson'])?>" />
  • <input type="submit" name="todo" value="Wstaw" />
  •  
  • <?php
  •  // Koniec wstawiania nieobecności -------------------------------------------------------------------
  •  page_footer();
  • ?>

    [/list]
  •  define('_ATTENDANCE_DB_TABLE', $conf_settings['db_tblprefix'] . 'attendance');
  •  
  •  if (!isset($_POST['class'], $_POST['toyear'], $_POST['tomonth'], $_POST['today'])) {
  •      if (!isset($_GET['id']))  
  •     exit;
  •  }
  •  
  •  page_header();
  •  page_menu();
  •  
  •  if (!isset($_GET['id'])) {
  •  
  •      $absentdate = $_POST['toyear'] . '-' . $_POST['tomonth'] . '-' . $_POST['today'];
  •      
  •      $classid = intval($_POST['class']);
  •      $classinfo = classGetClassById($classid);
  •  } else {
  •      $absentdate = date('Y-m-d');
  •      $classid = intval($_GET['id']);
  •      $classinfo = classGetClassById($classid);
  •  }
  •  
  •  $confquery = 'SELECT * FROM `CONFIG_DB_TABLE` WHERE `id`=1 ';
  •  $confresult = mysql_query($confquery) or die(mysql_error());
  •  $confrow = mysql_fetch_array($confresult);
  •  $max=$confrow['lessonmax'];
  •  
  • // Wstawinie nieobecności ------------------------------------------------------------------------
  •  if ($_POST['todo'] == 'Wstaw') {
  • ?>
  • <div id="content">
  • <h2>Wstawianie nieobecności w klasie <?=$classinfo['name']?>, <?php echo $absentdate; ?></h2>
  • <div id="breadcrumb"><a href="<?=$conf_settings['site_url']?>">Strona główna</a> -> <a href="/frekwencja/">Frekwencja</a> -> <em>Wstawianie nieobecności</em></div>
  •  
  • <?php if ($success) echo "<strong class="success">$success</strong>"; ?>
  •  
  • <div id="subnav">
  • <ul>
  •  <li>
  •   <form method="post" action="do.php" class="search">
  •    <label for="toyear">
  •     <img src="/images/options/search.png"/>
  •     &nbsp;Przejdź do:
  •    </label>
  •   <?php dateboxes(true, false); ?>
  •   <label for="class">
  •    Klasy:
  •   </label>
  •   <?php classSelectBox(); ?>
  •    <input type="submit" name="submit" value="Przejdź" />
  •   </form>
  •  </li>
  • </ul>
  • </div>
  • <div style="clear: both;"></div>
  •  
  • <p>Umieść znacznik obok każdego nieobecnego ucznia.</p>
  •  
  • <?php
  • $studentlist = classGetStudentsById($classid);
  •  
  • if (empty($studentlist)) {
  • ?>
  • W tej klasie nie ma wpisanych uczniów.  Aby zarządzać frekwencją, musisz <a href="/klasy/dodajstudent.php">dodać uczniów do tej klasy</a>.
  • <?php
  • } else {
  • ?>
  •  
  • <form method="post" action="wstaw.php">
  • <table cellpadding="5" class="classlist">
  • <tr><th rowspan="2">ID ucznia</th><th rowspan="2">Imię</th><th rowspan="2">Nazwisko</th><th rowspan="2">Nieobecny?</th><th colspan="<?=$max?>">Lekcje</th></tr>
  •  <tr>  <?php
  •  for($j=0; $j < $max; $j++) {
  •   echo "<th>";
  •   $n=$j+1;  
  •   echo $n.'</th>';
  •    }
  •    echo '</tr>';
  •    $i = 0;
  •    foreach ($studentlist as $student)
  •    {
  •      $i++;
  •      $studentinfo = studentGetStudentById(intval($student['studentid']));
  •      if ($i % 2)
  •       echo '<tr class="even">';
  •      else
  •       echo '<tr class="odd">';
  •      
  •      echo '<td>'. $student['studentid'] .'</td>';
  •      echo '<td>' . $studentinfo['firstname'] . '</td>';
  •      echo '<td>' . $studentinfo['lastname'] . '</td>';
  •      
  •      $absent = getStudentAttendance($absentdate,  $classid, intval($student['studentid']));
  •      $lesson = explode('|',$_POST['lesson']);
  •      if ($absent == true) {
  •            $absvalue = 'checked="checked" ';
  •      }
  •      
  •      echo '<td><input type="checkbox" name="student['.$student['studentid'].']" '.$absvalue.'/></td>';
  •      
  •   for($j=0; $j < $max; $j++) {
  •      if ($lesson[$j] == 1){
  •   $lesson_value[$j] = 'checked="checked"';
  •   }  ?>
  •    
  •   <td><input type="checkbox" name="<?php if(isset($lesson[$j])) echo $lesson[$j];?>" <?php echo $lesson_value[$j];?> ></td>
  •    <?php  
  •   if ($lesson[$j] == true){
  •   $lesson[$j] = 1;
  •   } else {
  •   $lesson[$j] = 0;
  •   }
  •   }
  •      echo '</tr>';
  •      echo "n";
  •    }
  •    echo '</table>';
  • }
  • ?>
  • <input type="hidden" name="absentdate" value="<?=$absentdate?>" />
  • <input type="hidden" name="classid" value="<?=$classid?>" />
  • <input type="hidden" name="classid" value="<?=$studentid?>" />
  • <input type="hidden" name="lesson" value="<?=$lesson = implode('|',$_POST['lesson'])?>" />
  • <input type="submit" name="todo" value="Wstaw" />
  •  
  • <?php
  •  // Koniec wstawiania nieobecności -------------------------------------------------------------------
  •  page_footer();
  • ?>

    [/list]
    [PHP] );
  •  if (!isset($_POST['classid'], $_POST['absentdate'])) {
  •     header('Location: /frekwencja/');
  •     exit;
  •  }
  •  
  •  $date = $_POST['absentdate'];
  •  $classid = intval($_POST['classid']);
  •  
  •  if (isset($_POST['student'])) {
  •  
  •     foreach ($student as $studentid => $status)
  •     {
  •   $query = 'INSERT INTO `intra_attendance` (`classid`, `studentid`, `date_absent`) VALUES ('.$classid.', '.$studentid.', '.$date.');';
  •   $result = @mysql_query($query) or die(mysql_error());
  •   return mysql_insert_id();
  •     }
  •  
  •  }
  •  
  •  header('Location: /frekwencja/index.php?success=1');
  •  exit;
  • ?>

    [/list]
  •  if (!isset($_POST['classid'], $_POST['absentdate'])) {
  •     header('Location: /frekwencja/');
  •     exit;
  •  }
  •  
  •  $date = $_POST['absentdate'];
  •  $classid = intval($_POST['classid']);
  •  
  •  if (isset($_POST['student'])) {
  •  
  •     foreach ($student as $studentid => $status)
  •     {
  •   $query = 'INSERT INTO `intra_attendance` (`classid`, `studentid`, `date_absent`) VALUES ('.$classid.', '.$studentid.', '.$date.');';
  •   $result = @mysql_query($query) or die(mysql_error());
  •   return mysql_insert_id();
  •     }
  •  
  •  }
  •  
  •  header('Location: /frekwencja/index.php?success=1');
  •  exit;
  • ?>

    [/list]
    chomiczek
    a jak tak trochę OT..
    masz coś takiego:
    1. <?php
    2. include '../includes/common.inc';
    3. include '../includes/klasy.inc';
    4. include '../includes/students.inc';
    5. include '../includes/frekwencja.inc';
    6. ?>


    jeżeli masz ustawione w apachu, że tego pliku nikt nie zobaczy to ok, ale spróbuj wkleić w URLa http://twojserwer.pl/includes/common.inc
    jeżeli masz tam jakieś hasła to zobaczysz je na ekranie.

    Ja osobiście jeżeli już stosuje .inc to zawsze nazwaPLiku.inc.php i mam 100% pewność, że jeśli tylko php działa to nikt nic nie zobaczy biggrin.gif

    proponowałbym Ci jeszcze używać zawsze tego samego 'otwarcia PHP'.. Ty raz piszesz <?PHP a innym razem <?

    ale to taki mały OT
    przemokrosno
    Jak się okazało wszystkiemu winna była linia:

    1. <?php
    2. $return mysql_insert_id();
    3. ?>

    Po jej wyrzuceniu wszystko działa ok poza małym szczegółem.
    Dopracowałem ten skrypt który operuje na tablicy attendance (frekwencja) w bazie mysql w postaci:
    ---------------------------------------------------------------- --
    | id | studentid | classid | lessons | date_absent |
    ---------------------------------------------------------------- --
    Do pola lessons powinien wstawić wartości pobrane z formularza (inputbox'ów) dla każdego studenta.

    1. <?php
    2.  include '../includes/common.inc';
    3.  include '../includes/klasy.inc';
    4.  include '../includes/students.inc';
    5.  include '../includes/frekwencja.inc';
    6.  
    7.  require_group(_TEACHER_GROUP);
    8.  define('_ATTENDANCE_DB_TABLE', $conf_settings['db_tblprefix'] . 'attendance');
    9.  
    10.  if (isset($_GET['success'])) $success = success('Frekwencja', $_GET['success']);
    11.  
    12.  if (!isset($_POST['class'], $_POST['toyear'], $_POST['tomonth'], $_POST['today'])) {
    13.      if (!isset($_GET['id']))       exit;  }
    14.  
    15.  page_header();
    16.  page_menu();
    17.  
    18.  if (!isset($_GET['id'])) {
    19.  
    20.      $absentdate = $_POST['toyear'] . '-' . $_POST['tomonth'] . '-' . $_POST['today'];
    21.      
    22.      $classid = intval($_POST['class']);
    23.      $classinfo = classGetClassById($classid);
    24.  } else {
    25.      $absentdate = date('Y-m-d');
    26.      $classid = intval($_GET['id']);
    27.  
    28.      $classinfo = classGetClassById($classid);
    29.  }
    30.  
    31.  $confquery = 'SELECT * FROM `intra_config` WHERE `id`=1 ';
    32.  $confresult = mysql_query($confquery) or die(mysql_error());
    33.  $confrow = mysql_fetch_array($confresult);
    34.  $max=$confrow['lessonmax'];
    35.  
    36. // Wstawinie nieobecności ------------------------------------------------------------------------
    37.  if ($_POST['todo'] == 'Wstaw') {
    38. ?>
    39. <div id="content">
    40. <h2>Wstawianie nieobecności w klasie <?=$classinfo['name']?>, <?php echo $absentdate; ?></h2>
    41. <div id="breadcrumb"><a href="<?=$conf_settings['site_url']?>">Strona główna</a> -> <a href="/frekwencja/">Frekwencja</a> -> <em>Wstawianie nieobecności</em></div>
    42.  
    43. <?php if ($success) echo "<strong class=\"success\">$success</strong>"; ?>
    44.  
    45. <div id="subnav">
    46. <ul>
    47.  <li>
    48.   <form method="post" action="wstaw.php" class="search">
    49.    <label for="toyear">
    50.     <img src="/images/options/search.png"/>
    51.     &nbsp;Przejdź do:
    52.    </label>
    53.   <?php dateboxes(true, false); ?>
    54.   <label for="class">
    55.    Klasy:
    56.   </label>
    57.   <?php classSelectBox(); ?>
    58.    <input type="submit" name="submit" value="Przejdź" />
    59.   </form>
    60.  </li>
    61. </ul>
    62. </div>
    63. <div style="clear: both;"></div>
    64.  
    65. <p>Umieść znacznik obok każdego nieobecnego ucznia.</p>
    66.  
    67. <?php
    68. $studentlist = classGetStudentsById($classid);
    69.  
    70. if (empty($studentlist)) {
    71. ?>
    72. W tej klasie nie ma wpisanych uczniów.  Aby zarządzać frekwencją, musisz <a href="/klasy/addstudent.php">dodać uczniów do tej klasy</a>.
    73. <?php
    74. } else {
    75. ?>
    76. <form method="post" action="take.php">
    77. <table cellpadding="5" class="classlist">
    78. <tr><th rowspan="2">ID ucznia</th><th rowspan="2">Imię</th><th rowspan="2">Nazwisko</th><th rowspan="2">Nieobecny?</th><th colspan="<?=$max?>">Lekcje</th></tr>
    79.  <tr>  <?php
    80.  for($j=0; $j < $max; $j++) {
    81.   echo "<th>";
    82.   $n=$j+1;  
    83.   echo $n.'</th>';
    84.    }
    85.    echo '</tr>';
    86.    $i = 0;
    87.    foreach ($studentlist as $student)
    88.    {
    89.      $i++;
    90.      $studentinfo = studentGetStudentById(intval($student['studentid']));
    91.      if ($i % 2)
    92.       echo '<tr class="even">';
    93.      else
    94.       echo '<tr class="odd">';
    95.      
    96.      echo '<td>'. $student['studentid'] .'</td>';
    97.      echo '<td>' . $studentinfo['firstname'] . '</td>';
    98.      echo '<td>' . $studentinfo['lastname'] . '</td>';
    99.      echo '<td><input type="checkbox" name="student['.$student['studentid'].']"/></td>';
    100.     for($j=1; $j <= $max; $j++) {
    101.     ?> <td><input type="text" name="lessons[]" maxlength="1" size="1" class="inputbox" value="<?php if(isset($lessons[$j])) echo $lessons[$j];?>" /></td>
    102.      <?php }
    103.  
    104.      echo '</tr>';
    105.      echo "\n";
    106.    }
    107.    echo '</table>';
    108. }
    109. ?>
    110. <input type="hidden" name="absentdate" value="<?=$absentdate?>" />
    111. <input type="hidden" name="classid" value="<?=$classid?>" />
    112. <input type="hidden" name="studentid" value="<?=$studentid?>" />
    113. <input type="submit" name="todo" value="Wstaw" />
    114. </form>
    115. <!-- Koniec wstawiania nieobecności -->
    116. </div> <!-- content -->
    117. <?php
    118.  page_footer();
    119. ?>



    Kod pliku wstaw.php:
    1. <?php
    2. include '../includes/common.inc';
    3. include '../includes/klasy.inc';
    4. include '../includes/students.inc';
    5. include '../includes/frekwencja.inc';
    6.  
    7. require_group(_TEACHER_GROUP);
    8.  
    9. if (!isset($_POST['classid'], $_POST['absentdate'])) {    header('Location: /frekwencja/');   exit;   }
    10.  
    11. $date = $_POST['absentdate'];
    12. $classid = intval($_POST['classid']);
    13.  
    14. if (isset($_POST['student'])) {
    15.   foreach ($_POST['student'] as $studentid => $status)  {
    16.     $query = "INSERT INTO `intra_attendance` VALUES ('', '".$classid."', '".$studentid."', '". $lessons = implode("|",$_POST['lessons'])."', '".$date."')";
    17.      $result = @mysql_query($query) or die(mysql_error());
    18.   }
    19. }
    20. header('Location: /frekwencja/index.php?success=1');
    21. ?>


    Który dla każdego nieobecnego studenta powinien pobrać wartości lessons jako nieobecności. I wszystko jest w porządku (wszystko działa i poprawnie przesyła) jeśli dodaję wartości nieobecności tylko dla jednego studenta. w przypadku kilku studentów łączy wszystkie nieobecności w jednym polu i dla każdego dodaje te same wartości do bazy danych czyli od pierwszego studenta do ostatniego zaznaczonego jako nieobecny.
    Implode służy do połączenia inputboxów w jeden rekord, które są pobierane dla maksymalnej liczby lekcji ($max)czyli dla każdej lekcji jest osobny inputbox np. dla $max = 10 lekcji więc łącze 10 inputboxów.
    Jeżeli ktoś wie gdzie popełniłem błąd to bardzo proszę o pomoc.
  • To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
    Invision Power Board © 2001-2025 Invision Power Services, Inc.