Page 1 of 1

Validating a code from two diff tables column but same db

Posted: Tue Jan 07, 2014 10:18 am
by dolpaz
Hello Guys/Gurus
Please I have an issue, am some months into php and i need your help/assistance.
This is the flow. a client register at another site, when we confirm the registration, we send them a code.
The code is generated and saved in another table name called code.
I develop a form (http://cash2money2020.com/form.html)
So all i want is if someone inputs the generated code we sent to them, and filled it in the form, it makes a database checks to see if the code exists in the other table, if yes, submit form..if not, error message that the code is invalid and the form will not be submitted:
This is the code i have so far which saves the data into the database:

Code: Select all

$why = $sql->real_escape_string($_POST['why']);
$interesting = $sql->real_escape_string($_POST['interesting']);
$impressive = $sql->real_escape_string($_POST['impressive']);
$code = $sql->real_escape_string($_POST['code']);

$query	= 	" INSERT INTO registration (id, why, interesting, impressive,  generated_code, submitted_date) VALUES ('', '$why', '$interesting', '$impressive', '$code', now()) ";

$sql->query($query) or die($query.'<br />'.$sql->error);

if($_SERVER['REQUEST_METHOD'] == 'POST')
This is where am stucked and i dont know what to do next.

Also, this is the code the generates and saves data into the database:

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<?php
$sqli = @mysqli_connect ( 'localhost', 'root', 'wisdom', 'cash2money' )  OR die ( mysqli_connect_error() ) ; 
mysqli_set_charset( $sqli, 'utf8' ) ; 

//Checking if the user has already validated the form we show the string
if(isset($_POST["submit"]))
{
    $length = 10;
    $randomString =  substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWX‌​YZ"), 0, $length);
    echo "$randomString";
}
else //If he hadn't show the form
{
    ?>
    <form method="post" action="">
        <input type="submit" name="submit" value="Generate Code" />
        <input type="hidden" name="code" value="" />
    </form>
    <?php
}

$code = $sqli->real_escape_string($_POST['submit']);

$query = "INSERT INTO code (id, generated) VALUES ('','$randomString')";

$sqli->query($query) or die($query.'<br />'.$sql->error);


//From here, there is error... but the above code saves the generated code into the database.
$result = mysqli_query("SELECT generated FROM code ", $sqli);  //guess a count statement should work

$row = mysqli_fetch_array($result);

$table1 =  $result("generated_code `registration`", $sqli);

$table2 =  $result("generated `code`", $sqli );

if( json_encode($table1) ==  json_encode($table2)  ){
    echo "Code does not match";

}else{
    echo "It matched";
}


?>
</body>
</html>
Please who will help me out with the comparison of files between two tables :cry: :cry: :cry: :roll: :evil: :evil:

Re: Validating a code from two diff tables column but same d

Posted: Tue Jan 07, 2014 3:17 pm
by social_experiment
the second script seems to confuse me a bit ;) you say you want to compare a value in table 1 (registration?) against a value in table 2 (code?) but you generate a new value $randomString, for what purpose?

Code: Select all

<?php
$result = mysqli_query("SELECT generated FROM code ", $sqli);  //guess a count statement should work
?>
you want to select from code where the field containing the generated code is the same as the user entered value; you're correct in guessing that a COUNT() statement will work; it's easier imo.

Code: Select all

<?php
$table1 =  $result("generated_code `registration`", $sqli);

$table2 =  $result("generated `code`", $sqli );
?>
this also makes little sense to me; could you please explain :)