simple question that I cant solved

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kingdm
Forum Commoner
Posts: 27
Joined: Thu Dec 03, 2009 9:32 am

simple question that I cant solved

Post by kingdm »

Hello.

I've got this question. I have a database with one table 'users'. Under these is a lot of columns, with almost 200+ records(rows).

Now my problem is I added a new field 'elem_course' and 'high_course'. Of course it's empty. Now I want all my records to have the value of "Primary" for the 'elem_course' and "Secondary" for the 'high_course'?

How can I do this in SQL or PHP? I'm asking because I'm tired of editing all of them one by one in phpmyadmin of XAMPP

Thanks. :banghead:
User avatar
JNettles
Forum Contributor
Posts: 228
Joined: Mon Oct 05, 2009 4:09 pm

Re: simple question that I cant solved

Post by JNettles »

When you create a new column you can specify a default value that updates all existing rows. Look for the option in PHPmyadmin or you can use sql that looks something like....

UPDATE users SET elem_course = 'Primary';

Double check that - should work but my mind is thinking in DB2 right now.....
kingdm
Forum Commoner
Posts: 27
Joined: Thu Dec 03, 2009 9:32 am

Re: simple question that I cant solved

Post by kingdm »

JNettles wrote:When you create a new column you can specify a default value that updates all existing rows. Look for the option in PHPmyadmin or you can use sql that looks something like....

UPDATE users SET elem_course = 'Primary';

Double check that - should work but my mind is thinking in DB2 right now.....
Thanks for your input mate. It did makes sense in me while trying, problem was solved. Credits also to skywalker from webdeveloper. Here's the query in case someone might have the similar problem.

Code: Select all

UPDATE users
SET 
elem_course = 'Primary',
high_course = 'Secondary'
User avatar
manohoo
Forum Contributor
Posts: 201
Joined: Wed Dec 23, 2009 12:28 pm

Re: simple question that I cant solved

Post by manohoo »

One thing to add to JNettles post, be careful with your SQL update, if you don't have a WHERE clause you can overwrite the entire column, that could be a real headache!
User avatar
JNettles
Forum Contributor
Posts: 228
Joined: Mon Oct 05, 2009 4:09 pm

Re: simple question that I cant solved

Post by JNettles »

Good point but if his entire column is empty then it won't make any difference. But, as you said, its a good idea to do WHERE blah = '' or something like that.
Post Reply