Page 1 of 1

Getting mysql field values as specific keys

Posted: Sat Dec 13, 2008 4:16 pm
by just_chris
Hello,

I'm creating a mailing list mech. where the administrator adds adds a bunch of emails that he wants to receive our publications. What im now trying to do is create the a link to remove an address he wants to and have that record deleted from the db. Each administrator has a unique id and they can add up to 25 emails right now.

The problem im having is basically I need to dynamically generate the removal link for the form and not have it remove all addresses related to that one id. Below you can see the id's are the same(this cannot change) but there can be different information for each. the link wold be something like a href=index.php?action=doRemove&email=$key[5]

-----------------------------------------------------------------------
id salutation fname lname position email type
------------------------------------------------------------------------
1377 Mr. fname lname pos example@example.com inp
1377 Mr. fname lname pos example1@example.com inp
1377 Mr. fname lname pos example2@example.com inp
1377 Mr. fname lname pos example3@example.com inp
1377 Mr. fname lname pos example4@example.com inp
1377 Mr. fname lname pos example5@example.com inp
__________________________________________________________________________________

Hopefully that is clear nad someone has some insight.

Thank you

Re: Getting mysql field values as specific keys

Posted: Sat Dec 13, 2008 4:36 pm
by Eran
What have you tried so far and what problems did you encounter?

Re: Getting mysql field values as specific keys

Posted: Sat Dec 13, 2008 4:50 pm
by just_chris
pytrin wrote:What have you tried so far and what problems did you encounter?
I havn't really tried anything yet as not even sure where to to start with this piece. Actually, I have tried to echo out the field names but that's not working either, probably the wrong code but... I should also mention this is alll being done inside mambo, but that shouldnt matter as i made new tables and all, thats actually where the id's are coming from as well.

My code so far is:

Code: Select all

function doRemove(){
    global $my, $database;
    $id= $my->id;
    $sql = "SELECT * FROM subscribe";
    $query= $database->setQuery($sql);
//$query = mysql_query($sql);
    $result= $database->query();
    $columns = mysql_num_fields($query);
    for($i = 0; $i < $columns; $i++) {
    echo mysql_field_name($columns,$result);
}
    //Need to match whats in the db to the email the user wants to remove by their user id.//
    //$query= "DELETE FROM subscribe WHERE email= example@example.com;  //example//
    //Email value should be $key[5];
    $database->setQuery($query);
    //$database->query() ? return true : mysql_error();
    $database->query() ? TRUE : mysql_error();
    //if (!$database->query()) die (mysql_error());
    echo $_POST['email']." is now unsubscribed";
    //doForm();
}

Re: Getting mysql field values as specific keys

Posted: Sat Dec 13, 2008 6:08 pm
by Eran
Why aren't you comparing against specific email addresses - meaning, why do you need to have them as keys in an imaginary array? it would be much easier if you associated the emails with a unique identifier (well, email addresses themselves are pretty unique).

Also, your script does some unnecessary things, like go over all the records in the table...

Re: Getting mysql field values as specific keys

Posted: Sun Dec 14, 2008 12:07 am
by cavemaneca
Try PHP 101 ( part 8 ): Databases and Other Animals - Part 2 and go to Wiping Out.
Except, use $row[5] instead of $row[0] of course.
I hope that leads you in the right direction.

Re: Getting mysql field values as specific keys

Posted: Sun Dec 14, 2008 8:52 am
by just_chris
pytrin wrote:Why aren't you comparing against specific email addresses - meaning, why do you need to have them as keys in an imaginary array? it would be much easier if you associated the emails with a unique identifier (well, email addresses themselves are pretty unique).

Also, your script does some unnecessary things, like go over all the records in the table...
Yes I am still trying to plan the code right now so i wanted to echo out everything to see what it did.

@cavemaneca, that looks very useful, could just be what i need.

Thank you both