Hello I am getting the following error:
Fatal error: Call to a member function execute() on a non-object in C:\Program Files (x86)\EasyPHP-DevServer-14.1VC9\data\localweb\projects\japanArt\public_html\webAdmin\updateProduct.php on line 55
Line 55 is
$stmt->execute();
This code is sitting on my updateProduct file which is:
define('ALLOW_ACCESS', 1);
$title = 'Update a product';
require('../../incAdmin/incHead.php');
require_once('../../incAdmin/adminConnect.php');
<h2>Choose the product you wish to update:</h2>
if ($_SESSION['loggedIn']) {
if (isset($_POST['cmdSubmit'])) {
// Check if a radio button has been selected
$message = '';
if (empty($_POST['rdoChooseRec'])) {
$message = 'ERROR: Please choose a product';
// If no errors, REDIRECT to updateAction.php
if ($message == '') {
$_SESSION['updateID'] = $_POST['rdoChooseRec'];
// start defining the URL
$url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']);
// check for trailing slash
if ((substr($url, -1) == '/') OR (substr($url, -1) == '\\') ) {
$url = substr($url, 0, -1); // get rid of slash
// add the page name and create the header
$url .= '/updateAction.php';
header("Location: $url");
exit(0);
else { // this is the first time form will be displayed. Initialise variable
$message = '';
$_SESSION['updateID'] = '';
<form id="frmUpdateProduct" method="post" action="updateProduct.php">
<p><br />
<input type="submit" name="cmdSubmit" id="cmdSubmit" value="Proceed to EDIT screen" />
<span style="color:#F00;"><?php print $message; ?></span>
<br /><br />
// set up the SQL query
$sql = 'SELECT p.pImage, p.productID, c.cName, p.pName, p.pPrice FROM Category as c, Product as p WHERE c.categoryID = p.productID';
$stmt = $db->prepare($sql);
// Execute the statement
$stmt->execute();
// Bind PHP variables to the output from the prepared statement
$stmt->bind_result($pImage, $productID, $cName , $pName, $pPrice);
// Set up the table headings
print '<table width="70%">';
print '<tr><th> </th>';
print '<th class="leftText">Category</th>';
print '<th class="leftText">Product ID</th>';
print '<th class="leftText">Product Name</th>';
print '<th class="leftText">Image name</th>';
print '<th class="rightText">Price</th></tr>';
// Fetch and display the output
while ($stmt->fetch()) {
print "\n<tr>";
print '<td>';
print '<input type="radio" name="rdoChooseRec" id="rdoChooseRec" value="' . $productID . '" />';
print '</td>';
print '<td>';
print $cName;
print '</td>';
print '<td>';
print $productID;
print '</td>';
print '<td>';
print $pName;
print '</td>';
print '<td>';
print $pImage;
print '</td>';
print '<td class="rightText">';
printf("%0.2f", $pPrice); // format price to 2 decimal places
print '</td>';
print '</tr>';
// Close the statement
$stmt->close();
</table>
</form>
<!----------------------------------------------------------------------------->
else {
print 'ERROR: you are not authorised to access this page';
require('../../incAdmin/incFoot.php');
I am also attaching the updateAction file:
define('ALLOW_ACCESS', 1);
$title = 'Update details';
require('../../incAdmin/incHead.php');
require_once('../../incAdmin/adminConnect.php');
<h2>Update fields for product ID <?php print $_SESSION['updateID']; ?></h2>
if ($_SESSION['loggedIn']) {
if (isset($_POST['cmdSubmit'])) {
// CREATE VARIABLES from form's POST data
$new_categoryID = $_POST['cboCategoryID'];
$new_pName = $_POST['txtName'];
$new_pPrice = $_POST['txtPrice'];
$new_pImage = $_POST['txtImage'];
// VALIDATE THE FORM (This is very basic. In your Japan Art Print website, make the validation more comprehensive)
$message = '';
if (empty($new_pName)) {
$message = "ERROR: Product name is required";
if (empty($new_pPrice)) {
$message = $message . "\nERROR: Product price is required";
// If no errors, update the record in the database
if ($message == '') {
$productID = $_SESSION['updateID'];
$sql = "UPDATE Product SET categoryID = $new_categoryID, pName ='$new_pName', pPrice = $new_pPrice, pImage = '$new_pImage' WHERE productID = $productID";
if ($stmt = $db->prepare($sql)) {
$stmt->execute();
$message = "Update successful. \nNumber of records amended: " . $stmt->affected_rows;
$stmt->close();
else {
$message = "Error while attempting to update the record.";
else { // first time form is displayed: Initialise variables and obtain record from database
$new_categoryID = '';
$message = '';
// run the database query to find requested record
$sql = 'SELECT c.cDesc, p.categoryID, p.productID, p.pName, p.pPrice, p.pImage FROM Category as c, Product as p WHERE c.categoryID = p.categoryID and p.productID = ' . $_SESSION['updateID'];
if ($stmt = $db->prepare($sql)) {
$stmt->execute();
// Bind PHP variables to the output from the prepared statement
$stmt->bind_result($old_cDesc, $old_categoryID, $old_productID, $old_pName, $old_pPrice, $old_pImage);
// Fetch and the record so it can be displayed in the form
while ($stmt->fetch()) {
$new_categoryID = $old_categoryID;
$new_pName = $old_pName;
$new_pPrice = $old_pPrice;
$new_pImage = $old_pImage;
$stmt->close();
else {
$message = "*** ERROR: Could not read table to find requested record. \nPlease try again later.";
<form id="frmUpdate" method="post" action="updateAction.php">
<p><br />
<label>Category:</label>
<select name="cboCategoryID">
//Set up a drop-down list of categories
$sql = 'SELECT * FROM Category ORDER BY cDesc';
$stmt = $db->prepare($sql);
$stmt->execute();
$stmt->bind_result($row_categoryID, $row_cDesc);
while ($stmt->fetch()) {
print "\n<option ";
if ($row_categoryID == $new_categoryID) { print 'selected '; }
print 'value="';
print $row_categoryID;
print '">';
print $row_cDesc;
print '</option>';
$stmt->close();
</select>
<br /><br />
<label>Product Name:</label>
<input type="text" name="txtName" id="txtName" size="70" value="<?php print $new_pName; ?>" />
<br /><br />
<label>Product price:     $</label>
<input type="text" name="txtPrice" id="txtPrice" size="8" value="<?php printf('%0.2f', $new_pPrice); ?>" />
<br /><br />
<label>Image filename:</label>
<input type="text" name="txtImage" id="txtImage" size="30" value="<?php print $new_pImage; ?>" />
<br /><br />
<input type="submit" name="cmdSubmit" id="cmdSubmit" value="Update this record" />
<br /><br />
<label>Report:</label>
<textarea name="txtMessage" id="txtMessage" cols="60" rows="4" readonly="readonly"
style="background-color:#FFFFFF;color:#000000; overflow:hidden;"><?php print $message;?></textarea>
</form>
<!----------------------------------------------------------------------------->
else {
print 'ERROR: you are not authorised to access this page';
require('../../incAdmin/incFoot.php');
Thank you in advance to anyone who is able to lead me in the right direction to correct this error.
Somewhere in the code, the object
$db
needs to be instantiated. The method/ function
execute()
on the object
$stmt
doesn’t exist (non-object), because the object
$db
is missing.
Scott
I don’t understand. He doesn’t even have a database object to make a connection with, so how can setting up the mysql reporting level help?
Scott
Where? The error
Fatal error: Call to a member function execute() on a non-object
Clearly says he hasn’t yet instantiated the $db object.
Scott
$stmt = $db->prepare($sql);
Somewhere in the code, the object $db needs to be instantiated. The method/ function execute() on the object $stmt doesn’t exist (non-object), because the object $db is missing.
Nope. If there was no $db object then the error message would be “call to member function
prepare
on a non object”. So $db obviously exists. The prepare method is not returning a $stmt object probably because of a sql error.
ahundiak:
Nope. If there was no $db object then the error message would be “call to member function prepare on a non object”. So $db obviously exists. The prepare method is not returning a $stmt object probably because of a sql error.
Oh duh! I stand corrected.
Scott
Not “could” but “should”. You should always keep this line in your code.
You don’t have to “find” errors, in the meaning of dosing something special in case of error. You just have to make PHP always report errors for you, whenever it happens. So this command does.
Would you have it in place you would have your other problem already solved as well.
Why would you have to put it anywhere manually? Don’t you have it in the
adminConnect.php
already?
Like I said, it have to be put before the line where you connect to mysqli. Therefore, it should be always in place whenever you use mysqli. As simple as that.
I love this. Sitepoint PHP community getting a great job done. Teaching. I also learned from this thread. Thank you
@colshrapnel
.
Scott