Advertisement
Guest User

Untitled

a guest
Apr 3rd, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. if(isset($_POST["submit"])){
  2. //$hostname='localhost';
  3. //$username='root';
  4. //$password='';
  5.  
  6.  
  7. try {
  8. $dbh = new PDO("mysql:host=$hostname;dbname=college",$username,$password);
  9.  
  10. $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  11. $sql = "INSERT INTO students (student_name, student_email, student_city)
  12. VALUES ('".$_POST["stu_name"]."','".$_POST["stu_email"]."','".$_POST["stu_city"]."')";
  13. if ($conn->query($sql)){
  14. echo "<script type= 'text/javascript'>alert('New Record Inserted Successfully');</script>";
  15. }
  16. else{
  17. echo "<script type= 'text/javascript'>alert('Data not successfully Inserted.');</script>";
  18. }
  19.  
  20. $dbh = null;
  21. }
  22. catch(PDOException $e)
  23. {
  24. echo $e->getMessage();
  25. }
  26.  
  27. }
  28.  
  29. require_once('../config/database.php'); //file with connection to db
  30.  
  31. class Category {
  32. ...
  33. public function create() {
  34. ???
  35. }
  36.  
  37. class Database {
  38.  
  39. public function getConnection() {
  40. $servername = "localhost";
  41. $username = "root";
  42. $password = "";
  43. try {
  44. $conn = new PDO("mysql:host=$servername;dbname=demo", $username, $password);
  45. // set the PDO error mode to exception
  46. $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  47. echo "Connected successfully with the database!<br/><br/>";
  48. return $conn;
  49.  
  50. } catch (PDOException $e) {
  51. echo "Connection failed with the database!<br/><br/> " . $e->getMessage();
  52. }
  53.  
  54. }
  55.  
  56. }
  57.  
  58. $db = new Database();
  59. $conn = $db->getConnection();
  60.  
  61.  
  62. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement