简体   繁体   English

MySQL和PHP | 选择后更新不起作用

[英]MySQL & PHP | Update doesnt work after select

I have this weird situation where my query isn't doing what it's supposed to do. 我有这种奇怪的情况,我的查询没有执行应做的事情。

Here is my not working code: 这是我不工作的代码:

$aanbodID = 1;
$db = //connection

$getData = $db->query("SELECT boekbaar_iframe FROM aanbod_20160206 WHERE id_aanbod=$aanbodID") or die(mysql_error());
while ($row_content = mysql_fetch_array($getData)) 
{       
    $zichtbaarjanee = $row_content['boekbaar_iframe']; // 0 or 1
}

if ($zichtbaarjanee == 0)
{
    $nieuwewaarde = 1;
}else{
    $nieuwewaarde = 0;
}

db->query("UPDATE aanbod_20160206 SET boekbaar_iframe = '$nieuwewaarde' WHERE id_aanbod = '$aanbodID'");
echo mysql_error();

And here is almost the same code that is working: 这里几乎是相同的代码:

 $aanbodID = 1;
$db = //connection

$getData = $db->query("SELECT boekbaar_iframe FROM aanbod_20160206 WHERE id_aanbod=$aanbodID") or die(mysql_error());
while ($row_content = mysql_fetch_array($getData)) 
{       
    $zichtbaarjanee = $row_content['boekbaar_iframe']; // 0 or 1
}

//switch these vars and its working    
$nieuwewaarde = 0;
// $nieuwewaarde = 1; 

db->query("UPDATE aanbod_20160206 SET boekbaar_iframe = '$nieuwewaarde' WHERE id_aanbod = '$aanbodID'");
echo mysql_error();

So i'm guessing the problem is somewhere in the if statement, but i've tried everything, also with and without ' ' or " ". 所以我猜问题出在if语句中,但是我已经尝试了所有方法,无论是否带有''或“”。

update: changed querys to mysqli_* 更新:将查询更改为mysqli_ *

the problem is still in the if/else statement 问题仍然在if / else语句中

because this is working: 因为这有效:

//if ($zichtbaarjanee == 0)
//{
//  $nieuwewaarde = 1;
//}else{
    $nieuwewaarde = 0;
//}

When I switch the 0 for a 1 it's also working, but when i comment in the piece of code it stops updating the table 当我将0切换为1时,它也起作用,但是当我在代码段中注释时,它将停止更新表

Please help! 请帮忙!

Every instance of MySQL needs to be replaced with MySQL i As it was deprecated in PHP 5.5. MySQL的每个实例都需要替换为MySQL i,因为它在PHP 5.5中已弃用。

http://php.net/manual/en/book.mysqli.php http://php.net/manual/zh/book.mysqli.php

And trying to concatenate: 并尝试串联:

mysql_query("UPDATE aanbod_20160206 SET boekbaar_iframe = ".$nieuwewaarde." WHERE id_aanbod = ".$aanbodID.""); 

Should be changed to: 应更改为:

mysqli_query("UPDATE aanbod_20160206 SET boekbaar_iframe = '$nieuwewaarde' WHERE id_aanbod = '$aanbodID'");

I understand you're trying to embed the variable in the query, however in this case it would just add 2 dots on either side of your variable. 我了解您正在尝试将变量嵌入查询中,但是在这种情况下,它只会在变量的任一侧添加2个点。

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

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