Jump to content

Updating Tables


-Karl-

Recommended Posts

Okay so normally I'd do

$query = "UPDATE users SET `username` = '$_POST[username]',`password` = '$_POST[password]',`email` = '$_POST' WHERE `id` = '$id'";

 

But what if I only want it to update the tables depending on what a user has changed in a form.

 

For example, the form outputs data from the database

<fieldset>
<input type="hidden" name="id" id="id">
<label for="username">Username:</label><input value="{$arr['username']}" type="text" name="username" id="username" onkeyup="checkUsernameForLength(this);" />
</fieldset>
<fieldset>
<label for="password">Password:</label><input name="password" id="password" type="password" onkeyup="checkPassword(this);" />
</fieldset>

 

What would be the correct way about doing the query if let's say the user only changes their password, not the username, therefore the username will not return blank and it will remain what it was.

 

Link to comment
https://forums.phpfreaks.com/topic/198863-updating-tables/
Share on other sites

create separate queries.  ultimately, to me, the concept of how people are updating their information is inefficient.

 

for starters, why let your users change their username?  typically not a common practice.

 

back to the point at hand.  just make sure the form is pre-populated with their existing credentials, which it appears you've already done that?, and that way, regardless of what field is being updated, the rest of the information is just being over-written with the same data.  redundant, yes.

 

or you can write several conditions to fill, or even create, a query dependent on what the user is trying to change.  but since PHP doesn't read minds, separating queries or just over-writing existing data in the db will exact same data from form fields would suffice for you.

 

and please tell me you are sanitizing your incoming form values and are not planning on dropping them right into the query.  look into mysql_real_escape_string() to avoid SQL Injection.

Link to comment
https://forums.phpfreaks.com/topic/198863-updating-tables/#findComment-1043832
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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