简体   繁体   English

PHP或MySQL不会更新数据库

[英]PHP or MySQL wont update database

Hey guys so I have been trying to figure out why my update function wont update my database. 大家好,所以我一直在尝试弄清为什么我的更新功能无法更新我的数据库。

Here is the function: 这是函数:

public function update() {

    if ( is_null( $this->id ) ) triger_error ("DailyMenuItem::update(): Attempt to update a DailyMenuItem object that does not have its ID property set.", E_USER_ERROR);

    $conn = new PDO ( DB_DSN, DB_USERNAME, DB_PASSWORD );
    $sql = "UPDATE daily_menu SET date=FROM_UNIXTIME(:date), name=:name, description=:description, price=:price WHERE id = :id";
    $st = $conn->prepare ( $sql );
    $st->bindValue( ":name", $this->name, PDO::PARAM_STR );
    $st->bindValue( ":description", $this->description, PDO::PARAM_STR );
    $st->bindValue( ":price", $this->price, PDO::PARAM_INT );
    $st->bindValue( ":date", $this->date, PDO::PARAM_INT);
    $st->execute();
    $conn = null;
}

This object contains this 这个物件包含这个

DailyMenuItem Object ( [id] => 4 [name] => Smoked Chicken Baguette [description] => w/goat cheese [price] => 13 [date] => 1330585200 ) DailyMenuItem对象([id] => 4 [name] =>熏鸡面包[description] => w /山羊奶酪[price] => 13 [date] => 1330585200)

I get no error message. 我没有收到错误消息。 It just does not update anything. 它只是不更新​​任何内容。 Please help :) 请帮忙 :)

You don't seem to be binding id. 您似乎没有绑定ID。

Could that be your problem. 可能是您的问题。

In every update and delete query you need an primary key value on which you are performing query. 在每个更新和删除查询中,您都需要一个用于执行查询的主键值。 Here in your example you have mentioned ID in the query but there is no bind below so with out the id query will not know which row to be updated. 在您的示例中,您在查询中提到了ID,但是下面没有绑定,因此,如果没有ID查询,将不知道要更新哪一行。

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

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