SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    May 2009
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    ? How do I check db if user exists update else insert?

    I'm trying to setup a twitter script that will update a users description, url, and photo.

    I've got everything working where it will insert, I was trying to use the replace function but not having any luck, here's what I have so far:

    Code:
    $xml = new SimpleXMLElement($xmlstring);
    $pimage = $xml->profile_image_url[0];
    $pdesc = $xml->description[0];
    $purl = $xml->url[0];
    
    $process_profile = "REPLACE INTO twitter_cache SET twitter_user ='$twid', pimage = '$pimage', pdesc = '$pdesc', purl = '$purl'";
    mysql_query($process_profile)
    or die(mysql_error());

  2. #2
    SitePoint Addict X-Cart's Avatar
    Join Date
    May 2009
    Posts
    256
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    First of all, you need to make sure there's a column which contains an unique value, otherwise a new record will be inserted. Could you provide a table structure for twitter_cache?
    X-Cart - tens thousands live online shops worldwide
    Follow us on Twitter

  3. #3
    Web Professional
    Join Date
    Oct 2008
    Location
    London
    Posts
    862
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

  4. #4
    SitePoint Member
    Join Date
    May 2009
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    table structure is:

    twitter_user varchar(255) utf8_unicode_ci
    pimage varchar(255) utf8_unicode_ci
    pdesc varchar(255) utf8_unicode_ci
    purl varchar(255) utf8_unicode_ci

  5. #5
    SitePoint Member
    Join Date
    May 2009
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So would this work?
    INSERT INTO table (twitter_user,pimage,pdesc,purl) VALUES ($twitter_user,$pimage,$pdesc,$purl)
    ON DUPLICATE KEY UPDATE pimage=$pimage, pdesc=$pdesc, purl=$purl;

    -is the syntax right as well?

    btw - I've made twitter_user 'unique'
    Last edited by twtFollow; May 19, 2009 at 00:29. Reason: added info

  6. #6
    SitePoint Addict X-Cart's Avatar
    Join Date
    May 2009
    Posts
    256
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In case twitter_user is unique, both versions should work.
    X-Cart - tens thousands live online shops worldwide
    Follow us on Twitter


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
  •