The meaning of PHP Fatal error: Call to a member function bind_param() on boolean
comes from not taking into account that $stmt
could be worth false
(a Boolean value, not a mysqli resource) in case of an error in prepare
.
A Boolean data has no properties or methods (like the method called bind_param()
that you try to use). Hence the message: "You can not call a member function of a Boolean data".
You can find out what is wrong with your code if you shield it correctly from error situations. It is a bad practice (and will cause situations like this) not to do it.
Obviously the error comes from having incorrectly concatenated the character strings because "categoria=?" . "where id_producto=?"
results in "categoria=?where id_producto=?"
, a SQL syntax error.
In the following example I show you how to shield your code before error conditions and avoid the concatenation of strings, prone to this type of errors:
public function modificar_producto($datos) {
/* No es necesario concatenar cadenas, se puede hacer de esta manera */
$sql = '
UPDATE productos SET
codigo_articulo=?,
titulo_producto=?,
contenido_producto=?,
precio_producto=?,
cantidad_disponible=?,
imagen_producto=?,
categoria=?
WHERE id_producto=?
';
$stmt = $this->consulta_prepared($sql);
/* Comprobamos si la preparación se finalizó con éxito o hubo error */
if ($stmt === false) {
/* Puedes hacer un return con ok a false o lanzar una excepción */
/*throw new Exception('Error en prepare: ' . $stmt->error);*/
return [ 'ok' => 'false' ];
}
$stmt->bind_param('sssiissi',
$datos[0],
$datos[1],
$datos[2],
$datos[3],
$datos[4],
$datos[5],
$datos[6],
$datos[7]
);
$stmt->execute();
if ($stmt === false) {
/* De nuevo puedes lanzar una excepción o devolver un error */
/*throw new Exception('Error en execute: ' . $stmt->error);*/
return [ 'ok' => 'false' ];
} else {
return [ 'ok' => 'true' ];
}
}
I, personally, prefer the use of exceptions. When you call the function modificar_producto
you can control an error condition easily:
try {
modificar_producto($datos);
/* Todo fue OK si llegamos a esta línea */
} catch (Exception $e) {
/* Podemos finalizar la ejecución con un mensaje o mostrar HTML con él */
die('Error modificando producto: ' . $e->getMessage());
}
Returning an array with an element ok
to true
or false
will prevent or make it difficult to debug the source of the error.