Log in

View Full Version : Resolved show me how to update db please



Dirt_Diver
01-21-2009, 09:46 PM
I was wondering if someone could help me build an admin update page.

I don't know how to even begin with this.

I don't have many fields that I need to update but I would like to have the ability to update some information on my database.

I have the form but I don't even know how to code this.

All I know is I need to use the update function but I don't even know if my form is right.

I guess what I want or how I think it should be set up is a field for me to enter the current team name and then a field for me to enter the updated name.

anyway here is my form, have I even started doing this right?


<form name="form2" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<p align="left">Old DPC Username:
<input name="OldUser" type="text" id="OldUser">
<br>
New DPC Username:
<input name="NewUser" type="text" id="NewUser">
<br>
Change username:
<input name="UpdateUser" type="text" id="UpdateUser">
<br>
To Team:
<select name="team_request" id="team_request">
<option value="NULL" selected>Please choose a team. </option>
<option value="Australia">Australia</option>
<option value="Canada_I">Canada I</option>
<option value="Canada_II">Canada II</option>
<option value="Canada_III">Canada III</option>
<option value="Germany">Germany</option>
<option value="Iceland">Iceland</option>
<option value="Islanders">Islanders</option>
<option value="Mystery">Mystery</option>
<option value="Poland">Poland</option>
<option value="UK">UK</option>
<option value="USA_Equestrian">USA Equestrian</option>
<option value="USA_Midwest">USA Midwest</option>
<option value="USA_Northeast">USA NorthEast</option>
<option value="USA_Northwest">USA NorthWest</option>
<option value="USA_Southeast">USA SouthEast</option>
<option value="USA_Southwest">USA SouthWest</option>
</select>
</p>
<p>
<input type="submit" name="submit" value="Update">
</p>
</form>

Nile
01-21-2009, 10:59 PM
Well... I would have to know your structure, but try this:


<?php
if(isset($_POST['submit'])){ //if the 'Update' button is pressed
mysql_query("UPDATE `usernames` SET `team_request ` = '{$_POST['team_request']}' , `username` = '{$_POST['updateUser']}' WHERE `username` = '{$_POST['hidden_username']}");
}
?>

There should be another field called hidden_username that holds the username that you are editting... Change the fields and such.

Dirt_Diver
01-22-2009, 12:18 AM
Actually Nile you do know my structure, I sent you a picture of it earlier this week for something else.

And just so I can understand what is going on here what exactly is the sole purpose of the hidden username field, don't I want to put in the username so it knows who to change?

Nile
01-22-2009, 12:22 AM
Yes. The hidden input will hold the username's name so that it knows which user to change. And I don't have the picture anymore, and I don't remember little things like a variable name or a table structure. =/

Dirt_Diver
01-22-2009, 12:34 AM
I understand I don't remember much either...

As for the hidden field if it's hidden how does it know which user to move? I can't tell it because it's hidden or am I missing something?

Field | Type | Null | Default | Comments
id | int(5) | No |
entry_username |varchar(32) | No
TeamCap | char(1) | No | N
team_request | varchar(32) | No
SeasonCount | int(1) | No | 0
terms |char(1) | No |0
timestamp | timestamp | No | CURRENT_TIMESTAMP

Dirt_Diver
01-22-2009, 12:36 AM
Also I adjusted the php can you tell me if it's right?


<?php
if(isset($_POST['submit'])){ //if the 'Update' button is pressed
mysql_query("UPDATE `signups` SET `team_request` = '{$_POST['team_request']}' , `entry_username` = '{$_POST['moveuser']}' WHERE `entry_username` = '{$_POST['newuser']}");
}
?>

I also adjusted my form.

<form name="form2" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<p align="left">Replace this user:
<input name="OldUser" type="text" id="OldUser">
<br>
With this user:
<input name="newuser" type="text" id="newuser">
</p>
<p align="left"><br>
Move this user:
<input name="moveuser" type="text" id="moveuser">
<br>
To this team:
<select name="team_request" id="team_request">
<option value="NULL" selected>Please choose a team. </option>
<option value="Australia">Australia</option>
<option value="Canada_I">Canada I</option>
<option value="Canada_II">Canada II</option>
<option value="Canada_III">Canada III</option>
<option value="Germany">Germany</option>
<option value="Iceland">Iceland</option>
<option value="Islanders">Islanders</option>
<option value="Mystery">Mystery</option>
<option value="Poland">Poland</option>
<option value="UK">UK</option>
<option value="USA_Equestrian">USA Equestrian</option>
<option value="USA_Midwest">USA Midwest</option>
<option value="USA_Northeast">USA NorthEast</option>
<option value="USA_Northwest">USA NorthWest</option>
<option value="USA_Southeast">USA SouthEast</option>
<option value="USA_Southwest">USA SouthWest</option>
</select>
</p>
<p>
<input type="submit" name="submit" value="Update">
</p>
</form>

Nile
01-22-2009, 12:40 AM
No, you can't change a name where the name doesn't exist, here:


<?php
if(isset($_POST['submit'])){ //if the 'Update' button is pressed
mysql_query("UPDATE `signups` SET `team_request` = '{$_POST['team_request']}' , `entry_username` = '{$_POST['moveuser']}' WHERE `entry_username` = '{$_POST['OldUser']}'");
}
?>
I didn't provide validation, so you may wanna do that to. :P

Nile
01-22-2009, 01:37 AM
It seems your topic is solved... Please set the status to resolved.. To do this:
Go to your first post ->
Edit your first post ->
Click "Go Advanced" ->
Then in the drop down next to the title, select "RESOLVED"