SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Addict
    Join Date
    Sep 2006
    Posts
    238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    some help with notifications

    I have this code:

    PHP Code:
    if ($_GET['add']) {
        

        
    $submit "INSERT INTO dbFriends (my_id, friend_id, status) VALUES (" $_COOKIE["valid_id"] . "," $_GET['add'] . ", 0 )";
        }
        
    mysql_query($submit);
        
    }

    if(
    mysql_query($submit))
        {
        echo 
    'Success';
        }
        else
        {
        echo 
    'Failed';
        } 
    What it does is if the GET request is add (example.php?get=1), it inserts some data into the database. It also displays if it is a success or failed.

    however if i go to example.php it will think that the query has failed and display 'Failed'. I only want it to display this if it attempted the query.

  2. #2
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Move the message inside your IF statement so it's only executed IF add was passed.
    Code PHP:
    <?php
    if( ! empty($_GET['add']) && 0 !== (int)$_GET['add'])
    {
        $result = mysql_query(sprintf(
            "INSERT INTO dbFriends (my_id, friend_id, status) VALUES ('%s', %d, 0);",
            mysql_real_escape_string($_COOKIE['valid_id']),
            $_GET['add']
        ));
     
        echo $result ? 'Success' : 'Failed' ;
    }

    *empty probably considers 0 as empty, but you'll need to check; hence the additional conditional.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  3. #3
    SitePoint Addict
    Join Date
    Sep 2006
    Posts
    238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi this would not be possible as what I want to do after the GET request is redirect to the main page.
    Basically I want the notice to be displayed on the main page after the redirect.

  4. #4
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    You made no mention of a redirect.

    State what you require, in full, and hopefully someone wil help out.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  5. #5
    SitePoint Addict
    Join Date
    Sep 2006
    Posts
    238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi, I have already stated what I require.

    I want to display a notification outside the GET request.

  6. #6
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay then simple:
    Code php:
    if( ! empty($_GET['add']) && 0 !== (int)$_GET['add'])
    {
        $result = mysql_query(sprintf(
            "INSERT INTO dbFriends (my_id, friend_id, status) VALUES ('%s', %d, 0);",
            mysql_real_escape_string($_COOKIE['valid_id']),
            $_GET['add']
        ));
     
        $msg = $result ? 'Success' : 'Failed' ;
    	header("Location: main.php?msg=" . $msg);
    	exit();
    }
    Now you can get the message $_GET['msg'] in main page to echo wherever you want! Or even you can use one time SESSION variable (one time means just unset the SESSION variable once echoed in main page).
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5


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
  •