Leaving field unchanged on UPDATE in php

I'm designing a user register system and am doing the admin update part for users at the moment.

However i have become stuck on something i thought would be simple.

When the admin updates a user they have a choice to change the password of that user. They can't see the password so obviously the box is left blank however i want it to be that if they do enter a new password then the password field updates. I have the code below, however what is happening at the moment is when the admin changes the password it all works fine, however if the admin leaves the password box empty, then the password is still changed to just a blank space.

I want it so that if the password box is left blank it doesn't change the current password?

It is a logic error. The code is making an md5 hash before testing for a blank field. You should test for the blank field before making the hash, or test for the blank field in the original input $_POST.

The first is how you setup your MySQL table. In some cases, depending on how you set it up, it will update the password field whenever that record is updated, which would explain this behavior or MySQL.

So, you can either figure that one out, or...

Grab the password for safe keeping before the update (see below).

I also re-write your queries. The if statement creates a query depeding on if you need to use the saved password or not rather than creating two separate queries.

I moved the mysql_query() function and the header() function below the if statement so that we only have to write it once.

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

Sorry for the late reply, got pulled into a meeting. Thanks for the replies, they had some good points in that i will be sure to look into (i do consider myself fairly novice still in this) so thats good.

Unfortunately i have tried both your example Ray and yours DrDamnit and neither have worked. I have a feeling that it may be to do with the database then like you said earlier. If so how can i go about making it work?

Regarding the or statement, my bad, should have known better just let it slip but have now changed to ||.

Featured Post

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Foreword (July, 2015)
Since I first wrote this article, years ago, a great many more people have begun using the internet. They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…

Things That Drive Us Nuts
Have you noticed the use of the reCaptcha feature at EE and other web sites? It wants you to read and retype something that looks like this.Insanity! It's not EE's fault - that's just the way reCaptcha works. But it is …

Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…