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

时间:2016-03-29 14:16:51

标签: php mysql if-statement sql-update

我有这种奇怪的情况,我的查询并没有做它应该做的事情。

这是我的工作代码:

$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();

这几乎是相同的代码:

 $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();

所以我猜这个问题是在if语句的某个地方,但是我已经尝试了所有的东西,无论有没有' '或" &#34 ;.

更新:将查询更改为mysqli _ *

问题仍在if / else语句中

因为这有效:

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

当我将0切换为1时,它也正常工作,但当我在该段代码中发表评论时,它会停止更新表格

请帮忙!

1 个答案:

答案 0 :(得分:0)

MySQL的每个实例都需要用MySQL i 替换 因为它在PHP 5.5中被弃用。

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

试图连接:

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

应改为:

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

我知道你试图在查询中嵌入变量,但在这种情况下,它只会在变量的两边添加2个点。