SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Wizard
    Join Date
    Nov 2003
    Location
    United Kingdom
    Posts
    2,120
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    I have a slight mysql problem using php. How do I do this sql insert query using php?

    Hi,

    I have 12 different email addresses being submitted by different people. Now I want to keep track of these email address so I know that people are not getting spammed.

    I have to following script:

    PHP Code:
    $fmail1 "name@domainonlineentertainmentwhateveryday.com";
    $fmail2 "chris@domainonlineentertainmentwhateverydays.com";

    for(
    $ix=0$ix 12$ix++)

        {
        
        
    $ixed $ix+1

    mysql_query
    ("insert into tellfriende (mailadd,hits) values ('${fmail.$ixed}','1')");

        } 
    Now I don't always have 12 email addresses. Some of the fields in the form is blank so I would not want these included. With the above query I was wondering if there is a way that I can insert an email address and if it is already in the database then it will just knock the hit counter up my 1 and will not add the email. I have already set up a unique index on the mailadded field.

    Is there a way of doing this without having to search the database to see if the email address is already in it. Could I just do the inserts and if it fails due to a duplicate email then it will add 1 to the hits field in the database. Can that be done.

    Also what is the best way of doing this php thing as with every email I am doing a new query. Can't I just do one query. I know it can be done, but I don't quite know how to.

    I hope you understand.

    Thanks.

  2. #2
    SitePoint Guru
    Join Date
    Jul 2005
    Location
    Orlando
    Posts
    634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    http://dev.mysql.com/doc/refman/5.0/...duplicate.html

    Look at this mySQL function.

    You could do the insert normally, and use the On Duplicate clause to increase the hit counter if that email address already exists.

  3. #3
    From downunder but sure 2 rise Hazardous's Avatar
    Join Date
    Aug 2003
    Location
    New Zealand
    Posts
    361
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    UFT....that's one cool function, i didn't know their was such a thing, i think i might use it to slim down some of my coding
    Hazardous
    My Blog

  4. #4
    SitePoint Wizard
    Join Date
    Nov 2003
    Location
    United Kingdom
    Posts
    2,120
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yep!

    That is something that I really hate about php and mysql. As I started learning it about 2 years ago and still am learning new things on a daily basis, I find that I am keep finding better ways of coding things and then have to redo major parts of my site time and time again.

    Quote Originally Posted by Hazardous
    UFT....that's one cool function, i didn't know their was such a thing, i think i might use it to slim down some of my coding


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •