28mikes Posted January 10, 2014 Share Posted January 10, 2014 ******* Its now resolved ********* I keep getting an error saying the messages below, I have been to those lines and tried changing everything I can think of but it is still failing. Notice: Trying to get property of non-object in C:\Users\hp\Documents\Web_Publishing Coursework\xampp\htdocs\WebPub\templates\viewProduct.php on line 15 Notice: Trying to get property of non-object in C:\Users\hp\Documents\Web_Publishing Coursework\xampp\htdocs\WebPub\templates\viewProduct.php on line 16 Notice: Undefined variable: row in C:\Users\hp\Documents\Web_Publishing Coursework\xampp\htdocs\WebPub\templates\viewProduct.php on line 17 Published on Notice: Trying to get property of non-object in C:\Users\hp\Documents\Web_Publishing Coursework\xampp\htdocs\WebPub\templates\viewProduct.php on line 18 1 January 1970 Here is the code of the 2 files I think the problem might be in, This is the view file that is producing the error above <?php include "templates/includes/header.php"; ?> <!-- end of top nav --> <header><!-- header --> <div id="plandesign"><img src="images/plans.png" alt="" /></div> <h1><a href="#">Student Bucket</a></h1> <p>Welcome to the University of Surrey Student Bucket, a place to grab yourself a bargain or sell your unwanted items to other students</p> </header><!-- end of header --> <section id="main"><!-- #main content and sidebar area --> <section id="content"><!-- #content --> <article> <h2><?php echo htmlspecialchars( $results['products']->productId)?></h2> <p><?php echo $results['products']->productDesc?></p> <p>Published on <?php echo date('j F Y', $results['products']->publicationDate)?></p> </article> </section><!-- end of #content --> <!-- sidebar --> <?php include 'templates/includes/sidebar.php'; ?> <!-- end of sidebar --> </section><!-- end of #main content and sidebar--> <?php include "templates/includes/footer.php"; ?> And below is the class that control everything <?php /** * @author Owner * @copyright 2014 */ /** * Class to handle articles */ class Product { // Properties /** * @var int The article ID from the database */ public $productId = null; /** * @var int The article ID from the database */ public $title = null; /** * @var int The article ID from the database */ public $subtitle = null; /** * @var int The article ID from the database */ public $category = null; /** * @var int The article ID from the database */ public $featured = null; /** * @var int The article ID from the database */ public $productDesc = null; /** * @var int The article ID from the database */ public $price = null; /** * @var int The article ID from the database */ public $publicationDate = null; /** * @var string Full title of the article */ public $universityId= null; /** * @var string A short summary of the article */ public $userId = null; /** * @var string The HTML content of the article */ public $picture = null; /** * Sets the object's properties using the values in the supplied array * * @param assoc The property values */ public function __construct( $data=array() ) { //if ( isset( $data['id'] ) ) $this->id = (int) $data['id']; no need because this is automatically if ( isset( $data['productId'] ) ) $this->productId = (int) $data['productId']; if ( isset( $data['title'] ) ) $this->title = preg_replace ( "/[^\.\,\-\_\'\"\@\?\!\:\$ a-zA-Z0-9()]/", "", $data['title'] ); if ( isset( $data['subtitle'] ) ) $this->subtitle = preg_replace ( "/[^\.\,\-\_\'\"\@\?\!\:\$ a-zA-Z0-9()]/", "", $data['subtitle'] ); if ( isset( $data['categoryId'] ) ) $this->categoryId = (int) $data['categoryId']; if ( isset( $data['featured'] ) ) $this->featured = (int) $data['featured']; if ( isset( $data['productDesc'] ) ) $this->productDesc = $data['productDesc']; if ( isset( $data['price'] ) ) $this->price = (int) $data['price']; if ( isset( $data['publicationDate'] ) ) $this->publicationDate = (int) $data['publicationDate']; //if ( isset( $data['universityId'] ) ) $this->universityId = (int) $data['universityID']; //if ( isset( $data['userId'] ) ) $this->userId = (int) $data['userID']; if ( isset( $data['picture'] ) ) $this->picture = preg_replace ( "/[^\.\,\-\_\'\"\@\?\!\:\$ a-zA-Z0-9()]/", "", $data['picture'] ); } /** public function __construct( $data=array() ) { //if ( isset( $data['id'] ) ) $this->id = (int) $data['id']; no need because this is automatically $this->productId = (int) $data['productId']; $this->title = preg_replace ( "/[^\.\,\-\_\'\"\@\?\!\:\$ a-zA-Z0-9()]/", "", $data['title'] ); $this->subtitle = preg_replace ( "/[^\.\,\-\_\'\"\@\?\!\:\$ a-zA-Z0-9()]/", "", $data['subtitle'] ); $this->categoryId = (int) $data['categoryId']; $this->featured = (int) $data['featured']; $this->productDesc = $data['productDesc']; $this->price = (int) $data['price']; $this->publicationDate = (int) $data['publicationDate']; //$this->universityId = (int) $data['universityID']; //$this->userId = (int) $data['userID']; $this->picture = preg_replace ( "/[^\.\,\-\_\'\"\@\?\!\:\$ a-zA-Z0-9()]/", "", $data['picture'] ); } **/ /** * Sets the object's properties using the edit form post values in the supplied array * * @param assoc The form post values */ public function storeFormValues ( $params ) { // Store all the parameters $this->__construct( $params ); // Parse and store the publication date if ( isset($params['publicationDate']) ) { $publicationDate = explode ( '-', $params['publicationDate'] ); if ( count($publicationDate) == 3 ) { list ( $y, $m, $d ) = $publicationDate; $this->publicationDate = mktime ( 0, 0, 0, $m, $d, $y ); } } } /** * Returns an Product object matching the given article ID * * @param int The product ID * @return Product|false The product object, or false if the record was not found or there was a problem */ public static function getById( $id ) { $conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD ); $sql = "SELECT *, UNIX_TIMESTAMP(publicationDate) AS publicationDate FROM Products WHERE id = :id"; $st = $conn->prepare( $sql ); $st->bindValue( ":id", $id, PDO::PARAM_INT ); $st->execute(); $row = $st->fetch(); $conn = null; if ( $row ) return new Product( $row ); } /** * Returns all (or a range of) Product objects in the DB * * @param int Optional The number of rows to return (default=all) * @param string Optional column by which to order the articles (default="publicationDate DESC") * @return Array|false A two-element array : results => array, a list of Article objects; totalRows => Total number of articles */ public static function getList( $numRows=1000000, $order="publicationDate DESC" ) { $conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD ); $sql = "SELECT SQL_CALC_FOUND_ROWS *, UNIX_TIMESTAMP(publicationDate) AS publicationDate FROM products ORDER BY " . mysql_real_escape_string($order) . " LIMIT :numRows"; $st = $conn->prepare( $sql ); $st->bindValue( ":numRows", $numRows, PDO::PARAM_INT ); $st->execute(); $list = array(); while ( $row = $st->fetch() ) { $productRow = new Product( $row ); $list[] = $productRow; } // Now get the total number of products that matched the criteria $sql = "SELECT FOUND_ROWS() AS totalRows"; $totalRows = $conn->query( $sql )->fetch(); $conn = null; return ( array ( "results" => $list, "totalRows" => $totalRows[0] ) ); } /** * Inserts the current Article object into the database, and sets its ID property. */ public function insert() { // Does the Article object already have an ID? if ( !is_null( $this->id ) ) trigger_error ( "Article::insert(): Attempt to insert an Article object that already has its ID property set (to $this->id).", E_USER_ERROR ); // Insert the Article $conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD ); $sql = "INSERT INTO articles ( title, subtitle, categoryID, featured, productDesc, price, publicationDate, universityId, userId, picture ) VALUES ( FROM_UNIXTIME(:publicationDate), :title, :summary, :content )"; $st = $conn->prepare ( $sql ); $st->bindValue( ":title", $this->title, PDO::PARAM_INT ); $st->bindValue( ":subtitle", $this->subtitle, PDO::PARAM_STR ); $st->bindValue( ":categoryId", $this->categoryId, PDO::PARAM_STR ); $st->bindValue( ":featured", $this->featured, PDO::PARAM_STR ); $st->bindValue( ":productDesc", $this->productDesc, PDO::PARAM_INT ); $st->bindValue( ":price", $this->price, PDO::PARAM_STR ); $st->bindValue( ":publicationDate", $this->publicationDate, PDO::PARAM_INT ); $st->bindValue( ":universityId", $this->universityId, PDO::PARAM_STR ); $st->bindValue( ":userId", $this->userId, PDO::PARAM_STR ); $st->bindValue( ":picture", $this->picture, PDO::PARAM_INT ); $this->id = $conn->lastInsertId(); $conn = null; } /** * Updates the current Article object in the database. */ public function update() { // Does the Article object have an ID? if ( is_null( $this->id ) ) trigger_error ( "Product::update(): Attempt to update an Article object that does not have its ID property set.", E_USER_ERROR ); // Update the Article $conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD ); $sql = "UPDATE articles SET publicationDate=FROM_UNIXTIME(:publicationDate), title=:title, summary=:summary, content=:content WHERE id = :id"; $st = $conn->prepare ( $sql ); $st->bindValue( ":title", $this->title, PDO::PARAM_STR ); $st->bindValue( ":subtitle", $this->subtitle, PDO::PARAM_STR ); $st->bindValue( ":categoryId", $this->categoryId, PDO::PARAM_INT ); $st->bindValue( ":featured", $this->featured, PDO::PARAM_INT ); $st->bindValue( ":productDesc", $this->productDesc, PDO::PARAM_STR ); $st->bindValue( ":price", $this->price, PDO::PARAM_STR ); $st->bindValue( ":publicationDate", $this->publicationDate, PDO::PARAM_INT ); $st->bindValue( ":picture", $this->picture, PDO::PARAM_STR ); $st->execute(); $conn = null; } /** * Deletes the current Article object from the database. */ public function delete() { // Does the Article object have an ID? if ( is_null( $this->id ) ) trigger_error ( "Article::delete(): Attempt to delete an Article object that does not have its ID property set.", E_USER_ERROR ); // Delete the Article $conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD ); $st = $conn->prepare ( "DELETE FROM products WHERE id = :id LIMIT 1" ); $st->bindValue( ":id", $this->id, PDO::PARAM_INT ); $st->execute(); $conn = null; } } ?> Link to comment https://forums.phpfreaks.com/topic/285257-help-with-notice-trying-to-get-property-of-non-object-in/ Share on other sites More sharing options...
28mikes Posted January 10, 2014 Author Share Posted January 10, 2014 I was making an error in my database query hence it was not creating an object. The column name I was calling didnt exist and after changing that it is now working. public static function getById( $id ) { $conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD ); $sql = "SELECT *, UNIX_TIMESTAMP(publicationDate) AS publicationDate FROM Products WHERE productId = :id"; $st = $conn->prepare( $sql ); $st->bindValue( ":id", $id, PDO::PARAM_INT ); $st->execute(); $row = $st->fetch(); $conn = null; if ( $row ) return new Product( $row ); } I have changed the WHERE id to WHERE productId. Link to comment https://forums.phpfreaks.com/topic/285257-help-with-notice-trying-to-get-property-of-non-object-in/#findComment-1464693 Share on other sites More sharing options...
mac_gyver Posted January 10, 2014 Share Posted January 10, 2014 there's a Mark Solved button at the lower-right-hand side of any post. i hit it for you in this thread. Link to comment https://forums.phpfreaks.com/topic/285257-help-with-notice-trying-to-get-property-of-non-object-in/#findComment-1464708 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.