zouzou zouzou - 3 months ago 6
MySQL Question

PHP MySQL Update query array(0) { }

Can anybody help me to understand why my query update dosen't update my data in my database.

This my code php :

<?php

$code = $_GET['code'];
$n1= $_GET['n1'];
$n2= $_GET['n2'];
$n3 = $_GET['n3'];

try {
$connexion= new PDO('mysql:host=localhost;dbname=data','mydata','password');
$sql_update = "UPDATE data.check SET numb_1='".$n1."',numb_2='".$n2."','numb_3'='".n3."' WHERE 'code_product' =".$code;
$query = $connexion-> prepare($sql_update);
$query -> execute();
$data_update= $query -> fetchAll(PDO::FETCH_ASSOC);
}

catch(PDOException $e)
{
echo "<br>" . $e->getMessage();
}


Thanks for any help.

Answer

1) Change

$sql_update = "UPDATE data.check SET  numb_1='" . $n1 . "',numb_2='" . $n2 . "','numb_3'='" . n3 . "' WHERE 'code_product' =" . $code;

To

$sql_update = "UPDATE data.check SET  numb_1='" . $n1 . "',numb_2='" . $n2 . "','numb_3'='" . $n3 . "' WHERE `code_product` =" . $code;

=> In n3 you forgot to add $. And, replace single quotes with backtick to enclose column name.

Updated Code

<?php

$code = $_GET['code'];
$n1 = $_GET['n1'];
$n2 = $_GET['n2'];
$n3 = $_GET['n3'];

try {

  $connexion = new PDO('mysql:host=localhost;dbname=data', 'mydata', 'password');

  $sql_update = $connexion->prepare("UPDATE `data`.`check` SET numb_1 = :numb_1 , numb_2 = :numb_2, numb_3 = :numb_3 WHERE `code_product` = :code_product");
  $sql_update->execute(array(':numb_1' => $n1,':numb_2'=>$n2, ':numb_3'=>$n3,':code_product'=>$code));


  $stmt = $connexion->prepare("SELECT * FROM `data`.`check` WHERE code_product=:code_product");
  $stmt->execute(array(':code_product'=>$code));
  $data_update= $stmt -> fetchAll(PDO::FETCH_ASSOC);

} catch (PDOException $e) {
  echo "<br>" . $e->getMessage();
}
?>