When try to print a row of database according to one criteria(Email, name), keep in mind the following area:

<?php

if (isset($_POST[‘submit’])){  //This line to know if the page if from submission or url load

$email=$_POST[’email’];
$con = mysqli_connect(‘mysql_address’,’username’,’password’,’databasename’);
$query = “SELECT * FROM movie_order”;
$data=mysqli_query($con,$query) or die(‘error geting data’);

while($row = mysqli_fetch_array($data)){

if ($email==$row[’email’]){
echo ‘Your name:’.$row[‘name’];
echo ‘Email:’.$row[’email’];
echo ‘Phone number:’.$row[‘phone’];
echo ‘Address:’.$row[‘address’];
echo ‘Payment:’.$row[‘total’];
}else {echo ‘<p style=”color:red; font-size:12px;”>Please check if you entered the right Email address, or else, there is no record related to this Email</p>’ //warning info.
}

}

mysqli_close($con);

}
?>

<form id=”trace” action=”<?php echo $_SERVER[‘PHP_SELF’] ?>” method=”post”>  //make the form sticky.
<label for=”email”> Enter your email:</label>
<input type=”text” autocomplete=”off” name=”email” id=”email”/>
<input type=”submit” name=”submit” id=”submit”/>
</form>

  • First try: This code works fine when you typed the correct email address to query the customer info. But if you typed the wrong email address, the code will print out all the comparing result, for instance, if you have 10 rows in the database, it will print out 10 times the  warning info.Think about if you have 10,000 entries in the database .
  • Second try: Then I insert the break; into the else clause, the problem became this: if the database contains the customer info, but not in the first line, the code will just print out warning message and stop the while condition.
  • Third try: Then I decided to use a variable to record the comparing result and print out the result after the while clause, so the while clause will not print again and again. So the procedure is like this: A: if the while clause find the right entry, print the result and break the loop immediately at the same time give the variable the value “true”; B: if the while clause could not find the right entry, do not print anything and keep on looping, if find the right entry go to A, if not, keep on doing B until the end, and give the variable value “false”. C: use if condition, if the variable value is false, print the  warning, if true, do nothing. This time the code works!

And the code like this:

<?php

if (isset($_POST[‘submit’])){  //This line to know if the page if from submission or url load

$email=$_POST[’email’];
$con = mysqli_connect(‘mysql_address’,’username’,’password’,’databasename’);
$query = “SELECT * FROM movie_order”;
$data=mysqli_query($con,$query) or die(‘error geting data’);

while($row = mysqli_fetch_array($data)){

if ($email==$row[’email’]){
echo ‘Your name:’.$row[‘name’];
echo ‘Email:’.$row[’email’];
echo ‘Phone number:’.$row[‘phone’];
echo ‘Address:’.$row[‘address’];
echo ‘Payment:’.$row[‘total’];

$iffind=true;
break;

}else {$iffind=false;
}

}
if(!($iffind)){echo ‘<p style=”color:red; font-size:12px;”>Please check if you entered the right Email address, or else, there is no record related to this Email</p>’;
}
mysqli_close($con);

}
?>

The final link is http://frankfu.click/trace.php , and this is the page for inputting the information to database: http://frankfu.click/order.html .