The html form value in which it’s failing at is
<form action="page.php?action=<?php echo $results['formAction']?>" method="post" enctype="multipart/form-data">
<input type="hidden" name="Id" value="<?php echo $results['example']->Id ?>"/>
The class name is “Example” and is constructed like this :
[php]
class Example {
public $Id = null;
public function __construct( $data=array() ) {
if ( isset( $data[‘Id’] ) ) $this->Id = (int) $data[‘Id’];
}
public function storeFormValues ( $params ) {
$this->__construct( $params );
}
public static function getById( $Id ) {
$conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
$sql = “SELECT *, UNIX_TIMESTAMP(exampleDate) AS exampleDate FROM example 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 Example( $row );
}
public function insert() {
if (!is_null( $this->Id ) ) trigger_error ( “Example::insert(): Attempt to insert an Example object that already has its ID property set (to $this->Id).”, E_USER_ERROR );
// Inserts other form values for db table if no error
}
}
// This is the form script on another doc/file. All is being connected through a config file within the pages.
function newExamlpeObject() {
$results = array();
$results[‘formAction’] = “newExamlpeObject”;
if ( isset( $_POST[‘saveChanges’] ) ) {
$item = new Example;
$item->storeFormValues( $_POST );
$item->insert();
header( “Location: admin.php?status=changesSaved” );
} elseif ( isset( $_POST[‘cancel’] ) ) {
// User has cancelled their edits
header( “Location: page.php” );
}
else {
// User has not posted the form yet: display the form
$results[‘example’] = new Example;
require( PAGES_PATH . “/theform.php” );
}
}
[/php]
The error that I’m recieving is :
Fatal error: Example::insert(): Attempt to insert an Example object that already has its ID property set (to 0). In Example.php - This is the doc/file
The table has no data so how is the property value set? The sql table for id is set like this :
TABLE examples
Id smallint unsigned NOT NULL auto_increment,
#other elements within this table
I’m wondering how it’s reading a value which is not set. Thanks to all in advance for any assistance on this issue.