[英]update in php class without using primary key
我正在嘗試從表單更新數據,但是所有代碼都可以正常工作,除非它不會在數據庫上更新。
這是我的實體課程
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;
}
我相信您的錯誤可能在這里:
$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'",
看看是否有效
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.