Advertisement
divaramadania

Untitled

Feb 13th, 2019
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.46 KB | None | 0 0
  1. <html>
  2. <head>
  3. <title>insert data in database using PDO (php data object)</title>
  4. <link rel="stylesheet" type="text/css" href="style.css">
  5. </head>
  6. <body>
  7.  
  8. <div id="main">
  9. <h2>Input Data</h2>
  10. <div id="login">
  11. <h2>Data Mata Pelajaran</h2>
  12. </hr>
  13. <form action="" method="post">
  14. <label>Nama:</label>
  15. <input type="text" name="nama" id="name" required"="required"
  16. placeholder="Mata Pelajaran"/><br /><br />
  17. <label>Jurusan:</label>
  18. <input type="text" name="nama" id="name" required"="required"
  19. placeholder="Jurusan"/><br /><br />
  20. <label>Kelas:</label>
  21. <input type="text" name="nama" id="name" required"="required"
  22. placeholder="Kelas"/><br /><br />
  23. <input type="submit" value=" simpan data" name="submit"/><br />
  24. </form>
  25. </div>
  26. <!-- Right Side Div -->
  27.  
  28. </div>
  29. <?php
  30. if(isset($_POST["submit"])){
  31. $hostname='localhost';
  32. $username='root';
  33. $password='';
  34.  
  35. try {
  36. $dbh = new PDO
  37. ("mysql:host=$hostname;dbname=eresha",$username,$password);
  38.  
  39. $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); //<== add this line
  40. $sql = "INSERT INTO mahasiswa (nama, jurusan)
  41. VALUES ('".$_POST["nama"]."','".$_POST["jurusan"]."')";
  42. if ($dbh->query($sql)) {
  43. echo "<script type= 'text/javascript'>alert
  44. ('New Record Inserted Successfully Inserted.');<script>";
  45. }
  46. else{
  47. echo "<script type= 'text/javascript'>alert
  48. ('Data not Successfully Inserted.');</script>";
  49. }
  50. header("location:datamhs.php");
  51. $dbh = null;
  52. }
  53. catch(PDOException $e)
  54. {
  55. echo $e->getMessage();
  56. }
  57.  
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement