Guest User

Untitled

a guest
Jan 13th, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. <?php
  2.  
  3.  
  4. // static function for access by scope resulation. self keyword for access class
  5.  
  6. class Database
  7. {
  8. private static $dbName = 'test' ;
  9. private static $dbHost = 'localhost' ;
  10. private static $dbUsername = 'root';
  11. private static $dbUserPassword = '';
  12.  
  13. private static $cont = null;
  14.  
  15. public static function connect()
  16. {
  17. self::$cont = new PDO( "mysql:host=".self::$dbHost.";"."dbname=".self::$dbName, self::$dbUsername, self::$dbUserPassword);
  18.  
  19. return self::$cont;
  20. }
  21.  
  22. }
  23.  
  24.  
  25.  
  26.  
  27. // insert class . after onsubmit run it
  28.  
  29.  
  30. class project_Insert{
  31.  
  32. public static function insert($sql) {
  33.  
  34. $pdo = Database::connect();
  35.  
  36. $insert = $pdo->query($sql);
  37.  
  38. if($insert){
  39. echo "inserted";
  40. }
  41.  
  42. }
  43.  
  44. }
  45.  
  46.  
  47.  
  48.  
  49.  
  50. // view class . after onsubmit run it
  51.  
  52.  
  53. class project_View{
  54.  
  55. public static function view($sql) {
  56.  
  57. $pdo = Database::connect();
  58.  
  59. foreach ($pdo->query($sql) as $row) {
  60. echo '<tr>';
  61. echo '<td>'. $row['name'] . '</td>';
  62. echo '</tr>';
  63. }
  64.  
  65. }
  66.  
  67. }
  68.  
  69.  
  70.  
  71. // check insert button
  72.  
  73.  
  74.  
  75. if(isset($_POST['submit'])){
  76.  
  77. $name = $_POST['name'];
  78.  
  79. $sql = "insert into abc values('','$name')";
  80.  
  81. $insert = project_Insert::insert($sql);
  82.  
  83. }
  84.  
  85.  
  86.  
  87.  
  88. // check view button
  89.  
  90.  
  91. if(isset($_POST['view'])){
  92.  
  93. $pdo = Database::connect();
  94.  
  95. $sql = 'SELECT * FROM abc';
  96.  
  97. $insert = project_View::view($sql);
  98.  
  99.  
  100. }
  101.  
  102.  
  103.  
  104. ?>
  105.  
  106.  
  107.  
  108. <form action="patter.php" method="post">
  109.  
  110. <input type="text" name="name">
  111. <input type="submit" name="submit" value="submit">
  112. <input type="submit" name="view" value="view">
  113.  
  114. </form>
Add Comment
Please, Sign In to add comment