SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Thread: if statement

  1. #1
    SitePoint Zealot
    Join Date
    Aug 2005
    Posts
    166
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    if statement

    I'm using this to insert songs that are submitted to my site:
    PHP Code:
        $insert_song = @mysql_query("SELECT artist_id, artist_name FROM artist ORDER BY artist_name ASC");
        if(!@
    mysql_num_rows($insert_song))
        {
            die(
    'You have to add at least one artist to the database first');
        }
        else
        {
            if(
    $_POST['submit'] && strlen(trim($_POST['song_name'])))
            {
                
    $insert mysql_query("INSERT INTO song SET
                                            artist_id         = '" 
    $artist_id "',
                                            song_name         = '" 
    addSlashes($_POST['song_name']) . "',
                                            song_features    = '" 
    addSlashes($_POST['features']) . "',
                                            song_lyrics         = '" 
    addSlashes($_POST['song_lyrics']) . "',
                                            song_contributer = '" 
    addSlashes($_POST['contributer']) . "',
                                            song_status = 'inactive',
                                            song_modified = NOW(),
                                            song_dateadded = NOW(),
                                            user_id = " 
    $_SESSION['user_id'] . "
                                    "
    );
                 
    $sql = ("INSERT INTO hits SET
                song_hits=0, artist_id='" 
    $_POST['artist'] . "', song_id=" mysql_insert_id() . ", song_hits_dateadded=now()");
                if (@
    mysql_query($sql)) {
                echo 
    '';
                } else {
                echo 
    '<p>Error submitting default hit value: ' .
                
    mysql_error() . '</p>';
       }
                if(
    $insert)
                {
                    
    $submit true;
                }
                else
                {
                    die(
    mysql_error());
                }
            }
        } 
    The code works fine for users that have accounts but I get a white screen when guests attempt to submit songs.

    This is obviously happening because of this line:
    PHP Code:
    user_id " . $_SESSION['user_id'] . " 
    Is there a way to write an if statement so that if a guest submits a song, the user_id will be blank?

    Thank you.

  2. #2
    SitePoint Wizard silver trophy Jelena's Avatar
    Join Date
    Feb 2005
    Location
    Universum, 3rd Corner
    Posts
    3,000
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Build your query this way..
    PHP Code:
    $sql "INSERT INTO song SET ";
    $sql .= " artist_id = '".$artist_id."'";
    $sql .= ", song_name = '".addSlashes($_POST['song_name'])."'";
    $sql .= ",  song_features = '".addSlashes($_POST['features'])."'";
    //etc...
    if(isset($_SESSION["user_id"]))
        
    $sql .= ", user_id = '".$_SESSION["user_id"]."'";
    else
        
    $sql .= ", user_id = ''";
        
    $insert mysql_query($sql); 
    PS. It is highly recommended that you check if query failed..
    PHP Code:
    if (!$insert) {
       die(
    'Invalid query: ' mysql_error());

    -- Jelena --

  3. #3
    SitePoint Addict betrollwin.com's Avatar
    Join Date
    Nov 2005
    Posts
    213
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    use the isset function

    darn beat me to it.


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
  •