I am trying to show an echo when a user improperly enters their license key, though, for some reason, the message appears twice.
$invalidkey = '<!DOCTYPE HTML>
<html>
<body>
<center>
<div class="container2"
<h1>Your Product Key is Invalid!</h1>
</div>
</center>
</body>
</html>
';
if ($resulto->num_rows > 0)
{
while($row = $resulto->fetch_assoc())
{
$user_group = $row["LicenseKey"];
$days = $row["Count"];
// Key is valid
if ($user_group == $key)
{
$keyvalidated = true;
echo $user_group;
echo $key;
}
// Key is invalid
else if ($usergroup !== $key)
{
echo $invalidkey;
$keyvalidated = false;
}
}
}
Here is an image of the error actually appearing:
My guess is that for whatever reason there are actually two records in the result set. The best fix would be to find a way to run the right query, which just returns a single record for a single user. As a quick fix, perhaps just check the first record:
if ($resulto->num_rows > 0) {
$row = $resulto->fetch_assoc();
$user_group = $row["LicenseKey"];
$days = $row["Count"];
// Key is valid
if ($user_group == $key) {
$keyvalidated = true;
echo $user_group;
echo $key;
}
// Key is invalid
else if ($usergroup !== $key) {
echo $invalidkey;
$keyvalidated = false;
}
}
Again, you need to find out why the result set has two, or more than one, records.
Please try adding a break since there could be more than one iteration (or you wouldn't need a loop):
// Key is invalid
else if ($usergroup !== $key)
{
echo $invalidkey;
$keyvalidated = false;
break;
}
The reason why it's showing 2 times is because query is fetching 2 results. So you need to limit query to just 1, just read the first record, or break the loop after 1st iteration.
while($row = $resulto->fetch_assoc())
{
$user_group = $row["LicenseKey"];
$days = $row["Count"];
// Key is valid
if ($user_group == $key)
{
$keyvalidated = true;
echo $user_group;
echo $key;
}
// Key is invalid
else if ($usergroup !== $key)
{
echo $invalidkey;
$keyvalidated = false;
}
break;
}
Related
I have a query for one of my search function. A while loop is applied to this query. I have used many sets of if functions inside this while loop to further filter the results as per the user search requirement. And the {email id} (one of the variable in the step1 result) of the final filter result in the if statement set is used to query another table where in a foreach loop is used & obtain other specific details in that table.
// WHILE SEARCH PROFILES
while ($result = mysql_fetch_array($result_finda)) {
$emails=$result['email'];
$age=$result['age'];
$name=$result['name'];
$height=$result['height'];
$groups=$result['groups'];
$country=$result['country'];
if (($age_from <= $age)&& ($age <= $age_to)) {
$a_emails =$emails;
$a_age=$age;
$a_firstname=$name;
$a_height=$height;
$a_groups=$groups;
$a_country=$country;
}
if (($height_from <= $a_height)&& ($a_height <= $height_to)) {
$h_emails =$a_emails;
$h_age=$a_age;
$h_firstname=$a_firstname;
$h_groups=$a_groups;
$h_country=$a_country;
}
foreach ($_POST['groups'] as $workgroup) {
if (strpos($workgroup, $h_groups) !== false) {
$m_emails =$h_emails;
$m_age=$h_age;
$m_name=$h_name;
$m_groups=$h_groups;
$m_country=$h_country;
}
}
foreach ($_POST['country'] as $workcountry) {
if (strpos($workcountry, $m_country) !== false) {
$c_emails =$m_emails;
$c_age=$m_age;
$c_name=$m_name;
$c_groups=$m_groups;
$c_country=$m_country;
}
}
$findeducation="SELECT * FROM education WHERE email='$c_emails'";
$result_findeducation=mysql_query($findeducation);
$educationstatus = mysql_fetch_array($result_findeducation);
$profile_ed=$educationstatus['education'];
foreach ($_POST['education'] as $educationstatus) {
if (strpos($educationstatus, $profile_ed) !== false) {
$e_emails =$_c_emails;
$e_age=$c_age;
$e_name=$c_name;
$e_groups=$c_groups;
$e_country=$c_country;
$e_education=$profile_ed;
}
}
if ($e_name) {
$count++;
}
echo $e_name;echo ' ';
echo $e_emails;echo ' ';
echo $e_age;echo '</br>';
echo $e_groups;echo '</br>';
echo $e_country;echo '</br>';
}
I'm getting the desired result and The results are filtered and shown as per the code above.
My problem is that I'm not getting the total COUNT of the results displayed. Kindly advise for a solution.
Lot of thanks in advance.
SQL :
$count = mysql_num_rows($result_finda);
PHP :
$count = count(mysql_fetch_array($result_finda));
EDIT
Use the following :
$count = 0; // Initialize $count
while ($result = mysql_fetch_array($result_finda))
{
// Filter your results
$count++; // Increment $count for each result that matches with your filters
}
print $count; // number of results after filtering
In your code :
$finda="SELECT * FROM USERS WHERE gender='$gender'";
$result_finda=mysql_query($finda);
$count = 0;
while ($result = mysql_fetch_array($result_finda)) {
$emails=$result['email'];
$age=$result['age'];
$name=$result['name'];
$height=$result['height'];
$groups=$result['groups'];
$country=$result['country'];
if (($age_from <= $age)&& ($age <= $age_to)) {
$a_emails =$emails;
$a_age=$age;
$a_firstname=$name;
$a_height=$height;
$a_groups=$groups;
$a_country=$country;
}
if (($height_from <= $a_height)&& ($a_height <= $height_to)) {
$h_emails =$a_emails;
$h_age=$a_age;
$h_firstname=$a_firstname;
$h_groups=$a_groups;
$h_country=$a_country;
}
foreach ($_POST['groups'] as $workgroup) {
if (strpos($workgroup, $h_groups) !== false) {
$m_emails =$h_emails;
$m_age=$h_age;
$m_name=$h_name;
$m_groups=$h_groups;
$m_country=$h_country;
}
}
foreach ($_POST['country'] as $workcountry) {
if (strpos($workcountry, $m_country) !== false) {
$c_emails =$m_emails;
$c_age=$m_age;
$c_name=$m_name;
$c_groups=$m_groups;
$c_country=$m_country;
}
}
$findeducation="SELECT * FROM education WHERE email='$c_emails'";
$result_findeducation=mysql_query($findeducation);
$educationstatus = mysql_fetch_array($result_findeducation);
$profile_ed=$educationstatus['education'];
foreach ($_POST['education'] as $educationstatus) {
if (strpos($educationstatus, $profile_ed) !== false) {
$e_emails =$_c_emails;
$e_age=$c_age;
$e_name=$c_name;
$e_groups=$c_groups;
$e_country=$c_country;
$e_education=$profile_ed;
}
}
if ($e_name) {
$count++;
}
echo $e_name;echo ' ';
echo $e_emails;echo ' ';
echo $e_age;echo '</br>';
echo $e_groups;echo '</br>';
echo $e_country;echo '</br>';
}
print "$count RESULTS"; // [number] RESULTS
when i wanna get all value to check with current user ip it just check last ip with current user , i don't know why before values doesnt check.
i fill IPs in a textarea like this : 176.227.213.74,176.227.213.78
elseif($maintenance_for == '2') {
$get_ips = $options['ips'];
$explode_ips = explode(',',$get_ips);
foreach ($explode_ips as $ips) {
if($ips == $_SERVER["REMOTE_ADDR"]){
$maintenance_mode = true;
}
else {
$maintenance_mode = false;
}
}
}
If you found the right value, you wan't to BREAK out of the foreach loop
$get_ips = $options['ips'];
$explode_ips = explode(',', $get_ips);
foreach($explode_ips as $ips) {
if ($ips == $_SERVER["REMOTE_ADDR"]) {
$maintenance_mode = true;
break; // If the IP is right, BREAK out of the foreach, leaving $maintenance_mode to true
} else {
$maintenance_mode = false;
}
}
yes, you will always override it. Its better to set a default and only set it once:
(Edit: added #Mathlight's answer, the break, in my solution as he suggested)
$maintenance_mode = false;
foreach ($explode_ips as $ips) {
if($ips == $_SERVER["REMOTE_ADDR"]){
$maintenance_mode = true;
break;
}
}
EDIT : another solution for the record, for the points of a oneliner
$maintenance_mode = in_array($_SERVER["REMOTE_ADDR"], $explode_ips);
I am trying to GET different rows from different columns in php/mysql, and pack them into an array. I am able to successfully GET a jason encoded array back IF all values in the GET string match. However, if there is no match, the code echos 'no match', and without the array. I know this is because of the way my code is formatted. What I would like help figuring out, is how to format my code so that it just displays "null" in the array for the match it couldn't find.
Here is my code:
include '../db/dbcon.php';
$res = $mysqli->query($q1) or trigger_error($mysqli->error."[$q1]");
if ($res) {
if($res->num_rows === 0)
{
echo json_encode($fbaddra);
}
else
{
while($row = $res->fetch_array(MYSQLI_BOTH)) {
if($_GET['a'] == "fbaddra") {
if ($row['facebook'] === $_GET['facebook']) {
$fbaddr = $row['addr'];
} else {
$fbaddr = null;
}
if ($row['facebookp'] === $_GET['facebookp']) {
$fbpaddr = $row['addr'];
} else {
$fbpaddr = null;
}
$fbaddra = (array('facebook' => $fbaddr, 'facebookp' => $fbpaddr));
echo json_encode($fbaddra);
}
}
}
$mysqli->close();
UPDATE: The GET Request
I would like the GET request below to return the full array, with whatever value that didn't match as 'null' inside the array.
domain.com/api/core/engine.php?a=fbaddra&facebook=username&facebookp=pagename
The GET above currently returns null.
Requests that work:
domain.com/api/core/engine.php?a=fbaddra&facebook=username or domain.com/api/core/engine.php?a=fbaddra&facebookp=pagename
These requests return the full array with the values that match, or null for the values that don't.
TL;DR
I need assistance figuring out how to format code to give back the full array with a value of 'null' for no match found in a row.
rather than assigning as 'null' assign null. Your full code as follows :
include '../db/dbcon.php';
$res = $mysqli->query($q1) or trigger_error($mysqli->error."[$q1]");
if ($res) {
if($res->num_rows === 0)
{
echo json_encode('no match');
}
else
{
while($row = $res->fetch_array(MYSQLI_BOTH)) {
if($_GET['a'] == "fbaddra") {
if ($row['facebook'] === $_GET['facebook']) {
$fbaddr = $row['dogeaddr'];
//echo json_encode($row['dogeaddr']);
} else {
$fpaddr = null;
}
if ($row['facebookp'] === $_GET['facebookp']) {
$fbpaddr = $row['dogeaddr'];
//echo json_encode($row['dogeaddr']);
} else {
$fbpaddr = null;
}
$fbaddra = (array('facebook' => $fbaddr, 'facebookp' => $fbpaddr));
echo json_encode($fbaddra);
}
}
}
$mysqli->close();
You can even leave else part altogether.
Check your code in this fragment you not use same names for variables:
if ($row['facebook'] === $_GET['facebook']) {
$fbaddr = $row['dogeaddr'];
//echo json_encode($row['dogeaddr']);
} else {
$fpaddr = 'null';
}
$fbaddr not is same as $fpaddr, this assign wrong result to if statement.
It was the mysql query that was the problem.
For those who come across this, and need something similar, you'll need to format your query like this:
** MYSQL QUERY **
if ($_GET['PUTVALUEHERE']) {
$g = $_GET['PUTVALUEHERE'];
$gq = $mysqli->real_escape_string($g);
$q1 = "SELECT * FROM `addrbook` WHERE `facebookp` = '".$gq."' OR `facebook` = '".$gq."'";
}
** PHP CODE **
if($_GET['PUTVALUEHERE']{
echo json_encode($row['addr']);
}
I have a code like this
First looping count how many post the array:
for($i = 0; $i < $jumlah_qty ;$i++) {
if(!empty($qty[$i]) && !empty($id_cat[$i])) {
Insert booking:
$insert_booking_hd = $user_class->select($az);
$id_cates = $id_cat[$i];
for($b = 0;$b<$qty[$i];$b++) {
First validation if $_POST[$id_cates) is set run this code:
if(isset($_POST[$id_cates."".$b])){
$id_seat = $_POST[$id_cates."".$b];
Find the seat number in $select_seat and find if seat number is exist in $seat_number:
$select_seat = $user_class->select($query);
$seat_number = $user_class->select($querys);
$row_seat = $user_class->numrows($select_seat);
$row_seat2 = $user_class->numrows($seat_number);
if($row_seat>0) {
$update_seat = $user_class->update($update_false);
$bol[$b] = FALSE;
} else {
if( $row_seat2>0 ) {
$insert_booking_dt = $user_class->insert($insert);
$update_seat = $user_class->update($update_true);
$bol[$b] = TRUE;
} else {
$bol[$b] = FALSE;
}
}
} else {
$insert_booking_dt = $user_class->insert($insert_without_seat);
$bol[$b] = TRUE;
}
if($bol[$b]) {
echo "FALSE";
header("location:../../../print.php?id=$id_booking");
}
else {
echo "WRONG";
header("location:../../../event.php?msg=Same seat number");
}
}
}
}
Anything wrong with my php validation?
Because if I input array of $id_seat it will always redirect to print.php although validation is FALSE
for example if I input 3 array and then I echo FALSE WRONG FALSE FALSE
still redirect to print.php not to event.php
How can I read if one of array is get WRONG and then redirect to event.php?
How can I read if one of array is get WRONG and then redirect to event.php?
You may break out of for-loops.
Instead of:
else {
echo "WRONG";
header("location:../../../event.php?msg=Same seat number");
}
You could try:
else {
echo "WRONG";
header("location:../../../event.php?msg=Same seat number");
break 2;
}
I have one more question for the day. I'm trying to make my biography page totally customizable for my custom CMS project I'm doing. If you notice in the view I have 3 h2 tags for Quotes, Allies, Rivals. What I would like to do is put the h3's into my db and then have it do a foreach loop for each one of them so I'm thinking some how I"m going to have to store the function that goes with the pageheading that way it doesn't have to run it if its not active on the page. I know this can be easily done however there's too much for me to focus on what I need to do in order to complete this. Keep in mind that depending on which page you are in the bio will impact which headings will be available.
As of right now here is my controller:
$activeTemplate = $this->sitemodel->getTemplate();
$footerLinks = $this->sitemodel->getFooterNav();
$bodyContent = "bio";//which view file
$bodyType = "main";//type of template
$this->data['activeTemplate'] = $activeTemplate;
$this->data['footerLinks']= $footerLinks;
$this->load->model('biomodel');
if($character !== "jfkdlsjl")
{
if((!empty($character))||(!isset($character))||(trim($character) !== '')||($character !== NULL))
{
$bioArray = $this->biomodel->getCharacterBio($character);
if ($bioArray == "empty")
{
$this->data['bioArray']= array();
}
else
{
if (($bioArray[0]->characters_statuses_id == 2)||($bioArray[0]->characters_statuses_id == 3)||($bioArray[0]->characters_statuses_id == 5))
{
$this->data['bioArray']= array();
}
else
{
$this->data['bioArray']= $bioArray;
$bioPagesArray = $this->biomodel->getBioPages();
$alliesArray = $this->biomodel->getCharacterAllies($bioArray[0]->id);
$rivalsArray = $this->biomodel->getCharacterRivals($bioArray[0]->id);
$quotesArray = $this->biomodel->getCharacterQuotes($bioArray[0]->id);
$this->data['bioPagesArray']= $bioPagesArray;
$this->data['alliesArray']= $alliesArray;
$this->data['rivalsArray']= $rivalsArray;
$this->data['quotesArray']= $quotesArray;
}
}
}
}
And here is my view:
echo "<h2>Quotes</h2>";
if (!empty($quotesArray))
{
echo "<ul>";
for($x = 0; $x <= (count($quotesArray)-1); $x++)
{
echo "<li>".stripslashes($quotesArray[$x]->quote)."</li>";
}
echo "</ul>";
}
echo "<h2>Allies</h2>";
if (!empty($alliesArray))
{
echo "<ul>";
foreach ($alliesArray as $row)
{
echo "<li>".stripslashes($row)."</li>";
}
echo "</ul>";
}
echo "<h2>Rivals</h2>";
if (!empty($rivalsArray))
{
echo "<ul>";
foreach ($rivalsArray as $row)
{
echo "<li>".stripslashes($row)."</li>";
}
echo "</ul>";
}
I don't know what you mean about storing the function nor which function you don't want to run.
Assuming we're working with the last else statement in your controller
$alliesArray = $this->biomodel->getCharacterAllies($bioArray[0]->id);
$rivalsArray = $this->biomodel->getCharacterRivals($bioArray[0]->id);
$quotesArray = $this->biomodel->getCharacterQuotes($bioArray[0]->id);
... and the function you "don't want to run" is the foreach loop on the array in the view, just handle the logic in your view:
if(($this->uri->segment(n)=='pageIwantQuotesOn') && (!empty($quotesArray)){
echo "<h2>Quotes</h2>";
echo "<ul>";
for($x = 0; $x <= (count($quotesArray)-1); $x++)
{
echo "<li>".stripslashes($quotesArray[$x]->quote)."</li>";
}
echo "</ul>";
}
...