简体   繁体   English

MySQL简单查询

[英]MySQL simple query

I have no idea what I'm doing wrong, but I'm not really good with MySQL. 我不知道我在做什么错,但是我对MySQL并不是很好。 I basically have a table in my database that has the column "status". 我的数据库中基本上有一个表,其中包含“状态”列。 If the statement is true I want to change the status to "1", and if its false I want to change the status to "0". 如果语句为true,则要将状态更改为“ 1”,如果语句为false,则将状态更改为“ 0”。

Where I'm having trouble, is the MySQL query, I have the PHP part all set up and working (tested it with echo). 我遇到麻烦的地方是MySQL查询,我所有的PHP部分都已设置并正常工作(已通过echo测试)。

My current query: (this is what happens after the { of my PHP if-statement) 我目前的查询:(这是在{if if语句的{之后)发生的情况

mysql_query('UPDATE page WHERE id="'.$id.'" SET status="1"');

The way I define $id: 我定义$ id的方式:

if(isset($_GET['id']))
{
$id = intval($_GET['id']);
}

Thanks ahead! 谢谢你!

Since you want to update all records (from your description) you can do 由于您要更新所有记录(根据您的描述),因此可以

UPDATE page
SET `status` = case when id = $id 
                   then 1
                   else 0 
               end

It should be: 它应该是:

mysql_query('UPDATE page SET status="1" WHERE id="$id"');

If using mysqli: 如果使用mysqli:

mysqli_query($cxn, 'UPDATE page SET status="1" WHERE id="$id"');

Change the order of SET and WHERE like this: 像这样更改SET和WHERE的顺序:

mysql_query('UPDATE page SET status=1 WHERE id="'.$id.'" ');

And you do not need the double " around a integer on MySQL. 而且您在MySQL上不需要在整数附近的double。

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

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