Populating a new field in an established database

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
SawkenDotCom
Forum Newbie
Posts: 5
Joined: Tue Feb 22, 2005 1:22 pm

Populating a new field in an established database

Post by SawkenDotCom »

I have a database with about 1,000 names in it, I want to track if they were sent a welcome email so I have set up a new column that will be just "y" for yes and "n" for no.

How would I go about inserting the "y" into the new database column for those 1,000 accounts?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Not sure if you can do this but

"INSERT INTO `yourtablename` (mailrecvd) VALUES ('y')" :?

Otherwise... if you have a primary key with some sort of auto-increment just loop over each row and insert it like this

Code: Select all

$query = "SELECT * FROM `tablename`";
$result = mysql_query($query);
$numrows = mysql_num_rows($result);
for ($i=0; $i<$numrows; $i++) &#123;
    $id = mysql_result($result, $i, 'id');
    $insert_query = "INSERT INTO `tablename` (`mailrecvd`) VALUES ('y') WHERE id=`$id`";
    $insert_result = mysql_query($insert_query);
&#125;
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

why not just include another field on the table that has the user information and then update that row when the mail has been sent?
SawkenDotCom
Forum Newbie
Posts: 5
Joined: Tue Feb 22, 2005 1:22 pm

Post by SawkenDotCom »

Burrito wrote:why not just include another field on the table that has the user information and then update that row when the mail has been sent?
That's the point, it's just that I already have over a thousand accounts in the table and for the life of me couldn't figure out the best way to make it so that the accounts that already recieved the welcome email were checked by the cron job.
SawkenDotCom
Forum Newbie
Posts: 5
Joined: Tue Feb 22, 2005 1:22 pm

Post by SawkenDotCom »

d11wtq wrote:Not sure if you can do this but

"INSERT INTO `yourtablename` (mailrecvd) VALUES ('y')" :?

Otherwise... if you have a primary key with some sort of auto-increment just loop over each row and insert it like this

Code: Select all

$query = "SELECT * FROM `tablename`";
$result = mysql_query($query);
$numrows = mysql_num_rows($result);
for ($i=0; $i<$numrows; $i++) &#123;
    $id = mysql_result($result, $i, 'id');
    $insert_query = "INSERT INTO `tablename` (`mailrecvd`) VALUES ('y') WHERE id=`$id`";
    $insert_result = mysql_query($insert_query);
&#125;
Thanks! I will try it later tonight.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

sounds like you need an update query...

http://dev.mysql.com/doc/mysql/en/update.html
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

d11wtq wrote:Not sure if you can do this but

"INSERT INTO `yourtablename` (mailrecvd) VALUES ('y')" :?

Otherwise... if you have a primary key with some sort of auto-increment just loop over each row and insert it like this

Code: Select all

$query = "SELECT * FROM `tablename`";
$result = mysql_query($query);
$numrows = mysql_num_rows($result);
for ($i=0; $i<$numrows; $i++) &#123;
    $id = mysql_result($result, $i, 'id');
    $insert_query = "INSERT INTO `tablename` (`mailrecvd`) VALUES ('y') WHERE id=`$id`";
    $insert_result = mysql_query($insert_query);
&#125;
Grrr :oops: I can't believe I wrote this... I must be ready for bed. INSERT??? Grrr some more. *Banging head on wall now*

*cough* UPDATE is what you need yes.

*Banging head on wall again*

Nite then guys :?

zzzzzzzzzzzzzzzzzzzzzz.........zzz...........

LOL
Post Reply