Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [php/mysql] edytowanie rekordow
Forum PHP.pl > Forum > PHP
dirtyhustlaz
skrypt pobiera mi dane z bazy i pokazuje je w formularzu, ale jak chce je zminie i chce zaktualizowac to wywala mi blad:
  1. Notice: Undefined index: id in F:\EasyPHP-12.1\www\glencaldy\editstudent.php on line 33


w tabeli 'student' mam pola: studentid, firstname, surname, username, password. chce zeby moliwa byla edycja wsyzstkich poza studentid.
jak zmienialem w tej 33 lini id na student id to wogole nie pobiera danych i nie wyskakuje ten blad juz na glownej stronie. oto kod mojego skryptu edycji danych. moglby ktos mi pomoc z tym albo dac jakies wskazowki? musze to skonczyc do szkoly w tym tygodniiu


  1. <?php
  2.  
  3. // Connects to your Database
  4.  
  5. mysql_connect("localhost", "root", "") or die(mysql_error());
  6.  
  7. mysql_select_db("glencaldy") or die(mysql_error());
  8.  
  9.  
  10. //checks cookies to make sure they are logged in
  11.  
  12. if (isset($_COOKIE['ID_my_site'])) {
  13. $username = $_COOKIE['ID_my_site'];
  14.  
  15. $pass = $_COOKIE['Key_my_site'];
  16.  
  17. $check = mysql_query("SELECT * FROM student WHERE username = '$username'") or die(mysql_error());
  18.  
  19. while ($info = mysql_fetch_array($check)) {
  20. //if the cookie has the wrong password, they are taken to the login page
  21.  
  22. if ($pass != $info['password']) {
  23. header("Location: login.php");
  24.  
  25. }
  26.  
  27.  
  28.  
  29. //otherwise they are shown the admin area
  30.  
  31. else {
  32. $a = trim($_REQUEST['a']);
  33. $id = trim($_GET['id']);
  34. if ($a == 'edit' and !empty($id)) {
  35. $result = mysql_query("SELECT * FROM student WHERE
  36. studentid='$id'") or die('Error');
  37.  
  38. if (mysql_num_rows($result) > 0) {
  39. // read content of table
  40. $r = mysql_fetch_assoc($result);
  41. echo "<h1>EDIT STUDENT</h1><p>";
  42. echo '<form action="editstudent.php" method="post">
  43. <input type="hidden" name="a" value="save" />
  44. <input type="hidden" name="studentid" value="' . $id . '" />
  45. First name:<br />
  46. <input type="text" name="firstname"
  47. value="' . $r['firstname'] . '" /><br />
  48. Surname:<br />
  49. <input type="text" name="surname"
  50. value="' . $r['surname'] . '" /><br />
  51. Username:<br />
  52. <input type="text" name="username"
  53. value="' . $r['username'] . '" /><br />
  54. Password:<br />
  55. <input type="text" name="password"
  56. value="' . $r['password'] . '" /><br />
  57. <input type="submit" value="Update Record" />
  58. </form>';
  59. echo "<a href=deletestudent.php><< Back to delete/edit menu</a><p>";
  60. echo "<a href=menu.php><< Back to main menu</a><p>";
  61.  
  62. }
  63.  
  64. }
  65. }
  66.  
  67. }
  68. } elseif ($a == 'save') {
  69. // get the new records from form
  70. $id = $_POST['studentid'];
  71. $firstname = trim($_POST['firstname']);
  72. $surname = trim($_POST['surname']);
  73. $username = trim($_POST['username']);
  74. $password = trim($_POST['password']);
  75. // update table
  76. mysql_query("UPDATE studnet SET firstname='$firstname',
  77. surname='$surname', username='&username', password='$password' WHERE studentid='$id'") or die('Error');
  78. echo 'records updated';
  79. }
  80.  
  81. else
  82. //if the cookie does not exist, they are taken to the login screen
  83. {
  84. header("Location: login.php");
  85.  
  86. }
  87.  
  88. ?>


tutaj wrzucam paczke z calym skryptem: https://dl.dropbox.com/u/63492647/glencaldy_dp.zip
nospor
Komunikat błędu przecież ci wyraźnie mówi co masz źle i w której linijce....
$id = trim($_GET['id']);
W _GET nie masz indeksu ID. Przesyłasz to ID? Przesyłasz GETem czy POSTem?
dirtyhustlaz
ok wiec zamienilem:

  1. $id = trim($_GET['id']);


na

  1. if(isset($_GET['id']))
  2. $id = trim($_GET['id']);
  3. else
  4. $id = '';


teraz nie wyskakuje notice ani zaden blad ale nie zmieniaja sie dane w bazie danych
nospor
Rety.... zadałem proste pytania:
Cytat
Przesyłasz to ID? Przesyłasz GETem czy POSTem?
Odpowiedz na nie bo to one są kluczem do rozwiązanie tej niesamowitej zagadki....
Skoro nie ma ID to niewyświetlanie błędu ci nie rozwiąze problemu, skoro ty polegasz na danych z tego ID....
dirtyhustlaz
przepraszam, postem.
nospor
No to skoro postem wysyłasz to czego szukasz go w $_GET?
dirtyhustlaz
blad poczatkujacego? smile.gif kasowanie rekordow z bazy zrobilem GETem i dziala to dlaczego edycja nie ddziala GETem?
nospor
Rety..... skoro dane wysyłąsz POSTEM to i z POST masz je odbierać a nie z GET.


To tak jakbyś paczkę wysłał Pocztą Polską a następnie kazał czekać odbiorcy na przyjazd DHL.... Myśl troche chłopie
dirtyhustlaz
ok, ide kawe sobie zrobic i juz to zmieniam wink.gif dziekuje za pomoc


nie dziala dalej, mam teraz pusta biala strone
nospor
Znaczy ze zrobiles blad. Pokaz cały kod po poprawkach
dirtyhustlaz
ja juz nie wiem, pomieszalo mi sie wszystko. prosze. moglbys to dla mnie naprawic?

to jest kod strony editdeletestudent.php: (ladnie pokazuje mi dane z student w tabelce z opcja edit i delete)

  1. <?php
  2.  
  3. // Connects to your Database
  4.  
  5. mysql_connect("localhost", "root", "") or die(mysql_error());
  6.  
  7. mysql_select_db("glencaldy") or die(mysql_error());
  8.  
  9.  
  10. //checks cookies to make sure they are logged in
  11.  
  12. if (isset($_COOKIE['ID_my_site'])) {
  13. $username = $_COOKIE['ID_my_site'];
  14.  
  15. $pass = $_COOKIE['Key_my_site'];
  16.  
  17. $check = mysql_query("SELECT * FROM student WHERE username = '$username'") or die(mysql_error());
  18.  
  19. while ($info = mysql_fetch_array($check)) {
  20. //if the cookie has the wrong password, they are taken to the login page
  21.  
  22. if ($pass != $info['password']) {
  23. header("Location: login.php");
  24.  
  25. }
  26.  
  27.  
  28.  
  29. //otherwise they are shown the admin area
  30.  
  31. else {
  32. echo "<h1>DELETE/EDIT STUDENTS</h1><p>";
  33. $result = mysql_query("SELECT * FROM student") or die('Error');
  34.  
  35. if (mysql_num_rows($result) > 0) {
  36. // if result is positive we display content
  37. echo "<table cellpadding=2 border=1>";
  38. while ($row = mysql_fetch_array($result)) {
  39. echo "<tr>";
  40. echo "<td>" . $row['studentid'] . "</td><td>" . $row['firstname'] . "</td><td>" . $row['surname'] . "</td>";
  41. echo "<td> <a href=deletestudent2.php?a=del&amp;id={$row['studentid']}>DEL</a>
  42. <a href=editstudent.php?a=edit&amp;id={$row['studentid']}>EDIT</a></td>";
  43. echo "</tr>";
  44. }
  45. echo "</table>";
  46. echo "<br>";
  47. echo "<a href=menu.php><< Back to main menu</a><p>";
  48. }
  49. }
  50. }
  51. }
  52.  
  53. else
  54. //if the cookie does not exist, they are taken to the login screen
  55. {
  56. header("Location: login.php");
  57.  
  58. }
  59.  
  60. ?>


to jest kod strony deletestudnet.php (kasuje rekordy z tabeli bez problemu):
  1. <?php
  2.  
  3. // Connects to your Database
  4.  
  5. mysql_connect("localhost", "root", "") or die(mysql_error());
  6.  
  7. mysql_select_db("glencaldy") or die(mysql_error());
  8.  
  9.  
  10. //checks cookies to make sure they are logged in
  11.  
  12. if (isset($_COOKIE['ID_my_site'])) {
  13. $username = $_COOKIE['ID_my_site'];
  14.  
  15. $pass = $_COOKIE['Key_my_site'];
  16.  
  17. $check = mysql_query("SELECT * FROM student WHERE username = '$username'") or die(mysql_error());
  18.  
  19. while ($info = mysql_fetch_array($check)) {
  20. //if the cookie has the wrong password, they are taken to the login page
  21.  
  22. if ($pass != $info['password']) {
  23. header("Location: login.php");
  24.  
  25. }
  26.  
  27.  
  28.  
  29. //otherwise they are shown the admin area
  30.  
  31. else {
  32.  
  33. $a = trim($_GET['a']);
  34. $id = trim($_GET['id']);
  35.  
  36. if ($a == 'del' and !empty($id)) {
  37. mysql_query("DELETE FROM student WHERE studentid='$id'") or die('Error.: ' . mysql_error());
  38. echo "<h1>DELETE STUDENT</h1><p>";
  39. echo 'Record deleted<br>';
  40. echo "<a href=menu.php><< Back to main menu</a><p>";
  41. }
  42. }
  43.  
  44. }
  45. }
  46.  
  47. else
  48. //if the cookie does not exist, they are taken to the login screen
  49. {
  50. header("Location: login.php");
  51.  
  52. }
  53.  
  54. ?>


a to jest kod tej nieszczesnej strony editstudent.php (pokazuje dane w formularzu ale jak naciskam update records wywala mi biala strone i dane w bazie sie nie zmieniaja)
  1. <?php
  2.  
  3. // Connects to your Database
  4.  
  5. mysql_connect("localhost", "root", "") or die(mysql_error());
  6.  
  7. mysql_select_db("glencaldy") or die(mysql_error());
  8.  
  9.  
  10. //checks cookies to make sure they are logged in
  11.  
  12. if (isset($_COOKIE['ID_my_site'])) {
  13. $username = $_COOKIE['ID_my_site'];
  14.  
  15. $pass = $_COOKIE['Key_my_site'];
  16.  
  17. $check = mysql_query("SELECT * FROM student WHERE username = '$username'") or die(mysql_error());
  18.  
  19. while ($info = mysql_fetch_array($check)) {
  20. //if the cookie has the wrong password, they are taken to the login page
  21.  
  22. if ($pass != $info['password']) {
  23. header("Location: login.php");
  24.  
  25. }
  26.  
  27.  
  28.  
  29. //otherwise they are shown the admin area
  30.  
  31. else {
  32. $a = trim($_REQUEST['a']);
  33. if(isset($_GET['id']))
  34. $id = trim($_GET['id']);
  35. else
  36. $id = '';
  37. if ($a == 'edit' and !empty($id)) {
  38. $result = mysql_query("SELECT * FROM student WHERE
  39. studentid='$id'") or die('Error');
  40.  
  41. if (mysql_num_rows($result) > 0) {
  42. // read content of table
  43. $r = mysql_fetch_assoc($result);
  44. echo "<h1>EDIT STUDENT</h1><p>";
  45. echo '<form action="editstudent.php" method="post">
  46. <input type="hidden" name="a" value="save" />
  47. <input type="hidden" name="studentid" value="' . $id . '" />
  48. First name:<br />
  49. <input type="text" name="firstname"
  50. value="' . $r['firstname'] . '" /><br />
  51. Surname:<br />
  52. <input type="text" name="surname"
  53. value="' . $r['surname'] . '" /><br />
  54. Username:<br />
  55. <input type="text" name="username"
  56. value="' . $r['username'] . '" /><br />
  57. Password:<br />
  58. <input type="text" name="password"
  59. value="' . $r['password'] . '" /><br />
  60. <input type="submit" value="Update Record" />
  61. </form>';
  62. echo "<a href=deletestudent.php><< Back to delete/edit menu</a><p>";
  63. echo "<a href=menu.php><< Back to main menu</a><p>";
  64.  
  65. }
  66.  
  67. }
  68. }
  69.  
  70. }
  71. } elseif ($a == 'save') {
  72. // get the new records from form
  73. $id = $_POST['studentid'];
  74. $firstname = trim($_POST['firstname']);
  75. $surname = trim($_POST['surname']);
  76. $username = trim($_POST['username']);
  77. $password = trim($_POST['password']);
  78. // update table
  79. mysql_query("UPDATE studnet SET firstname='$firstname',
  80. surname='$surname', username='&username', password='$password' WHERE studentid='$id'") or die('Error');
  81. echo 'records updated';
  82. }
  83.  
  84. else
  85. //if the cookie does not exist, they are taken to the login screen
  86. {
  87. header("Location: login.php");
  88.  
  89. }
  90.  
  91. ?>



no i jest to zrobione GETem jednak (chyba sam juz nie wiem). prosze o spojrzenie w kod i pomoc

po usunieciu sprawdzania ciasteczek dziala wszystko ok:


ten kod dziala bez zarzutow ale jak chce sprawdzic ciasta to wywala na koniec pusta strone. jak tu dodac sprawdzanie ciastek teraz? jesli nie ma ciasta ma pokazywac login.php a jak sa to ma pokazywac tresc strony

  1. <?php
  2. // Connects to your Database
  3.  
  4. mysql_connect("localhost", "root", "") or die(mysql_error());
  5.  
  6. mysql_select_db("glencaldy") or die(mysql_error());
  7.  
  8.  
  9. $a = trim($_REQUEST['a']);
  10. if(isset($_GET['id']))
  11. $id = trim($_GET['id']);
  12. else
  13. $id = '';
  14. if ($a == 'edit' and !empty($id)) {
  15. $result = mysql_query("SELECT * FROM student WHERE
  16. studentid='$id'") or die('Error');
  17.  
  18. if (mysql_num_rows($result) > 0) {
  19. // read content of table
  20. $r = mysql_fetch_assoc($result);
  21. echo "<h1>EDIT STUDENT</h1><p>";
  22. echo '<form action="editstudent.php" method="post">
  23. <input type="hidden" name="a" value="save" />
  24. <input type="hidden" name="studentid" value="' . $id . '" />
  25. First name:<br />
  26. <input type="text" name="firstname"
  27. value="' . $r['firstname'] . '" /><br />
  28. Surname:<br />
  29. <input type="text" name="surname"
  30. value="' . $r['surname'] . '" /><br />
  31. Username:<br />
  32. <input type="text" name="username"
  33. value="' . $r['username'] . '" /><br />
  34. Password:<br />
  35. <input type="text" name="password"
  36. value="' . $r['password'] . '" /><br />
  37. <input type="submit" value="save" />
  38. </form>';
  39.  
  40. }
  41.  
  42. }
  43. elseif ($a == 'save') {
  44. // get the new records from form
  45. $id = $_POST['studentid'];
  46. $firstname = trim($_POST['firstname']);
  47. $surname = trim($_POST['surname']);
  48. $username = trim($_POST['username']);
  49. $password = trim($_POST['password']);
  50. // update table
  51. mysql_query("UPDATE student SET firstname='$firstname',
  52. surname='$surname', username='&username', password='$password' WHERE studentid='$id'") or die('Error');
  53. echo 'records updated';
  54. }
  55.  
  56. ?>
!*!
1. Zacznij od jakiegoś kursu PHP np. http://pl.wikibooks.org/wiki/PHP
2. Zdecyduj się w końcu czy używasz GET czy POST i do czego/w którym momencie.
3. Używaj var_dump jak czegoś nie wiesz, gdzie leży błąd sprawdzając zmienne.
dirtyhustlaz
ok, juz mi wszystko dziala jak powinno, logowanie do bazy dalem do odzielnego pliku, zrobilem porzadek w kodzie no i okazalo sie ze mialem nastepujace bledy 1) literowka w nazwie tabeli wink.gif 2) zle domkniete klamry od sprawdzania ciastek.

dziekuje wszystkim za pomoc.

a o to poprawny kod.
  1. <?php
  2. // Connects to your Database
  3. require "connect.php";
  4. connection();
  5.  
  6.  
  7. if (isset($_COOKIE['ID_my_site'])) {
  8. $username = $_COOKIE['ID_my_site'];
  9.  
  10. $pass = $_COOKIE['Key_my_site'];
  11.  
  12. $check = mysql_query("SELECT * FROM student WHERE username = '$username'") or die(mysql_error());
  13.  
  14. while ($info = mysql_fetch_array($check)) {
  15. //if the cookie has the wrong password, they are taken to the login page
  16.  
  17. if ($pass != $info['password']) {
  18. header("Location: login.php");
  19.  
  20. }
  21.  
  22.  
  23.  
  24. //otherwise they are shown the admin area
  25.  
  26. else {
  27.  
  28. $a = trim($_REQUEST['a']);
  29. if(isset($_GET['id'])) $id = trim($_GET['id']);
  30. else
  31. $id = '';
  32. if ($a == 'edit' and !empty($id)) {
  33. $result = mysql_query("SELECT * FROM student WHERE studentid='$id'") or die('Error');
  34.  
  35. if (mysql_num_rows($result) > 0) {
  36. // read content of table
  37. $r = mysql_fetch_assoc($result);
  38. echo "<h1>EDIT STUDENT</h1><p>";
  39. echo '<form action="editstudent.php" method="post">
  40. <input type="hidden" name="a" value="save" />
  41. <input type="hidden" name="studentid" value="' . $id . '" />
  42. First name:<br /> <input type="text" name="firstname"
  43. value="' . $r['firstname'] . '" /><br />
  44. Surname:<br />
  45. <input type="text" name="surname"
  46. value="' . $r['surname'] . '" /><br /> Username:<br />
  47. <input type="text" name="username"
  48. value="' . $r['username'] . '" /><br />
  49. Password:<br />
  50. <input type="text" name="password" value="' . $r['password'] . '" /><br />
  51. <input type="submit" value="save" />
  52. </form>';
  53. echo "<a href=deletestudent.php><< Back to delete/edit menu</a><br>";
  54. echo "<a href=menu.php><< Back to main menu</a><p>";
  55.  
  56. }
  57. }
  58. elseif ($a == 'save') {
  59. // get the new records from form
  60. $id = $_POST['studentid']; $firstname = trim($_POST['firstname']);
  61. $surname = trim($_POST['surname']);
  62. $username = trim($_POST['username']);
  63. $password = trim($_POST['password']);
  64. // update table
  65. mysql_query("UPDATE student SET firstname='$firstname',
  66. surname='$surname', username='$username', password='$password' WHERE studentid='$id'") or die('Error');
  67. echo "<h1>EDIT STUDENT</h1><p>";
  68. echo '<i>Record Updated</i><p>';
  69. echo "<a href=deletestudent.php><< Back to delete/edit menu</a><br>";
  70. echo "<a href=menu.php><< Back to main menu</a><br>";
  71. }
  72.  
  73.  
  74.  
  75. }
  76. }
  77. }
  78. else
  79. //if the cookie does not exist, they are taken to the login screen
  80. {
  81. header("Location: login.php");
  82.  
  83. }
  84. ?>
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.