简体   繁体   English

在php类中更新而不使用主键

[英]update in php class without using primary key

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;
        }

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: 产品行之后,SET之前,下一行中的WHERE之前都没有空格,它将进行以下查询:

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 看看是否有效

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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