Some help with notifications

I have this 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.

Move the message inside your IF statement so it’s only executed IF add was passed.


<?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.

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.

You made no mention of a redirect.

State what you require, in full, and hopefully someone wil help out.

Hi, I have already stated what I require.

I want to display a notification outside the GET request.

Okay then simple:


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).