0

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 />";

Edited by PriteshP23: code

2
Contributors
12
Replies
13
Views
4 Years
Discussion Span
Last Post by PriteshP23
0

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
}
0

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.

Edited by PriteshP23: code

0

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.

Edited by pritaeas

0

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.

0

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?

Edited by pritaeas

0

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

0

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.

0

@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

Edited by PriteshP23: code

0

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.

0

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>";
}

Edited by PriteshP23: code

0

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.