Pomoc - Szukaj - U¿ytkownicy - Kalendarz
Pe³na wersja: [php + mysql]Usuwanie wierszy MySQL przez php
Forum PHP.pl > Forum > Przedszkole
eXido
Witam,

Przygode z php i SQL zaczalem dopiero tydzine temu, wiec prosze o wyrozumialosc.

Troche opisze sytuacje: Mam tabele w MySQL zwana 'Hirer' która zawiera informacje takie jak adres zamieszkania, telefon, email itd.
Kazdy wiersz w tej tabeli jest identyfikowany przez pole 'HirerID' które jest auto_increment primary key.

Teraz, zrobilem w php skrypt który wyswietla na stronie wszystkich Hirer'ów w tej tabeli. Jednak chce zeby na koncu kazdego wiersza miec link który skasuje z tej tabeli dany wiersz, jednak link ten nie dziala.

Bylbym wdzieczny gdyby ktos mógl podac gdzie mam blad i jak powinna ta strona wygladac by poprawnie dzialala:

  1. <html>
  2. <head>
  3.  
  4. </head>
  5. <body>
  6.  
  7. <?php
  8.  
  9. $dbcnx= @mysql_connect('localhost', 'root', 'laptop55');
  10. if (!$dbcnx) {
  11. exit('<p>Unable to connect to the database server at this time.</p>');
  12. }
  13.  
  14. if (!@mysql_select_db('Village_Hall')) {
  15. exit('<p>Unable to locate the Village Hall database at this time.</p>');
  16. }
  17.  
  18.  
  19.  
  20.  
  21. //removing deleted hirers if set
  22.  
  23. if (isset($_GET['deletehirer'])) {
  24. $HirerID = $row['HirerID'];
  25. $sql = "DELETE FROM Hirer WHERE HirerID=$HirerID";
  26. if (@mysql_query($sql)) {
  27. echo '<p> The Hirer has been removed from the database.</p>';
  28. } else {
  29. echo '<p> Error removing Hirer from database: ' . mysql_error() . '</p>';
  30. }
  31. }
  32. ?>
  33.  
  34. <!-- show Hirers currently in the database -->
  35.  
  36. Here are all the hirers currently in the database:</p>
  37. <?php
  38.  
  39. $result = @mysql_query('select HirerID, Title, Surname, Forename, Street_Number, Town_City, County, Post
    Code, Home_Tel, 
  40.  
  41. Mobile_Tel, Email from Hirer');
  42. if (!$result) {
  43. exit('<p>Error performing query: ' . mysql_error() . '</p>');
  44. }
  45.  
  46. while ($row = mysql_fetch_array($result)) {
  47.  
  48. ?>      <table width="85%" valign="center"><tr><td width="10">      <?php
  49.  
  50.  
  51. echo $row['HirerID']; ?> </td> 
  52.  
  53.  
  54. <td width="15"> <?php echo $row['Title']; ?> </td> 
  55.  
  56.  
  57. <td width="20"> <?php  echo $row['Surname']; ?> </td>  
  58.  
  59.  
  60. <td width="20"> <?php echo $row['Forename']; ?> </td> 
  61.  
  62.  
  63. <td width="10"> <?php echo $row['Street_Number']; ?> </td> 
  64.  
  65.  
  66. <td width="20"> <?php echo $row['Street_Name']; ?> </td> 
  67.  
  68.  
  69. <td width="20"> <?php echo $row['Town_City']; ?> </td> 
  70.  
  71.  
  72. <td width="20"> <?php echo $row['County']; ?> </td> 
  73.  
  74.  
  75. <td width="20"> <?php echo $row['PostCode']; ?></td> 
  76.  
  77.  
  78. <td width="20"> <?php echo $row['Home_Tel']; ?></td> 
  79.  
  80.  
  81. <td width="25"> <?php echo $row['Mobile_Tel']; ?></td> 
  82.  
  83.  
  84. <td width="25"> <?php echo $row['Email']; ?></td>
  85.  
  86.  
  87. <!-- set to delete Hirer on click -->
  88.  
  89. <td width="25"> <?php echo ' <a href="' . $_SERVER['PHP_SELF'] .
  90. '?deletehirer=' . $HirerID . '">' .
  91. 'Remove Hirer</a>'; 
  92. ?>
  93.  
  94.    </td>
  95.  
  96.  
  97.      </table>    <?php
  98.  
  99.  
  100. }
  101. ?>
  102.  
  103.  
  104.  
  105. </body>
  106. </html>


Dziekuje i pozdrawiam,

eXido
nospor
  1. <?php
  2. if (isset($_GET['deletehirer'])) {
  3. $HirerID = $row['HirerID'];
  4. ?>

nie uwazasz, ze powinno byc tak:
  1. <?php
  2. if (isset($_GET['deletehirer'])) {
  3. $HirerID = $_GET['deletehirer'];
  4. ?>
winksmiley.jpg

ps: prosze poprawic tytul o wlasciwy znacznik zgodnie z zasadami forum przedszkole
ps2: prosze poprawic bbcode na bbcode php
eXido
Witam, dziekuje za pomoc, jednak nadal mamy problem:

  1. <?php
  2. 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 '' at line 1
  3. ?>


Tutaj mam zaktualizowany plik, prosze spojrzec:

  1. <html>
  2. <head>
  3.  
  4. </head>
  5. <body>
  6.  
  7. <?php
  8.  
  9. $dbcnx= @mysql_connect('localhost', 'root', 'laptop55');
  10. if (!$dbcnx) {
  11. exit('<p>Unable to connect to the database server at this time.</p>');
  12. }
  13.  
  14. if (!@mysql_select_db('Village_Hall')) {
  15. exit('<p>Unable to locate the Village Hall database at this time.</p>');
  16. }
  17.  
  18.  
  19.  
  20.  
  21. //removing deleted hirers if set
  22.  
  23. if (isset($_GET['deletehirer'])) {
  24. $HirerID = $_GET['deletehirer'];
  25. $sql = "DELETE FROM Hirer WHERE HirerID=$HirerID";
  26. if (@mysql_query($sql)) {
  27. echo '<p> The Hirer has been removed from the database.</p>';
  28. } else {
  29. echo '<p> Error removing Hirer from database: ' . mysql_error() . '</p>';
  30. }
  31. }
  32. ?>
  33.  
  34. <!-- show Hirers currently in the database -->
  35.  
  36. Here are all the hirers currently in the database:</p>
  37. <?php
  38.  
  39. $result = @mysql_query('select HirerID, Title, Surname, Forename, Street_Number, Town_City, County, Post
    Code, Home_Tel, Mobile_Tel, Email from Hirer'
    );
  40. if (!$result) {
  41. exit('<p>Error performing query: ' . mysql_error() . '</p>');
  42. }
  43.  
  44. while ($row = mysql_fetch_array($result)) {
  45.  
  46. ?> <table width="85%" valign="center"><tr><td width="10"> <?php
  47.  
  48.  
  49. echo $row['HirerID']; ?> </td> 
  50.  
  51.  
  52. <td width="15"> <?php echo $row['Title']; ?> </td> 
  53.  
  54.  
  55. <td width="20"> <?php echo $row['Surname']; ?> </td>
  56.  
  57.  
  58. <td width="20"> <?php echo $row['Forename']; ?> </td> 
  59.  
  60.  
  61. <td width="10"> <?php echo $row['Street_Number']; ?> </td> 
  62.  
  63.  
  64. <td width="20"> <?php echo $row['Street_Name']; ?> </td> 
  65.  
  66.  
  67. <td width="20"> <?php echo $row['Town_City']; ?> </td> 
  68.  
  69.  
  70. <td width="20"> <?php echo $row['County']; ?> </td> 
  71.  
  72.  
  73. <td width="20"> <?php echo $row['PostCode']; ?></td> 
  74.  
  75.  
  76. <td width="20"> <?php echo $row['Home_Tel']; ?></td> 
  77.  
  78.  
  79. <td width="25"> <?php echo $row['Mobile_Tel']; ?></td> 
  80.  
  81.  
  82. <td width="25"> <?php echo $row['Email']; ?></td>
  83.  
  84.  
  85. <!-- set to delete Hirer on click -->
  86.  
  87. <td width="25"> <?php echo ' <a href="' . $_SERVER['PHP_SELF'] .
  88. '?deletehirer=' . $HirerID . '">' .
  89. 'Remove Hirer</a>'; 
  90. ?>
  91.  
  92.  </td>
  93.  
  94.  
  95.  </table> <?php
  96.  
  97.  
  98. }
  99. ?>
  100.  
  101.  
  102.  
  103. </body>
  104. </html>


Dziekuje.
Temat + phpcode juz poprawilem winksmiley.jpg
pEbE
  1. <?php
  2.  
  3. $sql = "DELETE FROM Hirer WHERE HirerID='$HirerID'";
  4.  
  5. ?>
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.