Advertisement
LrdArc

Form php | intip.in/insert

Dec 23rd, 2014
504
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.93 KB | None | 0 0
  1. <?php
  2. // ubah variable dibawah
  3. $db_user = 'root';
  4. $db_pass = 'password';
  5. $db_name = 'namaDB';
  6.  
  7. // connect to database
  8. $dsn = 'mysql:dbname=' . $db_name . ';charset=utf8;host=localhost';
  9. try {
  10.     $dbh = new PDO( $dsn, $db_user, $db_pass );
  11. } catch ( PDOException $e ) {
  12.     echo 'Connection failed: ' . $e->getMessage();
  13. }
  14.  
  15. // action when posted
  16. if ( $_POST ) {
  17.     $table = 'order'; // ubah ke nama tabel
  18.     $field = 'id,pesanan'; // kolomnya, kalo > 1 pisahkan pakai koma
  19.     $val = '?,?'; // ini sesuai jumlah kolomnya, pakai koma
  20.     $array = array( $_POST['id'], $_POST['pesanan'] ); // sesuai jumlah kolom juga
  21.  
  22.     $sth = $dbh->prepare( "INSERT INTO $table ($field) VALUES ($val)" );
  23.     $r = $sth->execute( $array );
  24.     echo ( $r ) ? 'Pesanan sudah disimpan.' : 'Gagal menyimpan pesanan.';
  25. }
  26.  
  27. ?>
  28. <form method="post">
  29.     <input type="text" name="pesanan"><!-- Tambahin input nya sesuai kebutuhan -->
  30.     <button type="submit">Pesan</button>
  31. </form>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement