arijulianto

Submit Multipel Data dengan PHP

Jan 10th, 2014
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.34 KB | None | 0 0
  1. // cara input multipel data ke database
  2.  
  3. /** insert multipel data **/
  4. // file multipel-insert-kategori.php
  5. <?php
  6. include "koneksi.php";
  7.  
  8. echo "<form action=\"tambah.php\" method=\"post\"><table>
  9. <tr>
  10.     <th>No.</th>
  11.     <th>Nama Kategori</th>
  12.     <th>Slug</th>
  13. </tr>";
  14. $jml_data = 10;
  15. for($i=1;$i<=$jml_data;$i++){
  16. echo "<tr>
  17.     <td align=\"center\">$i.</td>
  18.     <td><input type=\"text\" name=\"nama_kategori[]\" /></td>
  19.     <td><input type=\"text\" name=\"slug[]\" /></td>
  20. </tr>";
  21. }
  22. echo "</table><input type=\"submit\" name=\"simpan\" value=\"Simpan Perubahan\" /></form>";
  23. ?>
  24.  
  25.  
  26. // file tambah.php
  27. <?php
  28. include "koneksi.php";
  29. if(isset($_POST['simpan'])){
  30. // loop data
  31. foreach($_POST['nama_kategori'] as $index=>$nama_kategori){
  32.     $slug = $_POST['slug'][$index];
  33.     if(trim($nama_kategori)!="" && trim($slug)!="") // validasi inputan tidak kosong
  34.         echo("INSERT into kategori(nama_kategori,slug) VALUES('$nama_kategori','$slug')<br />");
  35. }
  36. echo "<script>history.back()</script>";
  37. }
  38. ?>
  39.  
  40.  
  41. /** update multipel data **/
  42. // file tampildata.php
  43. <?php
  44. echo "<form action=\"simpan.php\" method=\"post\">
  45. <table>
  46. <tr>
  47.     <th>No.</th>
  48.     <th>Nama Kategori</th>
  49.     <th>Slug</th>
  50.     <th>Hapus</th>
  51. </tr>";
  52. $no = 1;
  53. $sql = mysql_query("SELECT * from kategori order by nama_kategori");
  54. while($data = mysql_fetch_array($sql)){
  55. echo "<tr>
  56.     <td align=\"center\">$no.<input type=\"hidden\" name=\"id[]\" value=\"$data[idkategori]\" /></td>
  57.     <td><input type=\"text\" name=\"nama_kategori[]\" size=\"40\" value=\"$data[nama_kategori]\" /></td>
  58.     <td><input type=\"text\" name=\"slug[]\" size=\"40\" value=\"$data[slug]\" /></td>
  59.     <td align=\"center\"><input type=\"text\" name=\"hapus[]\" size=\"40\" value=\"1\" /></td>
  60. </tr>";
  61. }
  62. echo "</table><p><input type=\"submit\" name=\"simpan\" value=\"Simpan Perubahan \" /></p></form>";
  63. ?>
  64.  
  65. // file simpan.php
  66. <?php
  67. include "koneksi.php";
  68. if(isset($_POST['simpan'])){
  69. // loop data
  70. foreach($_POST['id'] as $index=>$idkategori){
  71.     $nama_kategori= $_POST['nama_kategori'][$index];
  72.     $slug = $_POST['slug'][$index];
  73.     $hapus= $_POST['hapus'][$index];
  74.     // update
  75.       mysql_query("UPDATE kategori set nama_kategori='$nama_kategori', slug='$slug' where idkategori='$idkategori'");
  76.     // hapus data
  77.     if($hapus)
  78.         mysql_query("DELETE from kategori where idkategori='$idkategori'");
  79. }
  80. echo "<script>history.back()</script>";
  81. }
  82. ?>
Advertisement
Add Comment
Please, Sign In to add comment