Kira - The God Kira - The God - 4 months ago 8
PHP Question

Can't update some fields in database

I'm creating an online game and I have a problem with updating some fields in database.
Here's the form and the php code.

<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");
}
}
?>


There are two updates and one redirect. So, updates don't work but redirect does.

Answer

Step 1:

Submit the form to a template, then dump out all your values from the form scope to ensure your getting what you need through from the form.

Step 2:

Using some test values write your queries in a query editor like mysql work bench or php myAdmin so that you have working queries.

Step 3:

Write the code that generates your queries in php, but instead of executing it dump it out to screen. Do this until the outpu looks like the query you designed in the step 2.

Step 4:

Finish the page and execute the query, look out for typos, syntax errors and security holes. then dump out results from the query.

Things you need to check in your current code:

mysql_query("upadte users set Id_Room = '$_POST[id]' where Id_User = '$_SESSION[id]'");

Typos - 'upadte' , inline substition - Id_Room = '{$_POST['id']}'.

Always walk through thorough debugging steps before posting large amounts of code and asking for help.

Comments