Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [php/mysql] edytowanie rekordow
dirtyhustlaz
post
Post #1





Grupa: Zarejestrowani
Postów: 22
Pomógł: 0
Dołączył: 29.03.2012

Ostrzeżenie: (0%)
-----


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
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
dirtyhustlaz
post
Post #2





Grupa: Zarejestrowani
Postów: 22
Pomógł: 0
Dołączył: 29.03.2012

Ostrzeżenie: (0%)
-----


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. ?>
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 28.12.2025 - 04:48