PHP站点工作不正常
我正试图为我和我的朋友们制作一个在线“银行”,用于像Minecraft这样的游戏。大部分事情我都做得很好,但是贸易方面的事情。设置是,我使用一个文本文件作为我们拥有的钱的存储,然后从我的PHP代码中调用这些文本文件,但数学计算不正确。我将放置一个链接到整个网站的压缩版本。我确实开始了一个会议,所有这些事情 压缩文件:PHP站点工作不正常,php,web,Php,Web,我正试图为我和我的朋友们制作一个在线“银行”,用于像Minecraft这样的游戏。大部分事情我都做得很好,但是贸易方面的事情。设置是,我使用一个文本文件作为我们拥有的钱的存储,然后从我的PHP代码中调用这些文本文件,但数学计算不正确。我将放置一个链接到整个网站的压缩版本。我确实开始了一个会议,所有这些事情 压缩文件: 交易完成。在3秒内重定向到主页 这里的问题是您引用的是文件包装器,而不是文件本身的内容 例如: $txt=$ethan-$amm 如果通过,比如说$10,这行代码实际上正在执行
交易完成。在3秒内重定向到主页
这里的问题是您引用的是文件包装器,而不是文件本身的内容
例如:
$txt=$ethan-$amm代码>
如果通过,比如说$10,这行代码实际上正在执行
$txt=[文件包装器]-10代码>
因为文件包装器不是一个值,所以您的计算是错误的。您需要使用以下命令读取包装器:
或
只需使用完全不同的函数。这就是我要做的。我将传递这些变量:amount
,user
。我会将user
设置到受影响用户的银行帐户(当然,除了我自己的帐户之外,从会话中)
你开始上课了吗?有错误吗?检查他们?好吗?投入太多?在打开PHP标记后立即将错误报告添加到文件顶部,例如RTFM:fopen()
返回文件句柄,而不是文件中的数据。你把php内部分配的数字加在一起,与文件中的数据完全没有关系,除了php数字告诉php哪个句柄是哪个句柄。当我把你需要使用fread
或fgets
读取文件Ethan时,它没有报告任何错误。如果只有一行,请使用fgets($filehandle)
。为什么不使用mysql Ethan这样的数据库?虽然我觉得只有四个条目太过分了,但如果您需要添加更多的用户,这会更容易。如果您打算使用db直接查找PDO,而不是mysql扩展。我永远无法理解mysql,我将再次查看它,但上次太让人困惑了。还有,Ethan,关于您的错误报告。检查您的php.ini
文件,确保display\u errors
处于启用状态,以及display\u startup\u errors
并将error\u reporting
设置为E\u ALL
。。这是假设您处于开发环境中。生产环境应该关闭/关闭所有错误,并且只将它们记录到文件中。我为每个用户使用了一个单独的文件。请再说一遍?我刚刚给了你一些惊人的代码,你就是这么说的?对不起,我很忙,不得不离开去赴约。对不起,我无意冒犯你。
<!DOCTYPE html>
<html>
<body>
<?php
$users = array('riley', 'ethan');
$banks = array();
foreach($users as $account_holder) {
// Set the path of your file holding this persons bank
$file_path = $account_holder . ".txt";
// Lets set the $values[name] variable to the contents of the file
// we also cast this as an integer, so that if empty, it will still be zero
// I also prepended the function with @, to suppress any errors (if this bank doesnt exist yet)
$banks[ $account_holder ] = (int) @file_get_contents($file_path);
}
function saveBanks() {
// Access from within the function scope
global $banks;
// Loop through the banks
foreach($banks as $account_holder => $balance) {
// Set the path of your file holding this persons bank
$file_path = $account_holder . ".txt";
// Open the file, create if necessary, empty either way
$fp = fopen($file_path, 'w');
// Write balance to file
fwrite($fp, $balance);
// Close file wrapper
fclose($fp);
}
}
// Let's grab our active user, and transaction amount
// If youre not familiar with this 'if' style, it's: condition ? value if true : value if false;
// http://davidwalsh.name/php-ternary-examples
$active_user = isset($_SESSION["user"]) ? $_SESSION["user"] : die("There is no active user.");
$txn_amount = isset($_POST["amount"]) ? $_POST["amount"] : die("There was no transaction amount received.");
$txn_user = isset($_POST["user"]) ? $_POST["user"] : die("There's no receiving bank account.");
// Process the transaction, check for validity
if(!isset($banks[ $txn_user ])) die("That receiving bank account doesn't exist.");
// Adjust balances
$banks[ $active_user ] -= $txn_amount;
$banks[ $txn_user ] += $txn_amount;
// Write to files
saveBanks();
?>
<p> Transaction made. Redirecting to home in 3 seconds. </p>
<p> New Balances: <?php foreach($banks as $user=>$bal): ?> <li><b><?php echo $user; ?></b>: <?php echo $bal; ?></li><?php endforeach; ?></p>
<meta http-equiv="refresh" content="3;url=bank.php" />
</body>
</html>