不使用主键

时间:2015-12-31 22:10:42

标签: php mysqli

我正在尝试从表单更新数据,但所有代码都工作得很好,除非它没有在数据库上更新。

这是我的实体类

        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;

        }

这是查询类

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

这是控制器类

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

设置值没有错误..可以看到值..它也显示数据更新但在数据库中没有变化..

[被修改]

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 个答案:

答案 0 :(得分:1)

我相信你的错误可能就在这里:

$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'",

产品之前和SET之前没有空格,也在下面一行中的WHERE之前,这将进行此查询:

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

将其更改为:

$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'",

看看它是否有效