I'm trying to edit lines from a database and when I try I can't.
Here as I call the process:
$economia = new Economia('', '','', $validador-> obtener_ingreso(), $validador-> obtener_gasto(), $validador -> obtener_nota());
var_dump($economia);
echo "</br>";
$economia_editada = RepositorioEconomia :: editar_economia(Conexion :: obtener_conexion(), $economia, $id);
var_dump($economia_editada);
Here $economy and $economy_edited:
object(Economia)#3 (6) { ["id_factura":"Economia":private]=> string(0) "" ["cliente":"Economia":private]=> string(0) "" ["fecha_dato":"Economia":private]=> string(0) "" ["ingreso":"Economia":private]=> string(1) "5" ["gasto":"Economia":private]=> string(2) "50" ["nota":"Economia":private]=> string(5) "wfweg" }
bool(false)
Aqui editar_economia Aqui deberia de rellenar un array con los datos y enviarlos:
public static function editar_economia($conexion, $economia, $id){
$economia_editada = [];
if (isset($conexion)){
try{
$sql = "UPDATE economia SET ingreso = :ingreso, gasto = :gasto, nota = :nota WHERE id_factura = :id";
$sentencia = $conexion -> prepare($sql);
$sentencia->bindParam(':id', $id, PDO::PARAM_STR);
$gasto = $economia->obtener_gasto();
$sentencia->bindParam(':gasto', $gasto, PDO::PARAM_STR);
$ingreso = $economia->obtener_ingreso();
$sentencia->bindParam(':ingreso', $ingreso, PDO::PARAM_STR);
$nota = $economia->obtener_nota();
$sentencia -> bindParam(':nota', $nota, PDO::PARAM_STR);
$sentencia -> execute();
$economia_editada = $sentencia -> fetch();
}catch (PDOException $ex){
print 'ERROR'. $ex->getMessage();
}
}
return $economia_editada;
}
Your query is of type
UPDATE
, and PDO has a specific method to know the number of affected rows in queries of typeDELETE
,INSERT
, orUPDATE
, that method isrowCount()
:It is important that you keep this in mind, also the second paragraph, because there are countless examples, tutorials and answers even here that do not know how to differentiate, using it
fetch
for queries in which it should not be used (as is your case in this question) or usingrowCount()
for type queriesSELECT
ignoring this can fail on some DBMS.Now we are going to apply it
rowCount()
to your case and we will improve other aspects that I will comment on at the end.If you notice, I have preferred an array-based style as an answer. The convention is simple: there will be a key
status
that will befalse
when some problem has occurred, and it will betrue
when rows have been updated. In both cases there will be a keymsg
with an informative message.To consume the response you would do something like this: