dsfdsf46465 2016-08-17 09:21
浏览 64
已采纳

PHP MySQL更新查询数组(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.

  • 写回答

2条回答 默认 最新

  • matlabmann 2016-08-17 09:30
    关注

    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();
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?