Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [php] Domyślna wartość w $_GET
Forum PHP.pl > Forum > Przedszkole
powertee
skrypt wykonuje dwa polecenia add lub edit które przekazywane sa metodą $_GET['action'], chciałbym uzyskać cos takiego zeby action domyślnie ustwiony był na operacje add, ta strona pobiera za pomocą GET informacje z innej strony co ma zrobić czy dodać czy edytowąć, chciałbym zeby strona domyślnie wykonywała dodawanie gdy tablica GET jest pusta, mysle ze trzeba wstawić tu jakiegoś if-a ale nie bardzo wim gdzie i jak:/, wie ktoś jak to zrbić??


  1. <?
  2. $link = mysql_connect("localhost", "root", "")
  3. or die("nie udalo sie poilaczyc: " . mysql_error());
  4. mysql_select_db("bazatel" , $link)
  5. or die (mysql_error());
  6.  
  7. $convert['edit'] = 'Edytuj' ; 
  8. $convert['add'] = 'Dodaj';
  9.  
  10. $seltab = "SELECT * FROM tabela" ;
  11. $result = mysql_query($seltab)
  12. or die ("Niepoprawne zapytanie: " . mysql_error());
  13. while ($row = mysql_fetch_array($result)) {
  14. $tel[$row['id']] = $row['nazwisko'];
  15. }
  16. switch ($_GET['action']) {
  17. case "edit";
  18. $seltab = "SELECT * FROM tabela WHERE id = '" . $_GET['id'] . "'";
  19. $result = mysql_query($seltab)
  20. or die ("Niepoprawne zapytanie: " . mysql_error());
  21. $row = mysql_fetch_array($result);
  22. $imie = $row['imie'];
  23. $id = $row['id'];
  24. $nazwisko = $row['nazwisko'];
  25. $email = $row['email'];
  26. $kod = $row['telefon'];
  27. break;
  28. default;
  29. $imie = "" ;
  30. $id = "" ;
  31. $nazwisko = "" ;
  32. $email = "" ;
  33. $telefon = "" ;
  34. break;
  35. }
  36. ?>
  37. <html>
  38. <head>
  39. <title>Dodaj / edytuj wpis...</title>
  40. <style type="text/css">
  41. TD{color:#353535;font-family:verdana}
  42. TH{color:#FFFFFF;font-family:verdena;background-color:#336699}
  43. </style>
  44. </head>
  45. <body>
  46. <form action="dodaj.php?action=
  47. <? 
  48. echo $_GET['action'] ;?>&type=tele&id=
  49. <? 
  50. if (isset($_GET['id'])) { echo $_GET['id']; } ?>" method="post">
  51. <?
  52. if (!empty($_GET['error'])) {
  53. echo "<div align=\"center\" " .
  54. "style=\"color:#ff0000;background-collor:#ff0000;" .
  55. "font-weight:bold\">" . nl2br(urldecode($_GET['error'])) .
  56. "</div><br />";
  57. }
  58. ?>
  59. <table border="0" width="750" cellspacing="1" cellpadding="3"
  60.  bgcolor="#353535" align="center">
  61. <tr>
  62.  <td bgcolor="#ffffff" width="30%">Imie:</td>
  63.  <td bgcolor="#ffffff" width="70%">
  64. <input type="text" name="title" value="<? echo $imie; ?>">
  65.  </td>
  66. </tr>
  67. <tr>
  68.  <td bgcolor="#ffffff" width="30%">Nazwisko</td>
  69.  <td bgcolor="#ffffff" width="70%">
  70. <input type="text" name="album" value="<? echo $nazwisko; ?>">
  71.  
  72.  </td>
  73. </tr>
  74.  
  75. <tr>
  76.  <td bgcolor="#ffffff" width="30%">Adres e-mail</td>
  77.  <td bgcolor="#ffffff" width="70%">
  78. <input type="text" name="kod" value="<? echo $email; ?>">
  79.  </td>
  80. </tr>
  81.  
  82.  
  83.  
  84. <tr>
  85.  <td bgcolor="#ffffff" width="30%">Telefon</td>
  86.  <td bgcolor="#ffffff" width="70%">
  87. <input type="text" name="uwagi" value="<? echo $telefon; ?>">
  88.  </td>
  89. </tr>
  90.  
  91. <tr>
  92. <td bgcolor="#FFFFFF" colspan="2" align="center">
  93. <input type="submit" name="SUBMIT" value="<? echo $convert[$_GET['action']]; ?>">
  94. </td>
  95. </tr>
  96. <table>
  97. </form>
  98. </body>
  99. </html>
Cezar708
zawsze możesz takie obejście:

  1. <?php
  2. if ( !isset($_GET['action']) ) $_GET['action'] = 'add';
  3. ?>
powertee
ok działa:) tylko nie wiem dlaczego widzi wszystkie pola jako puste i wyswietla komunikat o niewypełnieniu pól formularza
wstawiam skrypt który za to odpowiada nie wiem co jest nie tak:/
  1. <?
  2. $error = '';
  3. $link = mysql_connect("localhost", "root", "")
  4. or die("nie udalo sie poilaczyc: " . mysql_error());
  5. mysql_select_db("bazatel" , $link)
  6.  or die("cos jest nie tak : " . mysql_error());
  7.  
  8. switch ($_GET['action']) {
  9. case "edit";
  10.  switch ($_GET['type']){
  11.  case "tele";
  12.  $imie = trim($_POST['imie']);
  13.  if (empty($imie)) {
  14.  $error .= "PROSZE+WPISAC+IMIE";
  15.  }
  16. if (empty($_POST['nazwisko'])) {
  17.  $error .= "WYBIERZ+NAZWISKO";
  18. }
  19. if (empty($_POST['email'])) {
  20. $error .= "WYBIERZ+EMAIL";
  21. }
  22. if (empty($_POST['telefon'])) {
  23. $error .= "PODAJ+TELEFON";
  24. }
  25. if (empty($error)) { 
  26. $sql = "UPDATE tabela SET
  27. imie = '" . $_POST['imie'] . "' ,
  28. nazwisko = '" . $_POST['nazwisko'] . "' ,
  29. email = '" . $_POST['email'] . "' ,
  30. telefon = '" . $_POST['telefon'] . "' ,
  31. WHERE id = '" . $_GET['id'] . "'";
  32. } else {
  33. header("location:index.php?action=edit&error=" .
  34. $error . "&id=" . $_GET['id'] );
  35. }
  36.  break;
  37. }
  38. break;
  39. case "add";
  40. switch ($_GET['type']) {
  41. case "tele";
  42. $imie = trim($_POST['imie']);
  43.  if (empty($imie)) {
  44.  $error .= "PROSZE+WPISAC+IMIE";
  45.  }
  46. if (empty($_POST['nazwisko'])) {
  47.  $error .= "WYBIERZ+NAZWISKO";
  48. }
  49. if (empty($_POST['email'])) {
  50. $error .= "WYBIERZ+EMAIL";
  51. }
  52. if (empty($_POST['telefon'])) {
  53. $error .= "PODAJ+NUMER+TELEFONU";
  54. }
  55. if (empty($error)) {
  56. $sql = "INSERT INTO tabela
  57. (imie,
  58. nazwisko,
  59. email,
  60. telefon)
  61. VALUES
  62. ('" . $_POST['imie'] . "',
  63. '" . $_POST['nazwisko'] . "',
  64. '" . $_POST['email'] . "',
  65. '" . $_POST['telefon'] . "')";
  66. } else {
  67. header("location:index.php?action=add&error=" . $error);
  68. }
  69.  
  70. break;
  71. }
  72. break;
  73. }
  74. if (isset($sql) && !empty($sql)) {
  75. $resul = mysql_query($sql)
  76. or die("niepoprawne zapytanie: " . mysql_error());
  77. ?>
  78. <p align="center" style="color:#FF0000">
  79. Wykonane. <a href="lista.php">lista</a></p>
  80. <?
  81. }
  82. ?>
Mak
Jeżeli ten skrypt odnosi się do formularza w poprzednim poście to po pierwsze pozmieniaj parametry name w tagach input.
I jak już wspomniałem w innym temacie dotyczącym tego skryptu zamien średniki po case na dwukropki.
powertee
a co znimi nie tak wszystko sie zgrywa??
Mak
Cytat(powertee @ 14.03.2008, 14:33:08 ) *
  1. <input type="text" name="title" value="<? echo $imie; ?>">
  2. <input type="text" name="album" value="<? echo $nazwisko; ?>">
  3. <input type="text" name="kod" value="<? echo $email; ?>">
  4. <input type="text" name="uwagi" value="<? echo $telefon; ?>">


No nie zabardzo jeżeli w skrypcie sprawdzasz:
Kod
$_POST['imie']
$_POST['nazwisko']
$_POST['email']
$_POST['telefon']
powertee
już zanlazłem bląd ale teraz wyskakuje mi coś takiego : "niepoprawne zapytanie: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE id = '9'' at line 6"
dodawanie mam bląd składni:/ błąd znajduje sie 31 linijce tego skryptu, nie wiem co jest grane bo powinno być ok
  1. <?
  2. $error = '';
  3. $link = mysql_connect("localhost", "root", "")
  4. or die("nie udalo sie poilaczyc: " . mysql_error());
  5. mysql_select_db("bazatel" , $link)
  6.  or die("cos jest nie tak : " . mysql_error());
  7.  
  8. switch ($_GET['action']) {
  9. case "edit";
  10.  switch ($_GET['type']){
  11.  case "tele";
  12.  $imie = trim($_POST['imie']);
  13.  if (empty($imie)) {
  14.  $error .= "PROSZE+WPISAC+IMIE";
  15.  }
  16. if (empty($_POST['nazwisko'])) {
  17.  $error .= "WYBIERZ+NAZWISKO";
  18. }
  19. if (empty($_POST['email'])) {
  20. $error .= "WYBIERZ+EMAIL";
  21. }
  22. if (empty($_POST['telefon'])) {
  23. $error .= "PODAJ+TELEFON";
  24. }
  25. if (empty($error)) { 
  26. $sql = "UPDATE tabela SET
  27. imie = '" . $_POST['imie'] . "' ,
  28. nazwisko = '" . $_POST['nazwisko'] . "' ,
  29. email = '" . $_POST['email'] . "' ,
  30. telefon = '" . $_POST['telefon'] . "' ,
  31. WHERE id = '" . $_GET['id'] . "' ";
  32. } else {
  33. header("location:index.php?action=edit&error=" .
  34. $error . "&id=" . $_GET['id'] );
  35. }
  36.  break;
  37. }
  38. break;
  39. case "add";
  40. switch ($_GET['type']) {
  41. case "tele";
  42. $imie = trim($_POST['imie']);
  43.  if (empty($imie)) {
  44.  $error .= "PROSZE+WPISAC+IMIE";
  45.  }
  46. if (empty($_POST['nazwisko'])) {
  47.  $error .= "WYBIERZ+NAZWISKO";
  48. }
  49. if (empty($_POST['email'])) {
  50. $error .= "WYBIERZ+EMAIL";
  51. }
  52. if (empty($_POST['telefon'])) {
  53. $error .= "PODAJ+NUMER+TELEFONU";
  54. }
  55. if (empty($error)) {
  56. $sql = "INSERT INTO tabela
  57. (imie,
  58. nazwisko,
  59. email,
  60. telefon)
  61. VALUES
  62. ('" . $_POST['imie'] . "',
  63. '" . $_POST['nazwisko'] . "',
  64. '" . $_POST['email'] . "',
  65. '" . $_POST['telefon'] . "')";
  66. } else {
  67. header("location:index.php?action=add&error=" . $error);
  68. }
  69.  
  70. break;
  71. }
  72. break;
  73. }
  74. if (isset($sql) && !empty($sql)) {
  75. $resul = mysql_query($sql)
  76. or die("niepoprawne zapytanie: " . mysql_error());
  77. ?>
  78. <p align="center" style="color:#FF0000">
  79. Wykonane. <a href="lista.php">lista</a></p>
  80. <?
  81. }
  82. ?>
goozer
Przed WHERE nie ma byc przecinka.

G
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.