Contenu connexe Similaire à PHP 102: Out with the Bad, In with the Good (20) Plus de Jeremy Kendall (11) PHP 102: Out with the Bad, In with the Good1. PHP 102 Out with the Bad, In with the Good Memphis PHP, February 23, 2012 6. . . . I frequently caused as many problems as I solved. 21. index.php – db connection <?php $db = realpath ( dirname ( __FILE__ ) . '/data/db/bookshelf.db' ); $dsn = "sqlite: $db " ; $options = array ( PDO :: ATTR_ERRMODE => PDO :: ERRMODE_EXCEPTION , PDO :: ATTR_DEFAULT_FETCH_MODE => PDO :: FETCH_ASSOC ); try { $dbh = new PDO ( $dsn , null , null , $options ); } catch ( PDOException $e ) { echo "Error!: " . $e -> getMessage() . "<br /> " ; die (); } $books = $dbh -> query( " SELECT * FROM bookshelf ORDER BY title" ) -> fetchAll(); 22. index.php – db connection <?php $db = realpath ( dirname ( __FILE__ ) . '/data/db/bookshelf.db' ); $dsn = "sqlite: $db " ; $options = array ( PDO :: ATTR_ERRMODE => PDO :: ERRMODE_EXCEPTION , PDO :: ATTR_DEFAULT_FETCH_MODE => PDO :: FETCH_ASSOC ); try { $dbh = new PDO ($dsn, null , null , $options); } catch ( PDOException $e) { echo "Error!: " . $e -> getMessage() . "<br />"; die (); } $books = $dbh -> query(" SELECT * FROM bookshelf ORDER BY title") -> fetchAll() ; 23. index.php – db connection <?php $db = realpath ( dirname ( __FILE__ ) . '/data/db/bookshelf.db'); $dsn = "sqlite:$db"; $options = array ( PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION , PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC ); try { $dbh = new PDO ( $dsn , null , null , $options ); } catch ( PDOException $e ) { echo "Error!: " . $e -> getMessage() . "<br /> " ; die (); } $books = $dbh -> query(" SELECT * FROM bookshelf ORDER BY title") -> fetchAll(); 24. index.php – db connection <?php $db = realpath ( dirname ( __FILE__ ) . '/data/db/bookshelf.db'); $dsn = "sqlite:$db"; $options = array ( PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION , PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC ); try { $dbh = new PDO ($dsn, null , null , $options); } catch ( PDOException $e) { echo "Error!: " . $e -> getMessage() . "<br />"; die (); } $books = $dbh -> query( " SELECT * FROM bookshelf ORDER BY title" ) -> fetchAll(); 26. index.php – books table <?php if ( count ( $books ) > 0 ): ?> < table > < tr > < th > Title </ th >< th > Author </ th > </ tr > <?php foreach ( $books as $book ): ?> < tr > < td > < a href= "book-form.php?id=<?php echo $book ['id']; ?>" > <?php echo $book [ 'title' ]; ?> </ a > </ td > < td > <?php echo $book [ 'author' ]; ?> </ td > </ tr > <?php endforeach ; ?> </ table > <?php else : ?> < p > We have no books! </ p > <?php endif ; ?> 27. index.php – books table <?php if ( count ( $books ) > 0 ): ?> < table > < tr > < th > Title </ th >< th > Author </ th > </ tr > <?php foreach ( $books as $book ): ?> < tr > < td > < a href= "book-form.php?id=<?php echo $book ['id']; ?>" > <?php echo $book [ 'title' ]; ?> </ a > </ td > < td > <?php echo $book [ 'author' ]; ?> </ td > </ tr > <?php endforeach ; ?> </ table > <?php else : ?> < p >We have no books!</ p > <?php endif ; ?> 28. index.php – books table <?php if ( count ($books) > 0): ?> < table > < tr > < th >Title</ th >< th >Author</ th > </ tr > <?php foreach ( $books as $book ): ?> < tr > < td > < a href= "book-form.php?id=<?php echo $book ['id']; ?>" > <?php echo $book [ 'title' ]; ?> </ a > </ td > < td > <?php echo $book [ 'author' ]; ?> </ td > </ tr > <?php endforeach ; ?> </ table > <?php else : ?> < p >We have no books!</ p > <?php endif ; ?> 29. index.php – books table <?php if ( count ($books) > 0): ?> < table > < tr > < th >Title</ th >< th >Author</ th > </ tr > <?php foreach ($books as $book): ?> < tr > < td > < a href="book-form.php?id=<?php echo $book['id']; ?>"> <?php echo $book['title']; ?> </ a > </ td > < td > <?php echo $book['author']; ?> </ td > </ tr > <?php endforeach ; ?> </ table > <?php else : ?> < p > We have no books! </ p > <?php endif ; ?> 32. book-form.php <?php $id = empty ( $_GET [ 'id' ]) ? null : $_GET [ 'id' ]; if ( $id ) { // Database connection code $book = $dbh -> query( " SELECT title, author FROM bookshelf WHERE id = $id " ) -> fetch(); $title = $book [ 'title' ]; $author = $book [ 'author' ]; } 33. book-form.php <?php $id = empty ( $_GET [ 'id' ]) ? null : $_GET [ 'id' ]; if ($id) { // Database connection code $book = $dbh -> query(" SELECT title, author FROM bookshelf WHERE id = $id") -> fetch(); $title = $book['title']; $author = $book['author']; } 34. book-form.php <?php $id = empty ($_GET['id']) ? null : $_GET['id']; if ( $id ) { // Database connection code $book = $dbh -> query( " SELECT title, author FROM bookshelf WHERE id = $id " ) -> fetch(); $title = $book [ 'title' ]; $author = $book [ 'author' ]; } 35. book-form.php < form method= "post" action= "process-book.php" > < input type= "hidden" id= "id" name= "id" value= "<?php echo $id ; ?>" /> < dl > < dt > < label for= "title" > Title </ label > </ dt > < dd > < input type= "text" id= "title" name= "title" value= "<?php echo $title ; ?>" /> </ dd > < dt > < label for= "author" > Author </ label > </ dt > < dd > < input type= "text" id= "author" name= "author" value= "<?php echo $author ; ?>" /> </ dd > < dt > </ dt > < dd > < input type= "submit" value= "Submit" /> </ dd > </ dl > </ form > 36. book-form.php < form method= "post" action= "process-book.php" > < input type="hidden" id="id" name="id" value= "<?php echo $id ; ?>" /> < dl > < dt > < label for="title">Title</ label > </ dt > < dd > < input type="text" id="title" name="title" value= "<?php echo $title ; ?>" /> </ dd > < dt > < label for="author">Author</ label > </ dt > < dd > < input type="text" id="author" name="author" value= "<?php echo $author ; ?>" /> </ dd > < dt > </ dt > < dd > < input type="submit" value="Submit" /> </ dd > </ dl > </ form > 37. process-book.php <?php if ( strtolower ( $_SERVER [ 'REQUEST_METHOD' ]) == 'get' ) { header ( "Location: /" ); } // Database connection code if ( empty ( $_POST [ 'id' ])) { $sql = " INSERT INTO bookshelf (title, author) " . "VALUES ('{ $_POST ['title']}', '{ $_POST ['author']}')" ; $dbh -> exec( $sql ); } else { $sql = " UPDATE bookshelf SET title = '{ $_POST ['title']}', " . "author = '{ $_POST ['author']}' WHERE id = { $_POST ['id']}" ; $dbh -> exec( $sql ); } header ( "Location: /" ); 38. process-book.php <?php if ( strtolower ( $_SERVER [ 'REQUEST_METHOD' ]) == 'get' ) { header ( "Location: /" ); } // Database connection code if ( empty ($_POST['id'])) { $sql = " INSERT INTO bookshelf (title, author) " . "VALUES ('{$_POST['title']}', '{$_POST['author']}')"; $dbh -> exec($sql); } else { $sql = " UPDATE bookshelf SET title = '{$_POST['title']}', " . "author = '{$_POST['author']}' WHERE id = {$_POST['id']}"; $dbh -> exec($sql); } header ("Location: /"); 39. process-book.php <?php if ( strtolower ($_SERVER['REQUEST_METHOD']) == 'get') { header ("Location: /"); } // Database connection code if ( empty ($_POST['id'])) { $sql = " INSERT INTO bookshelf (title, author) " . "VALUES ('{$_POST['title']}', '{$_POST['author']}')"; $dbh -> exec($sql); } else { $sql = " UPDATE bookshelf SET title = '{$_POST['title']}', " . "author = '{$_POST['author']}' WHERE id = {$_POST['id']}"; $dbh -> exec($sql); } header ("Location: /"); 40. process-book.php <?php if ( strtolower ($_SERVER['REQUEST_METHOD']) == 'get') { header ("Location: /"); } // Database connection code if ( empty ( $_POST [ 'id' ])) { $sql = " INSERT INTO bookshelf (title, author) " . "VALUES ('{ $_POST ['title']}', '{ $_POST ['author']}')" ; $dbh -> exec( $sql ); } else { $sql = " UPDATE bookshelf SET title = '{$_POST['title']}', " . "author = '{$_POST['author']}' WHERE id = {$_POST['id']}"; $dbh -> exec($sql); } header ("Location: /"); 41. process-book.php <?php if ( strtolower ($_SERVER['REQUEST_METHOD']) == 'get') { header ("Location: /"); } // Database connection code if ( empty ($_POST['id'])) { $sql = " INSERT INTO bookshelf (title, author) " . "VALUES ('{$_POST['title']}', '{$_POST['author']}')"; $dbh -> exec($sql); } else { $sql = " UPDATE bookshelf SET title = '{ $_POST ['title']}', " . "author = '{ $_POST ['author']}' WHERE id = { $_POST ['id']}" ; $dbh -> exec( $sql ); } header ("Location: /"); 42. process-book.php <?php if ( strtolower ($_SERVER['REQUEST_METHOD']) == 'get') { header ("Location: /"); } // Database connection code if ( empty ($_POST['id'])) { $sql = " INSERT INTO bookshelf (title, author) " . "VALUES ('{$_POST['title']}', '{$_POST['author']}')"; $dbh -> exec($sql); } else { $sql = " UPDATE bookshelf SET title = '{$_POST['title']}', " . "author = '{$_POST['author']}' WHERE id = {$_POST['id']}"; $dbh -> exec($sql); } header ( "Location: /" ); 53. base.php <?php date_default_timezone_set ('America/Chicago'); error_reporting ( - 1); ini_set ('display_errors', 1); ini_set ('display_startup_errors', 1); $db = realpath ( dirname ( __FILE__ ) . '/../data/db/bookshelf.db' ); $dsn = "sqlite: $db " ; $options = array ( PDO :: ATTR_ERRMODE => PDO :: ERRMODE_EXCEPTION , PDO :: ATTR_DEFAULT_FETCH_MODE => PDO :: FETCH_ASSOC ); try { $dbh = new PDO ( $dsn , null , null , $options ); } catch ( PDOException $e ) { throw $e ; echo "Error!: " . $e -> getMessage() . "<br /> " ; die (); } 54. base.php <?php date_default_timezone_set ( 'America/Chicago' ); error_reporting ( - 1); ini_set ('display_errors', 1); ini_set ('display_startup_errors', 1); $db = realpath ( dirname ( __FILE__ ) . '/../data/db/bookshelf.db'); $dsn = "sqlite:$db"; $options = array ( PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION , PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC ); try { $dbh = new PDO ($dsn, null , null , $options); } catch ( PDOException $e) { throw $e; echo "Error!: " . $e -> getMessage() . "<br />"; die (); } 55. base.php <?php date_default_timezone_set ('America/Chicago'); error_reporting ( - 1 ); ini_set ( 'display_errors' , 1 ); ini_set ( 'display_startup_errors' , 1 ); $db = realpath ( dirname ( __FILE__ ) . '/../data/db/bookshelf.db'); $dsn = "sqlite:$db"; $options = array ( PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION , PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC ); try { $dbh = new PDO ($dsn, null , null , $options); } catch ( PDOException $e) { throw $e; echo "Error!: " . $e -> getMessage() . "<br />"; die (); } 59. We echo $title and $author < form method="post" action="process-book.php"> < input type="hidden" id="id" name="id" value="<?php echo $id; ?>" /> < dl > < dt > < label for="title">Title</ label > </ dt > < dd > < input type="text" id="title" name="title" value= "<?php echo $title ; ?>" /> </ dd > < dt > < label for="author">Author</ label > </ dt > < dd > < input type="text" id="author" name="author" value= "<?php echo $author ; ?>" /> </ dd > < dt > </ dt > < dd > < input type="submit" value="Submit" /> </ dd > </ dl > </ form > 60. Without defining $title and $author require_once dirname ( __FILE__ ) . '/library/base.php' ; $id = empty ( $_GET [ 'id' ]) ? null : $_GET [ 'id' ]; if ( $id ) { $book = $dbh -> query( . . . ) -> fetch(); $title = $book [ 'title' ]; $author = $book [ 'author' ]; } 61. Super easy to fix require_once dirname ( __FILE__ ) . '/library/base.php'; $id = empty ($_GET['id']) ? null : $_GET['id']; $title = null ; $author = null ; if ($id) { $book = $dbh -> query( . . . ) -> fetch(); $title = $book['title']; $author = $book['author']; } 73. book-form.php: After $statement = $dbh -> prepare( ' SELECT title, author FROM bookshelf WHERE id = :id' ); $statement -> bindParam( ':id' , $id ); $statement -> execute(); $book = $statement -> fetch(); 74. book-form.php: After $statement = $dbh -> prepare (' SELECT title, author FROM bookshelf WHERE id = :id'); $statement -> bindParam(':id', $id); $statement -> execute(); $book = $statement -> fetch(); 75. book-form.php: After $statement = $dbh -> prepare( ' SELECT title, author FROM bookshelf WHERE id = :id' ); $statement -> bindParam(':id', $id); $statement -> execute(); $book = $statement -> fetch(); 76. book-form.php: After $statement = $dbh -> prepare(' SELECT title, author FROM bookshelf WHERE id = :id'); $statement -> bindParam( ':id' , $id ); $statement -> execute(); $book = $statement -> fetch(); 77. book-form.php: After $statement = $dbh -> prepare(' SELECT title, author FROM bookshelf WHERE id = :id'); $statement -> bindParam(':id', $id); $statement -> execute(); $book = $statement -> fetch(); 78. process-book.php: Before if ( empty ( $_POST [ 'id' ])) { $sql = " INSERT INTO bookshelf (title, author) " . "VALUES ('{ $_POST ['title']}', '{ $_POST ['author']}')" ; $dbh -> exec( $sql ); } else { $sql = " UPDATE bookshelf SET title = '{ $_POST ['title']}', " . "author = '{ $_POST ['author']}' WHERE id = { $_POST ['id']}" ; $dbh -> exec( $sql ); } 79. process-book.php: After if ( $id ) { $statement = $dbh -> prepare( " UPDATE bookshelf SET title = :title, author = :author WHERE id = :id" ); $statement -> bindParam( ':title' , $title ); $statement -> bindParam( ':author' , $author ); $statement -> bindParam( ':id' , $id ); $statement -> execute(); } else { $statement = $dbh -> prepare( " INSERT INTO bookshelf (title, author) VALUES (:title, :author)" ); $statement -> bindParam( ':title' , $title ); $statement -> bindParam( ':author' , $author ); $statement -> execute(); } 80. process-book.php: After if ( $id ) { $statement = $dbh -> prepare( " UPDATE bookshelf SET title = :title, author = :author WHERE id = :id" ); $statement -> bindParam(':title', $title); $statement -> bindParam(':author', $author); $statement -> bindParam(':id', $id); $statement -> execute(); } else { $statement = $dbh -> prepare( " INSERT INTO bookshelf (title, author) VALUES (:title, :author)" ); $statement -> bindParam(':title', $title); $statement -> bindParam(':author', $author); $statement -> execute(); } 81. process-book.php: After if ($id) { $statement = $dbh -> prepare(" UPDATE bookshelf SET title = :title, author = :author WHERE id = :id"); $statement -> bindParam( ':title' , $title ); $statement -> bindParam( ':author' , $author ); $statement -> bindParam( ':id' , $id ); $statement -> execute(); } else { $statement = $dbh -> prepare(" INSERT INTO bookshelf (title, author) VALUES (:title, :author)"); $statement -> bindParam( ':title' , $title ); $statement -> bindParam( ':author' , $author ); $statement -> execute(); } 82. process-book.php: After if ($id) { $statement = $dbh -> prepare(" UPDATE bookshelf SET title = :title, author = :author WHERE id = :id"); $statement -> bindParam(':title', $title); $statement -> bindParam(':author', $author); $statement -> bindParam(':id', $id); $statement -> execute(); } else { $statement = $dbh -> prepare(" INSERT INTO bookshelf (title, author) VALUES (:title, :author)"); $statement -> bindParam(':title', $title); $statement -> bindParam(':author', $author); $statement -> execute(); } 96. BookshelfService.php namespace Bookshelfervice; class BookshelfService { private $_dbh ; public function __construct (PDO $dbh ) { } public function find ( $id ) { } public function findAll () { } public function save ( array $options ) { } } 98. BookshelfService.php public function find ( $id ) { $sql = ' SELECT * FROM bookshelf WHERE id = :id' ; $statement = $this -> _dbh -> prepare( $sql ); $statement -> bindParam( ':id' , $id ); $statement -> execute(); return $statement -> fetch(); } public function findAll () { $sql = ' SELECT * FROM bookshelf ORDER BY title' ; return $this -> _dbh -> query( $sql ) -> fetchAll(); } 99. BookshelfService.php public function save ( array $options ) { if ( $options [ 'id' ]) { $statement = $this -> _dbh -> prepare( " UPDATE bookshelf SET title = :title, author = :author WHERE id = :id" ); $statement -> execute( $options ); } else { unset ( $options [ 'id' ]); $statement = $this -> _dbh -> prepare( " INSERT INTO bookshelf (title, author) VALUES (:title, :author)" ); $statement -> execute( $options ); } } 102. base.php function autoload ( $className ) { $className = ltrim ( $className , ' ' ); $fileName = '' ; $namespace = '' ; if ( $lastNsPos = strripos ( $className , ' ' )) { $namespace = substr ( $className , 0 , $lastNsPos ); $className = substr ( $className , $lastNsPos + 1 ); $fileName = str_replace ( ' ' , DIRECTORY_SEPARATOR , $namespace ) . DIRECTORY_SEPARATOR ; } $fileName .= str_replace ( '_' , DIRECTORY_SEPARATOR , $className ) . '.php' ; require $fileName ; } spl_autoload_register ( 'autoload' ); 103. base.php // database connection code $bookshelf = new BookshelferviceBookshelfService ( $dbh ); 105. index.php: Before <?php require_once dirname ( __FILE__ ) . '/library/base.php' ; $books = $dbh -> query( " SELECT * FROM bookshelf ORDER BY title" ) -> fetchAll(); ?> 106. index.php: After <?php require_once dirname ( __FILE__ ) . '/library/base.php' ; $books = $bookshelf -> findAll(); ?> 107. book-form.php: Before if ( $id ) { $statement = $dbh -> prepare( ' SELECT title, author FROM bookshelf WHERE id = :id' ); $statement -> bindParam( ':id' , $id ); $statement -> execute(); $book = $statement -> fetch(); $title = $book['title']; $author = $book['author']; } 108. book-form.php: After if ( $id ) { $book = $bookshelf -> find( $id ); $title = $book['title']; $author = $book['author']; } 109. process-book.php: Before if ( $id ) { $statement = $dbh -> prepare( " UPDATE bookshelf SET title = :title, author = :author WHERE id = :id" ); $statement -> bindParam( ':title' , $title ); $statement -> bindParam( ':author' , $author ); $statement -> bindParam( ':id' , $id ); $statement -> execute(); } else { $statement = $dbh -> prepare( " INSERT INTO bookshelf (title, author) VALUES (:title, :author)" ); $statement -> bindParam( ':title' , $title ); $statement -> bindParam( ':author' , $author ); $statement -> execute(); } 118. PHP Manual: http://www.php.net/manual/en/index.php 120. PHP 101 Suggestions: http://csiphp.com/blog/2011/07/19/stop-doing-it-wrong-and-learn-to-code-good-too/ 121. PHP 101: PHP for the Absolute Beginner: http://devzone.zend.com/6/php-101-php-for-the-absolute-beginner/