i have a table called members which stores all the users information. what i am trying to do is allow the user to only update two fields in the database which are team number and pay scale for each user.
at the moment i can display all the user detials, i did this using a loop. what i want to be able to do is update certain fields for certain user, and it would be good if it could all be done in the same page.
any help would be good
here is the code i have so far
PHP Code:
$link = mysql_connect('localhost','username','password'); if(!$link){ die('Notconnected:'.mysql_error()); } //make foo the current db $db_selected= mysql_select_db('DB',$link); if(!$db_selected){ die('Can\'t use addressbook :'.mysql_error());};
You'll have to add input fields to the form for the data you want the user to change, and a hidden field for the table key value (HR number?).
Then, the script that receives the form values when the user submits the form, should check the validity of the values the user inserted, and if all is ok update the values in the database for that user.
I'd say condense your two for loops into one, I could be wrong but i've never been able to define variables with a for loop only update variables outside of the loop.
so basically your second loop is trying to run through variables that aren't there anymore, so it fails your if test and does nothing.
mysql_query("UPDATE members SET teamnumber='" . $row['teamnumber'] . "', payscale='" . $row['payscale'] . "' WHERE hrnumber='".$records[$i]["hrnumber"]."'");
in
PHP Code:
$query = "
UPDATE members
SET
teamnumber='" . $row['teamnumber'] . "'
, payscale='" . $row['payscale'] . "'
WHERE hrnumber='".$records[$i]['hrnumber']."'
";
echo "query: $query<br />";
mysql_query($query) or die("MySQL error - ".mysql_errno()." - ".mysql_error());
The echo will show you the query that is executed, so you can check if it's correct. Delete the echo once everything is working fine.
The 'or die' will show you the error MySQL throws if there is a problem with the query.