布尔值不会更改为false


Boolean will not change to false

当用户出售商品时,它会给钱并使其为false,但此程序会给钱,但不会将布尔值设置为false。

<? require("Left.php"); ?>
<html>
    <img src="images/X100.png" style="width:304px;height:228px">
    <p>The X100 is a miner that was designed in 1980, it is cheap and horrible, it will yeild little gold and uses lots of fuel</p>
    <table width="" height="1" border="1" align="center">
    <tr>
        <td>
            Fuel usage
            <td>50</td>
        </td>
    </tr>
    </table>
    <table width="" height="1" border="1" align="center">
        <td>
            Max gold per ton
            <td>3</td>
        </td>
    </table>
    <table width="" height="1" border="1" align="center">
        <td>
            Price
            <td>6000 money</td>
        </td>
    </table>
    <form method="post" >
<input type="submit" name="buy" id="buy" value="buy">
</form>
<? if($ownstarter == true){
    echo 'Sell your X100 for 3000 money';
    echo "<form method='post' > <input type='submit' name='Sell' id='Sell' value='Sell'> </form>";
}?>
</form>
</html>
<?
 if (isset($_POST['buy']) and $money >= 6000 and $ownstarter == false) {
    $money = $money - 6000;
    $ownstarter = true;
} elseif ($money <= 6000 and $ownstarter == false) {
    echo "You cannot afford this! ";
} elseif($ownstarter == true) {
    echo "You already own this item!";
 }
if(isset($_POST['Sell'])){
    $money = $money + 3000;
    $ownstarter = null;
}
$query = mysql_query($sql) or die(mysql_error());
mysql_query("UPDATE users SET ownstarter=$ownstarter WHERE id='$id'");
mysql_query("UPDATE users SET money=$money WHERE id='$id'");
 ?>
 <? require("Right.php"); ?>

更详细地说,如果用户拥有它,一个带有"出售"选项的按钮,如果用户点击该按钮,它会给他们5000美元,并将ownstarter设置为false。但它不会将其设置为false。(我也厌倦了将其设置为null,它也不起作用)它确实给了钱,但没有设置为false,这样按钮就不会消失。如果有帮助的话,这是它的照片http://puu.sh/cC7Kx/69c55ce1dc.jpg

编辑:好的,所以我向查询添加了或die(mysql_error());,我得到了这个错误:您的SQL语法有错误;在第1行的'WHERE id='3029'附近,查看与MySQL服务器版本相对应的手册,以获得要使用的正确语法。但是x200页面有几乎完全相同的代码(我还没有向它添加销售部分),并且它工作时没有任何错误。X200

<? require("Left.php"); ?>
<html>
    <img src="images/X200.jpg" style="width:304px;height:228px">
    <p>The X100 miner is a more efficient miner than the X100 miner, that being said, the X200 will still use alot of fuel for little gold. </p>
    <table width="" height="1" border="1" align="center">
    <tr>
        <td>
            Fuel usage
            <td>70</td>
        </td>
    </tr>
    </table>
    <table width="" height="1" border="1" align="center">
        <td>
            Max gold per ton
            <td>5</td>
        </td>
    </table>
    <table width="" height="1" border="1" align="center">
        <td>
            Price
            <td>1 point</td>
        </td>
    </table>
    <form method="post" >
<input type="submit" name="buy" id="buy" value="buy">
</form>
</html>
<?
 if (isset($_POST['buy']) and $points >= 1 and $ownminer1000 == false) {
    $ownminer1000 = true;
    $points = $points - 1;
 }elseif($points < 1 and $ownminer1000 == false){
    echo "You cannot afford this item!";
 }elseif($ownminer1000){
    echo "You already have this item!";
 }
$query = mysql_query($sql) or die(mysql_error());
mysql_query("UPDATE users SET ownminer1000=$ownminer1000 WHERE id='$id'")
or die(mysql_error());
mysql_query("UPDATE users SET points=$points WHERE id='$id'")
or die(mysql_error());
 ?>
 <? require("Right.php"); ?>

问题

使用PHP将true/false布尔化为$ownerminer1000的值,并将它们不带引号地传递到MySQL查询中,这会导致一个小问题。

当PHP将布尔值true强制转换为字符串(在查询中必须这样做)时,它会替换字符串1。您对true的查询有效,因为它扩展到:

SET ownminer1000=1 WHERE id='xxx'

$ownerminer1000的值为false时,PHP将false强制转换为空字符串。结果是您的查询看起来像:

SET ownminer1000= WHERE id='xxx'
---------------^^^  

PHP的著名/臭名昭著的类型杂耍规则中详细介绍了这些内容

最简单的修复方法:

这在语法上是无效的。如果您想继续对该变量使用布尔值,则需要在将其传递到MySQL之前对其进行转换。您可以最容易地做到这一点,首先将其强制转换为整数。

// true becomes 1, false becomes 0
$ownerminer1000 = intval($ownerminer1000);
mysql_query("UPDATE users SET ownminer1000=$ownminer1000 WHERE id='$id'") or die(mysql_error());

现在,查询在语法上是有效的,并且对于false值将成功。

我注意到另一件事——您正在对具有相同$id的同一个表执行两个单独的UPDATE语句。然后可以安全地将它们组合为一个,在SET子句中有多个field=value对。

// Update points and ownerminer1000 at the same time
mysql_query("UPDATE users SET points=$points, ownminer1000=$ownminer1000 WHERE id='$id'") or die(mysql_error()); 

弃用警告:

标准免责声明适用于mysql_*()函数的使用。一年多前,它们在PHP 5.5中被弃用,新代码应该使用而不是。相反,现在是开始学习使用PDO或MySQLi的时候了。两者都是较新的API,都支持准备好的语句,这将提高查询的安全性。我们看不到上面变量$money, $points, $id的来源,但如果它们源自用户输入,则可能容易受到SQL注入的攻击。

这个针对MySQL开发人员的PDO教程非常好,并且在旧的mysql_*()函数的上下文中定义了PDO的使用。最重要的是开始学习使用prepare()/execute()。如果准备一个带有绑定占位符值而不是布尔变量的语句,那么今天的问题本可以避免。