doomdude Posted September 27, 2011 Share Posted September 27, 2011 Hey guys, I'm just trying to add a form button to my site that updates a user's data in the mysql database. However I'm struggling trying to find a way to define which field to edit, can someone tell me if I'm along the correct lines: Page with submit button: <?php $result = mysql_query("SELECT * FROM members WHERE Username='$_SESSION[username]'") or die(mysql_error()); while($row = mysql_fetch_array( $result )) { echo '<b>Resources</b><br />'; echo 'Wood: ' . $row['res1'] . ' <br />'; echo 'Iron: ' . $row['res2'] . ' <br />'; echo 'Credits: ' . $row['credits'] . ' <br />'; } echo 'Offence Power: '; echo $offencepower; echo '<br />'; echo 'Defence Power: '; echo $defencepower; echo '<br />'; echo 'Total Power: '; echo $totalpower; ?> <br /><br /> <form action="update.php" method="post"> <input type="hidden" name="Memberid" value="' . $row['Memberid'] . '"> <input type="submit" name="submit" value="submit"> </form> Update.php: <?php include ('config.php'); ?> <?php $Memberid = mysql_real_escape_string($_POST['Memberid']); if (isset($_POST['Memberid'])) { $query=("UPDATE members SET totalpower = '10' WHERE Memberid='$Memberid'"); mysql_query($query); echo $query; /* header('Location: ' . $_SERVER['HTTP_REFERER']);*/ } ?> When echoing the query I'm getting: UPDATE members SET totalpower = '10' WHERE Memberid='\\\' . $row[\\\'Memberid\\\'] . \\\'' Thanks Quote Link to comment https://forums.phpfreaks.com/topic/247969-update-one-record-depending-on-whos-logged-in/ Share on other sites More sharing options...
AyKay47 Posted September 27, 2011 Share Posted September 27, 2011 in your form you are not including the php into the html markup the correct way...you will want this instead.. <form action="update.php" method="post"> <input type="hidden" name="Memberid" value="<?php echo $row['Memberid']; ?>"> <input type="submit" name="submit" value="submit"> </form> if you are expecting one row to be grabbed from the db using your query, which in this case it should be.. you will want to use an if statement here instead of a while loop.. include the form in your if statement so the $row['Memberid'] variable is valid Quote Link to comment https://forums.phpfreaks.com/topic/247969-update-one-record-depending-on-whos-logged-in/#findComment-1273305 Share on other sites More sharing options...
doomdude Posted September 27, 2011 Author Share Posted September 27, 2011 in your form you are not including the php into the html markup the correct way...you will want this instead.. <form action="update.php" method="post"> <input type="hidden" name="Memberid" value="<?php echo $row['Memberid']; ?>"> <input type="submit" name="submit" value="submit"> </form> if you are expecting one row to be grabbed from the db using your query, which in this case it should be.. you will want to use an if statement here instead of a while loop.. include the form in your if statement so the $row['Memberid'] variable is valid Hey thanks for the reply. I've changed the form now. Where you say my while should be a if, I'm not sure I understand why, I originally created the while to be able to pull the data for these: echo '<b>Resources</b><br />'; echo 'Wood: ' . $row['res1'] . ' <br />'; echo 'Iron: ' . $row['res2'] . ' <br />'; echo 'Credits: ' . $row['credits'] . ' <br />'; However, I've change the while to an if and it has indeed worked. Any chance you could explain why the while didn't work in the context but the if does? Thanks for your help! Quote Link to comment https://forums.phpfreaks.com/topic/247969-update-one-record-depending-on-whos-logged-in/#findComment-1273307 Share on other sites More sharing options...
AyKay47 Posted September 28, 2011 Share Posted September 28, 2011 well the while loop technically should work also, your main issue was your form, and not including the PHP correctly.. a while loop is a loop, thus it is designed to loop through more then one instance of data.. your query is meant to only grab one row, not multiple rows, so there is no need to loop through one row.. which is why a simple if statement will suffice here.. Quote Link to comment https://forums.phpfreaks.com/topic/247969-update-one-record-depending-on-whos-logged-in/#findComment-1273480 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.