Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [MySQL][PHP]UPDATE w php
Forum PHP.pl > Forum > Przedszkole
przemokrosno
Witam
Mam problem z uaktualnieniem danych z bazy MySql. Napisałem skrypt który pobiera dane z bazy mysql ale ich nie uaktualnia:
  1. <?php
  2.  
  3. ini_set('display_errors', 1);
  4.  
  5. include '../includes/common.inc.php';
  6. include '../includes/klasy.inc.php';
  7. include '../includes/students.inc.php';
  8. include '../includes/plan.inc.php';
  9. include '../includes/przedmioty.inc.php';
  10.  
  11. require_group(_TEACHER_GROUP);
  12.  
  13. extract($_POST);
  14. page_header();
  15. page_menu();
  16.  
  17. $classid=$_REQUEST['classid'];
  18. $przedmiotid=$_REQUEST['przedmiotid'];
  19.  
  20. if (!isset($_REQUEST['classid'])) {
  21. $classid = intval($_REQUEST['classid']);
  22. $przedmiotid = intval($_REQUEST['przedmiotid']);
  23. $classinfo = classGetClassById($classid);
  24. $przedmiotinfo = przedmiotGetPrzedmiotById(intval($przedmiotid));
  25. } else {
  26. $classid = intval($_REQUEST['classid']);
  27. $classinfo = classGetClassById($classid);
  28. $przedmiotid = intval($_REQUEST['przedmiotid']);
  29. $przedmiotinfo = przedmiotGetPrzedmiotById(intval($przedmiotid));
  30. }
  31.  
  32. // Wstawinie ocen ------------------------------------------------------------------------
  33. ?>
  34. <div id="content">
  35. <h2>Edycja ocen klasy <?=$classinfo['name']?>, z przedmiotu <?php echo $przedmiotinfo['lessonname']; ?></h2><div id="breadcrumb"><a href="<?=$conf_settings['site_url']?>">Strona główna</a> -> <a href="/oceny/">Oceny</a> -> <em>Edycja ocen</em></div>
  36.  
  37. <?php if ($success) echo "<strong class=\"success\">$success</strong>"; ?>
  38.  
  39. <div style="clear: both;"></div>
  40.  
  41. <?php
  42. $studentlist = classGetStudentsById($classid);
  43.  
  44. if (empty($studentlist)) {
  45. ?>
  46. Aktualnie nie ma uczniów w tej klasie. Aby zarządzać ocenami musisz <a href="/klasy/addstudent.php">dodać uczniów do tej klasy</a>.
  47. <?php
  48. } else {
  49. ?>
  50.  
  51. <form method="post" action="change.php">
  52. <table cellpadding="5" class="classlist">
  53. <tr><th rowspan="2">ID</th><th rowspan="2">Nazwisko</th><th rowspan="2">Imię</th><th colspan="11">Oceny</th></tr>
  54. <tr>
  55. <?php
  56. for($j=0; $j < 11; $j++) {
  57. echo "<th>";
  58. $n=$j+1;
  59. echo $n.'</th>';
  60. }
  61. echo '</tr>';
  62. $i = 0;
  63. foreach ($studentlist as $student)
  64. {
  65. $i++;
  66. $studentinfo = studentGetStudentById(intval($student['studentid']));
  67. if ($i % 2)
  68. echo '<tr class="even">';
  69. else
  70. echo '<tr class="odd">';
  71. echo '<td>'. $student['studentid'] .'</td>';
  72. echo '<td>' . $studentinfo['lastname'] . '</td>';
  73. echo '<td>' . $studentinfo['firstname'] . '</td>';
  74.  
  75. $query='SELECT marks FROM '._MARKS_DB_TABLE.' WHERE `studentid`='.$student['studentid'].' AND przedmiotid='.$przedmiotid.' LIMIT 1';
  76. $result = @mysql_query($query) or die(mysql_error());
  77. $row = mysql_fetch_array($result);
  78. var_dump($row);
  79.  
  80. $marks = explode("|", $row['marks']);
  81.  
  82. for($j=0; $j < 11; $j++) {?>
  83. <td><input type="text" name="marks[]" maxlength="2" size="3" class="inputbox" value="<?php if(isset($marks[$j])) echo $marks[$j];?>" ></td>
  84. <? }
  85. // echo '<td><input type="text" name="marks[]" value="'.$marks[$j].'" maxlength="2" size="3" class="inputbox"'; " ></td>
  86. echo '</tr>';
  87. echo "\n";
  88. }
  89. echo '</table>';
  90. ?>
  91. <input type="hidden" name="przedmiotid" value="<?=$przedmiotid?>" />
  92. <input type="hidden" name="classid" value="<?=$classid?>" />
  93. <input type="hidden" name="studentid" value="<?=$studentid?>" />
  94. <input type="hidden" name="marks" value="<?=$marks?>" />
  95. <input type="submit" value="Zapisz" />
  96. </form>
  97. <?php } ?>
  98. </div> <!-- content -->
  99.  
  100. <?php
  101. page_footer();
  102. echo "<pre>".print_r($_POST, true)."</pre>";
  103. ?>

oraz plik change.php
  1. <?php
  2.  
  3. ini_set('display_errors', 1);
  4.  
  5. include '../includes/common.inc.php';
  6. include '../includes/klasy.inc.php';
  7. include '../includes/students.inc.php';
  8.  
  9. require_group(_TEACHER_GROUP);
  10.  
  11. if (isset($_GET['success'])) $success = success('Oceny', $_GET['success']);
  12.  
  13. if (!isset($_POST['classid'], $_POST['przedmiotid'], $_POST['marks'])) { header('Location: /oceny/'); exit; }
  14.  
  15. $classid = intval($_POST['classid']);
  16. $przedmiotid = intval($_POST['przedmiotid']);
  17. $marks = intval($_POST['marks']);
  18.  
  19. if (isset($classid)) {
  20. $studentlist = classGetStudentsById($classid);
  21. foreach($studentlist as $student) {
  22.  
  23. $query_update = "UPDATE `intra_marks` SET `marks` = '".implode('|',$marks)."' WHERE `studentid` = '".$student['studentid']."' AND `przedmiotid` = '".$przedmiotid."' ";
  24. $result_update = @mysql_query($query_update) or die(mysql_error());
  25. }
  26. }
  27. header('Location: /oceny/index.php?success=2');
  28. echo "<pre>".print_r($_POST, true)."</pre>";
  29. ?>

Jeżeli ktoś wie gdzie popełniłem błąd to bardzo proszę o pomoc.
TrevorGryffits
Zamknąłeś $marks w apostrof, czyli PHP go nie interpetuje, a przekazuje dalej jak leci. Innymi słowy - szuka w tablicy zmiennej o indeksie dokładnie $marks, nie wstawia jego wartości.
przemokrosno
To prawda przeoczyłem to, ale po usunięciu $: $marks = explode("|", $row['marks']); nic nie wczytuje z bazy i nie zapisuje.
TrevorGryffits
Zobacz jak wygląda $row przy pomocy var_dump();
przemokrosno
var_dump(); zwraca coś takiego:
array(16)
{
[0]=> string(2) "16" ["id"]=> string(2) "16" [1]=> string(1) "2" ["studentid"]=> string(1) "2" [2]=> string(2) "15" ["przedmiotid"]=> string(2) "15" [3]=> string(0) "" ["marks"]=> string(0) "" [4]=> string(0) "" ["marks2"]=> string(0) "" [5]=> string(0) "" ["endmark1"]=> string(0) "" [6]=> string(0) "" ["endmark2"]=> string(0) "" [7]=> string(0) "" ["date"]=> string(0) "" }

array(16) { [0]=> string(2) "13" ["id"]=> string(2) "13" [1]=> string(1) "1" ["studentid"]=> string(1) "1" [2]=> string(2) "15" ["przedmiotid"]=> string(2) "15" [3]=> string(4) "4|+3" ["marks"]=> string(4) "4|+3" [4]=> string(0) "" ["marks2"]=> string(0) "" [5]=> string(0) "" ["endmark1"]=> string(0) "" [6]=> string(0) "" ["endmark2"]=> string(0) "" [7]=> string(0) "" ["date"]=> string(0) "" }

array(16) { [0]=> string(2) "10" ["id"]=> string(2) "10" [1]=> string(1) "3" ["studentid"]=> string(1) "3" [2]=> string(2) "15" ["przedmiotid"]=> string(2) "15" [3]=> string(0) "" ["marks"]=> string(0) "" [4]=> string(0) "" ["marks2"]=> string(0) "" [5]=> string(0) "" ["endmark1"]=> string(0) "" [6]=> string(0) "" ["endmark2"]=> string(0) "" [7]=> string(0) "" ["date"]=> string(0) ""
}

Jak ograniczę SELECT do marks to daje to:
array(2) { [0]=> string(0) "" ["marks"]=> string(0) "" }
array(2) { [0]=> string(4) "4|+3" ["marks"]=> string(4) "4|+3" }
array(2) { [0]=> string(0) "" ["marks"]=> string(0) "" }

Poprawiłem również indexy z i na j i SELECT działa ale nie UPDATE

Udało mi się uruchomić pobieranie danych z bazy - Proszę jeszcze o pomoc z ich uaktualnianiem. Jeśli 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.