Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> usuwanie rekordu z bazy danych., za pomocą PHP + AJAX
vojtasek
post
Post #1





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

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


Mam pliczek PHP w którym wykonuję operację pobierania z bazy danych. Pobieranie wykonuje się prawidłowo. Oto on dane.php:

  1. $hostname = 'localhost';
  2. $username = 'root';
  3. $password = 'root';
  4.  
  5. $dbname = 'fenster';
  6.  
  7. try {
  8. $dbh = new PDO("mysql:host=$hostname;dbname=$dbname", $username, $password);
  9.  
  10. if($_POST['profil']) {
  11. $profil = $_POST['profil'];
  12. $farbe = $_POST['farbe'];
  13. $fenstertyp = $_POST['fenstertyp'];
  14. $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  15.  
  16. $sql = "INSERT INTO bestellen (profil, farbe, fenstertyp)
  17. VALUES (:profil, :farbe, :fenstertyp)";
  18.  
  19. $stmt = $dbh->prepare($sql);
  20.  
  21.  
  22. $stmt->bindParam(':profil', $profil, PDO::PARAM_STR);
  23. $stmt->bindParam(':farbe', $farbe, PDO::PARAM_STR);
  24. $stmt->bindParam(':fenstertyp', $fenstertyp, PDO::PARAM_STR);
  25.  
  26. if ($stmt->execute()) {
  27. populate_shoutbox();
  28. }
  29. }
  30. }
  31. catch(PDOException $e) {
  32. echo $e->getMessage();
  33. }
  34.  
  35. if($_POST['refresh']) {
  36. populate_shoutbox();
  37. }
  38.  
  39.  
  40. function populate_shoutbox() {
  41. global $dbh;
  42. $sql = "select * from bestellen";
  43. echo '<table width="100%" border="0" align="right" cellpadding="5" cellspacing="0">';
  44. foreach ($dbh->query($sql) as $row) {
  45. echo '<tr>';
  46. echo '<td>';
  47. echo '<b><font color="ff0000">Fenstertyp</font></b>:<br /><br /><p>', $row['fenstertyp'];
  48. echo '</td>';
  49. echo '<td>';
  50. echo '<b><font color="ff0000">Profil</font></b>: ', $row['profil'],'<br />';
  51. echo '<b><font color="ff0000">Farbe</font></b>: ', $row['farbe'],'<br />';
  52. echo '<b><font color="ff0000">Decorfarbe</font></b>: ', $row['farbe'],'<br />';
  53. echo '<b><font color="ff0000">Verglasung</font></b>: ', $row['farbe'],'<br />';
  54. echo '<b><font color="ff0000">termische</font></b>: ', $row['farbe'],'<br />';
  55. echo '<b><font color="ff0000">schallschutzverglasung</font></b>: ', $row['farbe'],'<br />';
  56. echo '<b><font color="ff0000">sicherheitsverglasung</font></b>: ', $row['farbe'],'<br />';
  57. echo '<b><font color="ff0000">ornament</font></b>: ', $row['farbe'],'<br />';
  58. echo '<b><font color="ff0000">Sprossen</font></b>: ', $row['farbe'],'<br />';
  59. echo '<b><font color="ff0000">Beschlag</font></b>: ', $row['farbe'],'<br />';
  60. echo '<b><font color="ff0000">Griffe</font></b>: ', $row['farbe'],'<br />';
  61. echo '<b><font color="ff0000">Aussenfensterbank</font></b>: ', $row['farbe'],'<br />';
  62. echo '<b><font color="ff0000">Rolladen</font></b>: ', $row['farbe'],'<br /><hr></hr><br />';
  63. echo '</td>';
  64. echo '<td>';
  65. echo '<a href="#" id='.$row['id'].'; class="usun">Delete</a>';
  66. echo '</td>';
  67. echo '<td>';
  68. echo '</td>';
  69. echo '</tr>';
  70. echo '<tr>';
  71. echo '';
  72. echo '</tr>';
  73. }
  74. echo '</table>';
  75. }
  76. if($_POST['id'])
  77. {
  78. $id=$_POST['id'];
  79. $id = mysql_escape_String($id);
  80. $sql = "delete from bestellen where id='$id'";
  81. mysql_query( $sql);
  82. }
  83. ?>


dalej operuje skryptem AJAX w pliku np. div.js
  1. $(function() {
  2. $(".usun").click(function() {
  3. var id = $(this).attr("id");
  4. var dataString = 'id='+ id ;
  5. var parent = $(this).parent();
  6.  
  7. $.ajax({
  8. type: "POST",
  9. url: "dane.php",
  10. data: dataString,
  11. cache: false,
  12.  
  13. success: function()
  14. {
  15. if(id % 2)
  16. {
  17. parent.fadeOut('slow', function() {$(this).remove();});
  18. }
  19. else
  20. {
  21. parent.slideUp('slow', function() {$(this).remove();});
  22. }
  23. }
  24. });
  25.  
  26. return false;
  27. });
  28. });



i nie chce mi usuwać tego ch...go rekordu...nie wiem co jest grane.... a ma to usuwać bez odświeżania strony dynamicznie...
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: 22.09.2025 - 03:40