Jump to content

Update one record depending on who's logged in?


doomdude

Recommended Posts

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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..

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.