Updating Database where spaces exist between columns

I’m trying to update certain columns in MYSQL database with PHP, but they are spaced apart, with other columns of information in between.

full database $query -
(join_date, last_updated, username, pass_word, first_name, last_name, email, gender, dob, city, state, country, bit_about_me, profile_pic)

those I want to update in the $query -
(last_updated, first_name, last_name, email, gender, dob, city, state, country, bit_about_me, profile_pic)

Please notice the removal of ‘username, pass_word’ in between ‘last_updated’ and ‘first_name’.

The question I have is:

how do I write the code in PHP to update certain columns in my database when they are spaced apart?

Do I:

A. write out in the $query the entire outline of the database, but only update certain parts through accessing certain variables in the form?
or
B. write out ONLY the columns I wish to update in the $query, separating them through commas, AND, individual SET etc?

Immediate follow-up question:

IF I have to put the whole database outline in the $query, what do I put in the columns where I don’t want new information to be inputted?

Note: I have tried using UPDATE with SET having all columns = $variables with a comma separating each column; [php]$query = “UPDATE Users SET last_updated = NOW(), first_name = ‘$first_name’, last_name = ‘$last_name’, email = ‘$email’, gender = ‘$gender’, dob = ‘$dob’, city = ‘$city’, state = ‘$state’, country = ‘$country’, bit_about_me = ‘$bit_about_me’, profile_pic = ‘$profile_pic’ WHERE gamertag =” . $_SESSION[‘gamertag’];[/php]

I have also used AND after removing the command in between each column;[php]$query = “UPDATE Users SET last_updated = NOW() AND first_name = ‘$first_name’ AND last_name = ‘$last_name’ AND email = ‘$email’ AND gender = ‘$gender’ AND dob = ‘$dob’ AND city = ‘$city’ AND state = ‘$state’ AND country = ‘$country’ AND bit_about_me = ‘$bit_about_me’ AND profile_pic = ‘$profile_pic’ WHERE gamertag =” . $_SESSION[‘gamertag’];[/php]

I understand I may still be able to use either or, but with a different $query outline;

Any help, tips, advice, or information on this would be greatly appreciated.

-ECP03

When doing a SQL update you can set the value of as many (or as few) columns you want. In other words, just use the columns you want to update in your update statement.

related:
You need to look into parameterized queries, this is the “new” standard (was introduced 10 years ago) where you send the query and the parameters separate to the database server. That way you stay safe from sql injection.

Using MySQL or better MySQLi queries, if you use the SET option, you are telling the database system to
just update those fields only. Which is exactly what you want to do. So, the first line you showed is the
correct one. Just do not put in any of the fields you want to leave alone.

Here is a tutorial on a site that is one of the best for beginners. If you do not understand updates, then,
I assume you will like this site. It has a ton of tutorials on how to set up just about anything. They are
simple to understand and show a vast amount of info for beginners and experienced programmers too.

http://www.w3schools.com/php/php_mysql_update.asp
Hope this helps…

Sponsor our Newsletter | Privacy Policy | Terms of Service