Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [MySQL][PHP]Aktualizacja rekordu mysql
Forum PHP.pl > Forum > Przedszkole
smigla
Witam, mam taki o to kod wyświetlający rekord oraz kod edytujący i nie wiem jak zrobić żeby po kliknięciu w jakiś przycisk mógł edytować wyświetlony rekord

  1. <?php
  2. // konfiguracja
  3. $conn_config = array(
  4. 'host' => 'localhost',
  5. 'port' => '3306',
  6. 'user' => 'root',
  7. 'pass' => 'password',
  8. 'db' => 'myDatabase',
  9. 'db_type' => 'mysql',
  10. 'encoding' => 'utf-8'
  11. );
  12.  
  13. // próba połączenia
  14. try
  15. {
  16. $dsn = $conn_config['db_type'] .
  17. ':host=' . $conn_config['host'] .
  18. ';port=' . $conn_config['port'] .
  19. ';encoding=' . $conn_config['encoding'] .
  20. ';dbname=' . $conn_config['db'];
  21.  
  22.  
  23. $dbh = new PDO($dsn, $conn_config['user'], $conn_config['pass']);
  24.  
  25. // ustawienie trybu raportowania błędów
  26. $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  27.  
  28. define('DB_CONNECTED', true);
  29. echo '<h1>Connection success!</h1>';
  30.  
  31. // zapytanie do bazy
  32. $stmt = $dbh->query('SELECT * FROM customers');
  33.  
  34. // pobranie/wyświetlenie wyników
  35. while($row = $stmt->fetch())
  36. {
  37. echo 'id: ' . $row['customer_id'] .
  38. ', imię:' . $row['first_name'] .
  39. ', nazwisko:' . $row['last_name'] .
  40. '<br />';
  41.  
  42. }
  43. // łapiemy ewentualny wyjątek:
  44. } catch(PDOException $e)
  45. {
  46. die('Unable to connect: ' . $e->getMessage());
  47. }


  1. if($_POST['action'] == 'update')
  2. {
  3. $id = $_POST['id'];
  4.  
  5. $password = $_POST['password'];
  6. $first_name = $_POST['first_name'];
  7. $last_name = $_POST['last_name'];
  8. $email = $_POST['email'];
  9. $country = $_POST['country '];
  10. $city = $_POST['city'];
  11. $post_code = $_POST['post_code'];
  12. $address = $_POST['address'];
  13. $phone = $_POST['phone'];
  14. $updated_at = time();
  15.  
  16. // przygotowanie szkieletu zapytania INSERT INTO
  17. $stmt = $dbh->execute('UPDATE customers SET
  18. password = :password,
  19. first_name = :first_name,
  20. last_name = :last_name,
  21. email = :email,
  22. country = :country,
  23. city = :city,
  24. post_code = :post_code,
  25. address = :address,
  26. phone = :phone,
  27. updated_at = :updated_at
  28. WHERE customer_id = :id
  29. ');
  30.  
  31. // przypisujemy zmienne do placeholderów
  32.  
  33. bindValue(':login', $login, PDO::PARAM_STR);
  34. bindValue(':password', $password, PDO::PARAM_STR);
  35. bindValue(':first_name', $first_name, PDO::PARAM_STR);
  36. bindValue(':last_name', $last_name, PDO::PARAM_STR);
  37. bindValue(':email', $email, PDO::PARAM_STR);
  38. bindValue(':country', $country , PDO::PARAM_STR);
  39. bindValue(':city', $city, PDO::PARAM_STR);
  40. bindValue(':post_code', $post_code, PDO::PARAM_STR);
  41. bindValue(':address', $address, PDO::PARAM_STR);
  42. bindValue(':phone', $phone, PDO::PARAM_STR);
  43. bindValue(':updated_at', $updated_at, PDO::PARAM_INT);
  44. bindValue(':id', $id, PDO::PARAM_INT);
  45.  
  46. // wykonujemy zapytanie
  47. $result = $stmt->execute();
  48.  
  49. if($result !== false)
  50. {
  51. echo 'Zaktualizowano użytkownika o ID = ' . $id;
  52. } else {
  53. echo 'Wystąpił błąd';
  54. }
  55.  
  56. $stmt->closeCursor();
  57. unset($stmt);
  58. $dbh = null;
  59. }



Ktoś pomoże ? lub ma może link do gotowego takiego skryptu który umożliwia edycję rekordów za pomocą PHP byle to było nie przestarzałe smile.gif
viking
A w czym problem? Update masz. Robisz select danych, wkładasz je do formularza, odczytujesz, update, gotowe.
W miejscu gdzie wyświetlasz id zrób linka w stylu edit=id i poźniej na podstronie edycji

  1. SELECT * FROM customers WHERE customer_id = ?
smigla
Mam już wyświetlone dane w formularzu tylko nie wiem jak zrobić się odwołać do tego update.

  1. <form action="" method="post">
  2. <tr>
  3. <th>Description</th>
  4. <th><input type="text" name="description" style="background:url(images/tlo2.png) repeat; font-weight: bold; color: #74777b; padding: 5px; width: 500px; font-size: 0.6em; border-style: none; -webkit-border-radius: 5px;-moz-border-radius: 5px;border-radius: 5px;" value="'.$row['1'].'"/></th>
  5. <td><input type="submit" value="EDYTUJ"></td>
  6. </tr>
  7. <tr>
  8. <td>Keywords</td>
  9. <td><input type="text" name="keywords" style="background:url(images/tlo2.png) repeat; font-weight: bold; color: #74777b; padding: 5px; width: 500px; font-size: 0.6em; border-style: none; -webkit-border-radius: 5px;-moz-border-radius: 5px;border-radius: 5px;" value="'.$row['2'].'"/></td>
  10. <td><input type="submit" value="EDYTUJ"></td>
  11. </tr>
  12. <tr>
  13. <td>Robots</td>
  14. <td><input type="text" name="robots" style="background:url(images/tlo2.png) repeat; font-weight: bold; color: #74777b; padding: 5px; width: 500px; font-size: 0.6em; border-style: none; -webkit-border-radius: 5px;-moz-border-radius: 5px;border-radius: 5px;" value="'.$row['3'].'"/></td>
  15. <td><input type="submit" value="EDYTUJ"></td>
  16. </tr>
  17. <tr>
  18. <td>Title</td>
  19. <td><input type="text" name="title" style="background:url(images/tlo2.png) repeat; font-weight: bold; color: #74777b; padding: 5px; width: 500px; font-size: 0.6em; border-style: none; -webkit-border-radius: 5px;-moz-border-radius: 5px;border-radius: 5px;" value="'.$row['4'].'"/></td>
  20. <td><input type="submit" value="EDYTUJ"></td>
  21. </tr>
  22. </form>


galileox86
Więc tak jak kolega wyżej napisał musisz przekazać jakoś informacje o akcji skoro ją filtrujesz tutaj bo inaczej nigdy nie zostanie ten if spelniony:

  1. if($_POST['action'] == 'update')


<form action="?action=update">

Poza tym ten formularz to chyba nie do tego:

Po pierwsze input

[html]

<form action='?action=update'>

</form>
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.