Hello, I am studying a way to increment a value before saving to the database but I need help, searching in Google for parts I was able to assemble the code and yes it works, its objective is to update a value in the database, but I'm finding the code confusing could anyone help me?
<?php
try{
$id = 1;
$pdo = new PDO("mysql:host=localhost;dbname=isbn_db", "root", "");
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
//busca no banco de dados
$stmt2 = $pdo->prepare("SELECT * FROM isbn ORDER BY numero_isbn");
$stmt2->execute();
//fecAll busca todos os resultados
$results = $stmt2->fetchALL(PDO::FETCH_ASSOC);
//BUSCA O VALOR NO BANCO DE DADOS
foreach ($results as $value){
$var = $value['numero_isbn'];
}
//incrementa o valor obtido no banco
$var = $var + 10000000000;
$stmt = $pdo->prepare('UPDATE isbn SET numero_isbn = :numero_isbn WHERE id = :id');
$stmt->execute(array(
':id' => $id,
':numero_isbn' => $var
));
// echo $stmt->rowCount();
echo "ISBN: ".$var;
}catch(PDOException $e){
echo 'Error: '. $e->getMessage();
echo "ERRO";
}
?>