Search code examples
phpmysqlsqlcomparison

how to compare the value in the same table in a different column


so i have a verification table consists of key_id, key_verification, confirm_key, and key_status

this is the sql process for client_verifycodez.php where i want to compare the value of key_verification and confirm_key if they are the same

the update sql is needed so that when client enter the key fro a form, it will enter into db as confirm_key. so by that i want to compare the value of confirm_key with the already existing key_verification

    $sql1 = "UPDATE verification SET confirm_key = '".$confirm_key."' WHERE key_id ='".$id."'";
    mysql_query($sql1);

    $sql = ("SELECT * FROM verification WHERE key_verification = confirm_key");
    $query = mysql_query($sql) or die ("Error: " . mysql_error());
    $check = mysql_fetch_array($query);

    if($check==true)
    {
    echo "<center>";
    echo "Your key is invalid!";
    echo "<br>";
    echo "<a href=client_verifycodez.php>Back </a>";
    echo "</center>";
    }
    else
    {
    header("Location: home.php");
    }

so what i need is how do i compare so that when key_verification and confirm_key is equals, it will go to home.php or else alert. i think i have the problem with the sql.

can anyone perhaps help me? thank you


Solution

  • You can't do like this, use sub-query,

    "SELECT * FROM verification WHERE key_id =". $id ." AND key_verification
     = (SELECT confirm_key FROM verification WHERE key_id =". $id .")"
    

    Note: Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO, or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.