Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // static function for access by scope resulation. self keyword for access class
- class Database
- {
- private static $dbName = 'test' ;
- private static $dbHost = 'localhost' ;
- private static $dbUsername = 'root';
- private static $dbUserPassword = '';
- private static $cont = null;
- public static function connect()
- {
- self::$cont = new PDO( "mysql:host=".self::$dbHost.";"."dbname=".self::$dbName, self::$dbUsername, self::$dbUserPassword);
- return self::$cont;
- }
- }
- // insert class . after onsubmit run it
- class project_Insert{
- public static function insert($sql) {
- $pdo = Database::connect();
- $insert = $pdo->query($sql);
- if($insert){
- echo "inserted";
- }
- }
- }
- // view class . after onsubmit run it
- class project_View{
- public static function view($sql) {
- $pdo = Database::connect();
- foreach ($pdo->query($sql) as $row) {
- echo '<tr>';
- echo '<td>'. $row['name'] . '</td>';
- echo '</tr>';
- }
- }
- }
- // check insert button
- if(isset($_POST['submit'])){
- $name = $_POST['name'];
- $sql = "insert into abc values('','$name')";
- $insert = project_Insert::insert($sql);
- }
- // check view button
- if(isset($_POST['view'])){
- $pdo = Database::connect();
- $sql = 'SELECT * FROM abc';
- $insert = project_View::view($sql);
- }
- ?>
- <form action="patter.php" method="post">
- <input type="text" name="name">
- <input type="submit" name="submit" value="submit">
- <input type="submit" name="view" value="view">
- </form>
Add Comment
Please, Sign In to add comment