SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    Non-Member MrLister's Avatar
    Join Date
    Feb 2001
    Posts
    108
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    if/elseif statement

    i have two tables. essays and essays_add. inside essays_add 'm trying to keep track of the users who posted their essay. inside essays_add i have add_name and add_total. Inside my add.php where they will submit it. it has username spot. what i'm trying to do is if that username does not exist in the add_name database it will make one. If it does exist it will add 1 more to add_total. how would i do this. i tried but failed miserable. please help

  2. #2
    SitePoint Zealot New Oddity's Avatar
    Join Date
    Jun 2001
    Location
    Georgia
    Posts
    102
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Run a select statement looking for entries with there user name... If it comesback null (no entries found) create a new entry for them, else add to there tottal...

    IE
    PHP Code:
    $sql "SELECT userName FROM table WHERE  userName = \"" $name "\"";
    $result mysql_query($sql);
    if (
    $result == null){
      
    //code to add entry
    }else[
      
    //code to add to user's tottal

    I hope this helps you.
    --Odd
    "We all live in a yellow subroutine."
    "Some call it insanity; I call it inspiration!"

  3. #3
    Non-Member MrLister's Avatar
    Join Date
    Feb 2001
    Posts
    108
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    oh ok, now i know how to do it. just one question is it neccessary to have \"" . $name . "\"";. would userName = $name" just work?

  4. #4
    SitePoint Zealot New Oddity's Avatar
    Join Date
    Jun 2001
    Location
    Georgia
    Posts
    102
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I believe that when putting a value into a sql statement it need quotes if the value is anything other than an int, otherwise it needs quotes. I'm not rock solid sure, but it's good to be safe...
    --Odd
    "We all live in a yellow subroutine."
    "Some call it insanity; I call it inspiration!"

  5. #5
    Non-Member MrLister's Avatar
    Join Date
    Feb 2001
    Posts
    108
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    oh ok. i just asked that because of the . $name . not sure what those . meants.

  6. #6
    SitePoint Zealot New Oddity's Avatar
    Join Date
    Jun 2001
    Location
    Georgia
    Posts
    102
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    . is like + in C and C++. It means concatination. Basicly it tells the run time compiler to take these two strings and but them together to make one string.
    --Odd
    "We all live in a yellow subroutine."
    "Some call it insanity; I call it inspiration!"

  7. #7
    What's HTML?
    Join Date
    May 2001
    Location
    San Diego, CA
    Posts
    1,701
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Or you could do:
    PHP Code:
    $sql "SELECT userName FROM table WHERE  userName ='$name'"
    It uses apostrophe's around the variable instead. I think I read that in PHP you can use apostrophe's inside a string that is created quotation marks. And you can use quotation marks inside a string that is created by apostrophe's.

    For example:
    PHP Code:
    $sql "SELECT userName FROM table WHERE  userName ='$name'"
    and
    PHP Code:
    $sql 'SELECT userName FROM table WHERE  userName ="$name"'
    should both work fine.

    Are there any disadvantages to doing this instead of using the concatenation method?
    Ryan Kuhle - A Proud Advisor - Got Questions? Just Ask!
    Get your website started for less than $20! Click Here

  8. #8
    SitePoint Zealot New Oddity's Avatar
    Join Date
    Jun 2001
    Location
    Georgia
    Posts
    102
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes... This should also work... There are a couple different ways to do it... It really is a difference in habit. I usualy find that mixing 's and "s can be confusing when cruising over code, so I take the lengthy way in order to be sure (to myself) that I got the syntax correct.
    --Odd
    "We all live in a yellow subroutine."
    "Some call it insanity; I call it inspiration!"

  9. #9
    What's HTML?
    Join Date
    May 2001
    Location
    San Diego, CA
    Posts
    1,701
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmm, never thought of it that way. Whenever I look at other code I usually see the concatenation method, but always wondered why they took the longer route. Now I know.
    Ryan Kuhle - A Proud Advisor - Got Questions? Just Ask!
    Get your website started for less than $20! Click Here

  10. #10
    SitePoint Zealot New Oddity's Avatar
    Join Date
    Jun 2001
    Location
    Georgia
    Posts
    102
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There is also the point of which method take less process time... Concatination wiht the . operator is a lower level command when changing from something that is in 's to "s takes longer for the comiler to translate it into machine language. It is also the issue of interperted languages versus compiled languages... It's all about what you started programming with, habits tend to stick across many languages... But that is a disscusion for another thread...
    --Odd
    "We all live in a yellow subroutine."
    "Some call it insanity; I call it inspiration!"

  11. #11
    SitePoint Member
    Join Date
    Jun 2001
    Location
    PA
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How about doing neither:

    $sql = "SELECT userName FROM table WHERE userName = \"$name\"";
    Adam W.
    Web Developer / Columnist
    http://www.uscho.com


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
  •