简体   繁体   中英

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.

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();
}
?>

After your update execution you need to query again for fetching result like,

$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();

$query = $dbh->prepare("SELECT * FROM data.check");
$query->execute();
$data_update= $query -> fetchAll(PDO::FETCH_ASSOC);// now it will get records

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM