dongzhao8233 2015-12-31 22:10
浏览 34
已采纳

在不使用主键的情况下在php类中更新

I m Trying to update a data from form but all code is work fine execept it doesnot update on database..

here is my Entities class

        class ClothingEntities {
        public $PID;
        public $PCODE;
        public $productname;
        public $color;
        public $size;
        public $stock;
        public $price;
        public $image;
        public $date;
        public $review;
        public $made;
        public $type;


        function __construct($PID, $PCODE, $productname, $color, $size, $stock, $price, $image, $date, $review, $made, $type) {
            $this->PID = $PID;
            $this->PCODE = $PCODE;
            $this->productname = $productname;
            $this->color = $color;
            $this->size = $size;
            $this->stock = $stock;
            $this->price = $price;
            $this->image = $image;
            $this->date = $date;
            $this->review = $review;
            $this->made = $made;
            $this->type = $type;

        }

Here Is query Class

function UpdateCloth($PCODE,ClothingEntities $cloth){
          require 'connection.php';
           $link=mysqli_connect($host, $user, $pass, $db);
           $link1=mysqli_select_db($link, $db);
           $query=  sprintf("UPDATE product"
                   . "SET productname='%s',color='%s',size='%s',stock='%s',price='%s',image='%s',date='%s',review='%s',made='%s',type='%s'"
                   . "WHERE PCODE='$PCODE'",
                    mysqli_real_escape_string($link,$cloth->productname),
                    mysqli_real_escape_string($link,$cloth->color),
                    mysqli_real_escape_string($link,$cloth->size),
                    mysqli_real_escape_string($link,$cloth->stock),
                    mysqli_real_escape_string($link,$cloth->price),
                    mysqli_real_escape_string($link,"image/".$cloth->image),
                    mysqli_real_escape_string($link,$cloth->date),
                    mysqli_real_escape_string($link,$cloth->review),
                    mysqli_real_escape_string($link,$cloth->made),
                    mysqli_real_escape_string($link,$cloth->type));
            $result=  mysqli_query($link, $query);
           mysqli_close($link);
           return $result;
        }

Here is the controller class

function UpdateCloth($PCODE){
    $PID=$_POST['txtPID'];
    $PCODE=$_POST['txtPCODE'];
    $productname=$_POST['txtproductname'];
    $color=$_POST['txtcolor'];
    $size=$_POST['txtsize'];
    $stock=$_POST['txtstock'];
    $price=$_POST['txtprice'];
    $image=$_POST['txtimage'];
    $date=$_POST['txtdate'];
    $review=$_POST['txtreview'];
    $made=$_POST['txtmade'];
    $type=$_POST['txttype'];        
    $cloth=new ClothingEntities($PID,$PCODE,$productname, $color, $size, $stock, $price, $image, $date, $review, $made, $type);
    $clothModel = new ClothingModel();
    $clothModel->UpdateCloth($PCODE,$cloth);
}

There is No error on setting the value.. the value can be seen.. and it also display data is updated but in database there will be no change..

[Edited]

function UpdateCloth($PCODE,ClothingEntities $cloth){
          require 'connection.php';
           $link=mysqli_connect($host, $user, $pass, $db);
           $link1=mysqli_select_db($link, $db);
           $query=  sprintf("UPDATE product "
                   . " SET productname='%s',color='%s',size='%s',stock='%s',price='%s',image='%s',date='%s',review='%s',made='%s',type='%s'"
                   . " WHERE PCODE='$PCODE'",
                    mysqli_real_escape_string($link,$cloth->productname),
                    mysqli_real_escape_string($link,$cloth->color),
                    mysqli_real_escape_string($link,$cloth->size),
                    mysqli_real_escape_string($link,$cloth->stock),
                    mysqli_real_escape_string($link,$cloth->price),
                    mysqli_real_escape_string($link,"image/".$cloth->image),
                    mysqli_real_escape_string($link,$cloth->date),
                    mysqli_real_escape_string($link,$cloth->review),
                    mysqli_real_escape_string($link,$cloth->made),
                    mysqli_real_escape_string($link,$cloth->type));
            $result=  mysqli_query($link, $query)or die(mysql_error());
           mysqli_close($link);
           return $result;
        }
  • 写回答

1条回答 默认 最新

  • dongping6974 2015-12-31 22:17
    关注

    I believe your error might be in here:

    $query=  sprintf("UPDATE product"
                       . "SET productname='%s',color='%s',size='%s',stock='%s',price='%s',image='%s',date='%s',review='%s',made='%s',type='%s'"
                       . "WHERE PCODE='$PCODE'",
    

    There is no space after product nor before SET, also before WHERE in the line below, which would make this query:

    UPDATE productSETproductname='%s',...,type='%s'WHERE PCODE='$PCODE';
    

    Change it to this:

    $query=  sprintf("UPDATE product "
                       . " SET productname='%s',color='%s',size='%s',stock='%s',price='%s',image='%s',date='%s',review='%s',made='%s',type='%s'"
                       . " WHERE PCODE='$PCODE'",
    

    See if it works

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测