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.

6
  • 2
    Because update queries don't return any data. Commented Aug 17, 2016 at 9:22
  • you are using update.you should use select to return any data Commented Aug 17, 2016 at 9:23
  • Also think about what happens if I send ...&code=0%20or%201=1 in the GET request. This code is wide open to SQL injection. Commented Aug 17, 2016 at 9:28
  • I think you got enough time to go through the answer given below. Atleast respond to the answer @ZouZou. Commented Aug 17, 2016 at 12:50
  • @NanaPartykar I just tested your code. Commented Aug 17, 2016 at 13:33

2 Answers 2

2

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();
}
?>
Sign up to request clarification or add additional context in comments.

8 Comments

Why do you have mysqli and pdo?
My Bad @Ivan . Thanks For Pointing Out.
I have a problem PDO not throwing exception with unbound parameters
Why you want to throw exception? Code is working or not? Data coming or not @zouzou.?
@NanaPartykar data is not coming :(
|
1

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

1 Comment

Why not just have one query with two statements (UPDATE and SELECT)?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.