Php Can';t更新数据库中的某些字段
我正在创建一个在线游戏,在更新数据库中的一些字段时遇到问题。 下面是表单和php代码Php Can';t更新数据库中的某些字段,php,mysql,Php,Mysql,我正在创建一个在线游戏,在更新数据库中的一些字段时遇到问题。 下面是表单和php代码 <form method='post'> <div style="float: left; width: 630px; color: white;"> <div style="float: left; width: 400px; background: #555; height: 20px;">
<form method='post'>
<div style="float: left; width: 630px; color: white;">
<div style="float: left; width: 400px; background: #555; height: 20px;">
<?php
if ($row['Protected'] == 1)
{
?>
<img src="images/pass.png"></img>
<input type="password" name="pass" placeholder="Password" style="height: 18px;" />
<?php
}
echo("<input type='submit' name='enter' style='background: #555; text-decoration: underline;' value='$row[Name]' />");
?>
</div>
<div style="float: right; width: 229px; background: #555; margin-left: 1px; height: 20px;">
<?php
echo($row['NrPlayers']);
echo("/");
echo($row['MaxPlayers']);
echo(" Players");
?>
</div>
<div style="clear: both;">
<?php
echo($row['Descr']);
?>
</div>
<div style="background: #ccc; font-size: 13px; margin-bottom: 2px; color: black;">
<?php
echo($row['FName']);
echo(" ");
echo($row['LName']);
?>
</div>
<input type="hidden" name="id" value="<?php$row['Id_Room']?>" />
<input type="hidden" name="protect" value="<?php$row['Protected']?>" />
<input type="hidden" name="password" value="<?php$row['Pass']?>" />
<input type="hidden" name="nr" value="<?php$row['NrPlayers']?>" />
</div>
</form>
<?php
if (isset($_POST['enter']))
{
if ($_POST['protect'] == 1)
{
if ($_POST['pass'] == $_POST['password'])
{
$nr = $_POST['nr'] + 1;
mysql_query("upadte users set Id_Room = '$_POST[id]' where Id_User = '$_SESSION[id]'");
mysql_query("update rooms set NrPlayers = '$nr' where Id_Room = '$_POST[id]'");
header("Location: game.php");
}
else
{
?>
<span style="color: red; text-align: center;">The password you entered is incorrect.</span>
<?php
}
}
else
if (($_POST['protect'] == 0))
{*/
$nr = $_POST['nr'] + 1;
mysql_query("upadte users set Id_Room = '$_POST[id]' where Id_User = '$_SESSION[id]'");
mysql_query("update rooms set NrPlayers = '$nr' where Id_Room = '$_POST[id]'");
header("Location: game.php");
}
}
?>
步骤1:
将表单提交到模板,然后从表单范围中转储所有值,以确保从表单中获得所需的内容
步骤2:
使用一些测试值在查询编辑器(如mysql work bench或php myAdmin)中编写查询,这样您就有了工作查询
步骤3:
用php编写生成查询的代码,但不要执行它,而是将其转储到屏幕上。这样做,直到输出单元看起来像您在步骤2中设计的查询
步骤4:
完成页面并执行查询,注意输入错误、语法错误和安全漏洞。然后从查询中转储结果
您需要在当前代码中签入的内容:
mysql_query("upadte users set Id_Room = '$_POST[id]' where Id_User = '$_SESSION[id]'");
打字错误-'upadte',内联替换-Id_Room='{$\u POST['Id']}'
在发布大量代码并寻求帮助之前,请务必完成彻底的调试步骤。“更新不起作用”此外,您还有一个输入错误:)mysql\u查询(“upadte->mysql\u查询(”updatethere是另一个输入错误:如果启用了短标记,则应该读取。请永远不要在sql查询中直接使用来自$\u GET、$\u POST或$\u COOKIE的任何值。每个人都可以轻松删除完整的数据库。始终对其进行筛选,确保这些值有效或只是映射它们。欢迎使用堆栈溢出!请不要使用mysql.*
新代码的函数。它们不再维护,社区已开始。请参阅?相反,您应该了解并使用或。如果您不能决定,将帮助您选择。如果您愿意学习。