I want to display message box. I don't know why it is not working.

Instead of "Already Registered..!!" message it displays " Duplicate entry 'test' for key 'nom' " .

Thanking you in advanced.

    $query = " SELECT * FROM Humeur_log WHERE prenom =  '".$prenom."' AND nom =  '".$nom."' AND CURRENT_TIMESTAMP >= DATE_ADD( datelog, INTERVAL 24 HOUR ) LIMIT 0 , 30";
    $result = mysql_query($query);
    $NumResponse = mysql_num_rows($result);
    if ($NumResponse>0)
    {
        echo "<script type='text/javascript'>\n";
        echo "alert('You are already registered..!! ')\n";
        echo "</script>";
    }
    else
    {          
        $query = " INSERT INTO  `ogysformtryit`.`Humeur_log` (`logid` ,`prenom` ,`nom` ,`datelog` ,`humeurlog`)VALUES (NULL ,  '".$prenom."',  '".$nom."', CURRENT_TIMESTAMP ,  '".$humeur."') " ;
        $result = mysql_query($query) or die(mysql_error());;
        echo "<script type='text/javascript'>\n";
        echo "alert('Successfully registered..!! .!')\n";
        echo "</script>";
        echo "<br />";

The die() outputs that particular message. You are trying to insert a value that already exists in a unique column. You should do it like this:

$query = "...";
$result = mysql_query($query);
if ($result) {
    // output successfully registered script
}
else {
    // insert failed
}

There is a problem with this lines

$NumResponse = mysql_num_rows($result);
if ($NumResponse>0)

Actually, there is condition if row exist or not, and then i need to displayed that message. I have tried your code. But when i try to insert new user it is not working. I mean to say it displays wrong messgae box and not adding in database.

The message you first showed: "Duplicate entry 'test' for key 'nom'" indicates a database error. I think the column nom is set to be unique, so you cannot enter test again if it is already in your table.

I have removed that unique index from column nom. Still, i am not able to add new user.
In short, there should be two message box with proper message reading the input.

For that reason i have used $NumResponse. But it is not working anymore.

Please let me know the correction in my above code.

If you try your original code now, still the same problem? If so, try the query in phpMyAdmin with your test data, and see what that gives.

Is the javascript in your page and not showing, or not in your page at all?

Yes, but When i REFRESH the page it is adding each time. Before it was ok.

@pritaeas

Please let me know what is the reason behind it.

I have no way of testing your code right now. Apart from that, since you don't provide the table structures and some test data, I can't test anyway.

@pritaeas

I would like to understand the REFRESH page action and why it is adding each time.
I have table with primary key of logid.

Here it is:

|Column|Type|Null|Default
|//**logid**//|int(11)|No|
|prenom|varchar(30)|No|
|nom|varchar(30)|No|
|datelog|timestamp|Yes|CURRENT_TIMESTAMP
|humeurlog|varchar(20)|No|

|287|test|test2|2012-10-29 14:54:33|good

I would like to understand the REFRESH page action and why it is adding each time.

I don't know, perhaps $nom and $prenom are not set correctly. The query works, and the code looks fine. You'll need to add some echo statements so you can see what happens, or use a decent debugger.

I have set the $nom and $prenom as unique index in order to avoid duplicate entry.

Now, only thing that creats problem is when i try to test with duplicate entry, instead of message box, it displays "Duplicate entry 'test' for key 'nom".

I just need to replace that message with message box where i have used javascript.

My latest code:

$query = " SELECT * FROM Humeur_log WHERE prenom =  '".$prenom."' AND nom =  '".$nom."' AND CURRENT_TIMESTAMP >= DATE_ADD( datelog, INTERVAL 24 HOUR ) LIMIT 0 , 30";
$result = mysql_query($query);
$NumResponse = mysql_num_rows($result);
if($NumResponse>0)
{
    echo "<script type='text/javascript'>\n";
    echo "alert('TEST FAIL...')\n";
    echo "</script>";
}
else
{          
    $query = " INSERT INTO  `Humeur_log` (`logid` ,`prenom` ,`nom` ,`datelog` ,`humeurlog`)VALUES (NULL ,  '".$prenom."',  '".$nom."', CURRENT_TIMESTAMP ,  '".$humeur."') " ;
    $result = mysql_query($query) or die(mysql_error());;
    echo "<script type='text/javascript'>\n";
    echo "alert('DONE..!!')\n";
    echo "</script>";
}

@pritaeas
I have remove index from $nom and $prenom. Now, it's working. Thank you so much for quick response.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.