简体   繁体   English

如果内容发生变化,MySQL将不会更新

[英]MySQL won't update if content is changed

I got a PHP and MySQL problem, when I update the headline I got no problem. 我遇到了PHP和MySQL问题,当我更新标题时我没有遇到任何问题。 But when updating while having changed the content it won't update at all. 但是在更改内容时进行更新时,它根本不会更新。

Any idea to what's going wrong. 知道出了什么问题。

$query = mysql_query("UPDATE about SET headline='$headline' WHERE content='$content'") 
or die(mysql_error());

$query = mysql_query("SELECT * FROM about ORDER BY id DESC");
while ($row = mysql_fetch_assoc($query))
{
   $id = $row['id'];
   $headline = $row['headline'];
   $content = $row['content'];

   header("location: ../");
}

I'm coding in PHP and using and MySQL server. 我用PHP编写代码并使用MySQL服务器。

I would prefer a solution that only calls the database once when updating. 我更喜欢一种只在更新时调用数据库一次的解决方案。

When using an UPDATE statement, the WHERE clause is used to specify the record(s) to update. 使用UPDATE语句时,WHERE子句用于指定要更新的记录。 In the following case, where you've modified both the headline and content: 在以下情况中,您修改了标题和内容:

UPDATE about SET headline='$headline' WHERE content='$content'

The above query attempts to update the headline field where the content field equals the new content. 上述查询尝试更新内容字段等于新内容的标题字段。 There won't be any rows where the existing content matches the new content. 现有内容与新内容不匹配,不会有任何行。

You'd have to do something similar to: 你必须做类似的事情:

UPDATE about SET headline='$headline', content='$new_content' WHERE content='$old_content'

However, you should generally be selecting a record by an identifier, not a content field. 但是,您通常应该按标识符而不是内容字段选择记录。

UPDATE about SET headline='$headline', content='$new_content' WHERE id = $id

If you update using a field like content, you may accidentally update all rows that have the same content, instead of just the row that you're trying to update. 如果使用类似内容的字段进行更新,则可能会意外更新具有相同内容的所有行,而不仅仅是您尝试更新的行。 Using a unique identifier allows you to update only the specified row. 使用唯一标识符可以仅更新指定的行。

You could store the identifier in the session (on the server side). 您可以将标识符存储在会话中(在服务器端)。

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

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