echo function is not showing any output - php

<?php
session_start();
if (isset($_POST["user_name"]) && isset($_POST["user_pwd"])) {
include"php/connect_to_mysql.php";
$user_name = preg_replace('#[^A-Za-z0-9]#i', '', $_POST["user_name"]);
$user_pwd = preg_replace('#[^A-Za-z0-9]#i', '', $_POST["user_pwd"]);
$sql = "SELECT * FROM user_ac WHERE user='$user_name' OR email='$user_name' AND pwd='$user_pwd' LIMIT 1";
$result = mysqli_query($myConnection, $sql);
if (mysqli_num_rows($result) > 0) {
echo "Successfully Logged In";
header("location: login.html?abc=234");
} else {
}
} else {
}
In this code, inside the second if statement, echo is not displaying anything, but the header function executes correctly.
Please help, thanks.

header function redirect you to new page, so your echo will appear in your current page but your code by this line:
header("location: login.html?abc=234");
will redirect you to new page.

You cannot see the echo because you are going to another page.
As test, remove the header("location: login.html?abc=234"); and you will see the echo.

Related

How to non-verify mobile number user redirect to verification page

If user is logged in then if his mobile number is verified then he will allow to move index.php else he will move to mobileverify.php. So i write a function and call this function in index page if unverified user tries to move in index.php function will redirect him mobileverify.php but function is not working please see the code below and tell me where i am wrong
function mobile_verify(){
if(isset($_SESSION['user_id'])){
$login = $_SESSION['user_id'];
$query =mysql_query("SELECT * FROM `users` WHERE `user_id`='$login'");
$row = mysql_num_rows($query);
$verify = $row['verify'];
if($verify === ""){
header('Location: mobileverify.php');
exit();
}
}
}
mysql_num_rows($query);
returns the number of rows in the result set not the values.
try
$row = mysql_fetch_array($result);
you are basically fetching the no of rows meeting your WHERE clause.
IT DOES NOT RETURN THE DATA ITSELF
you could use
$row = mysql_fetch_array($query);
and then check
$verify = $row['verify'];
if(!$verify){
header('Location: mobileverify.php');
}else{
header('Location: index.php');
}

Can I have 2 header locations in PHP?

So, I know my code was working and I have tried un-doing all steps to see where the bug is, but I am still keep getting an error
my php id=0.
Can you guys show me how I can fix my code up?
The error is as follows:
undefined variable list_id. It works on my localmachine but not when
uploaded to server.
Thanks.
The following is my code:
if(!empty($_GET['id'])){
$list_id = intval(($_GET['id']));
try {
$sql = 'SELECT * FROM items where id =' . $list_id;
$query = $pdo->prepare($sql);
$query->execute();
} catch(Exception $e) {
echo $e->getMessage();
die();
}
$list = $query->fetch(PDO::FETCH_ASSOC);
if ($list == FALSE) {
header("location: index.php");
}
}
if ($list_id == 0) {
header("location: index.php");
}
Seems there are a few issues here. I have added the comments inline.
if(!empty($_GET['id'])){
$list_id = intval($_GET['id']); //was double parenthesis here
try {
$sql = 'SELECT * FROM items where id =' . $list_id;
$query = $pdo->prepare($sql);
$query->execute();
} catch(Exception $e) {
echo $e->getMessage();
die();
}
$list = $query->fetch(PDO::FETCH_ASSOC);
$count = count($list); //count result and use for comparison instead of false
if ($count === 0) {
header("location: index.php");
exit;
}
} else {
header("location: index.php"); //if no $_GET, redirect
exit;
}
All you need to do is instantiate the variable $list_id :
$list_id = 0; // <-- HERE
if(!empty($_GET['id'])){
$list_id = intval(($_GET['id']));
...
...
It looks as though you want to redirect to the index if there is no id parameter. Since you already check for its presence, redirect in the else clause. Remove the last block and add:
else
{
header("location: index.php");
exit;
}
You may want to add exit; after the header() call in the if block as well, so that code that uses the database further down isn't executed.

Notice Undefine Offset PHP

I'm trying to redirect a page if the admin changes the page to not visible, by changing the visible value to zero, what is wrong with my code, i keep getting the error
<?php
$result = mysqli_query($con,"SELECT VISIBLE FROM menu WHERE id=0");
if($row = mysqli_fetch_array($result))
{
header('Location: ./unavailable.php? error=pagenotavaialbe');
}
else
?>
Try something like this
$result = mysqli_query($con,"SELECT VISIBLE FROM menu WHERE id=0");
if(mysqli_num_rows($result) == 0){
header('Location: ./unavailable.php? error=pagenotavaialbe');
exit;
} else {
$row = mysqli_fetch_array($result);
//do your code here
}
try this :
if($result)
{
if($row = mysqli_fetch_array($result))
{
header('Location: ./unavailable.php? error=pagenotavaialbe');
}
}

PHP If Statements Not Firing

I'm currently building a system for a football league. And are currently working on the script file for adding results. Most of the script works and the result is always successfully added to the database. However the authentication part seems to fail. The if statement on line 12 does not seem to fire and I can't understand why.
My code can be found in the pastebin link here: http://pastebin.com/ty4pdGgn
<?PHP
include 'functions.php';
dbConnect();
//$userEmail = mysql_real_escape_string($_POST["userEmailText"]);
$userCode = mysql_real_escape_string($_POST["userPasscodeText"]);
$authenticated = false;
$userEmail = "info#example.com";
if ($userEmail == "info#example.com") {
header('Location: ../results.php?error=authentication');
}
$allUsers = mysql_query("SELECT * FROM accounts WHERE email = '$userEmail'");
while ($thisUser = mysql_fetch_assoc($allUsers)){
if ($userCode != $thisUser['passCode']) {
header('Location: ../results.php?error=authentication2');
}
echo $thisUser['passCode'];
$authenticated = true;
$userID = $thisUser['userID'];
}
if (!$authenticated) {
header('Location: ../results.php?error=authentication3');
}
$dateSubmitted = $_POST['submissionDate'];
$homeTeam = $_POST['homeTeam'];
$awayTeam = $_POST['awayTeam'];
$homeGoals = $_POST['homeGoals'];
$awayGoals = $_POST['awayGoals'];
if ($homeTeam == $awayTeam) {
header("Location: ../results.php?error=team");
}
if (getTeamLeague($homeTeam) != getTeamLeague($awayTeam)) {
header("Location: ../results.php?error=league");
} else {
$leagueID = getTeamLeague($homeTeam);
}
if ($homeGoals > $awayGoals) {
$winnerID = $homeTeam;
} else if ($homeGoals < $awayGoals) {
$winnerID = $awayTeam;
} else if ($homeGoals == $awayGoals) {
$winnerID = -1;
}
$cQuery = mysql_query("INSERT INTO results VALUES ('', $userID, '$dateSubmitted', $leagueID, $homeTeam, $homeGoals, $awayTeam, $awayGoals, $winnerID, 0)");
if ($cQuery){
header('Location: ../results.php');
} else {
echo mysql_error();
}
?>
Any help with this matter will be much appreciated. The functions.php contains no errors as this is all to do with database entry and not the authentication.
Put a die(); after the header("Location:...");
As your comparison code (the "if" part on line 12) that you pasted has to work, i have two advice:
Put a die(); or exit(); after the header() part.
Try looking here, as I am not sure if header() will work, while the location path you set is relative. Basic advice is to always use base paths for redirects, like "http://your.site.com/script.php").

if query does not exist, redirect to another page

This code only redirects to notenrolled.php even if the input value is correct. I want it to continue the process if the value entered is correct. Is there something wrong with my code?
<?php
//Setup connection to the database
$connect = mysql_pconnect("localhost", "root", "")
or die(mysql_error());
//Connect to the database
mysql_select_db("dbgis", $connect) or die(mysql_error());
$query = "SELECT * from tbl_student WHERE stud_id= '$stud_id' ";
$result = mysql_query($query);
$totalrows = mysql_num_rows($result);
while($row = mysql_fetch_array($result))
{
header("Location: yesno.php");
}
if($totalrows != 0)
{
header("Location: notenrolled.php");
}
?>
I tried the die(); and it seems to be working because it just says a redirection looping error with yesno.php. So I think I might have put the code in the wrong .php page.
The flow is like this: I have a guard.php page where I could search a query(stud_id) using my search form in the page. I then want to check whether the query exists and if it doesn't, I want it to redirect to notenrolled.php else if the query is found, I want it to proceed to yesno.php.
When you set a Location header, you ALWAYS immediately follow it with exit or die().
(Only if you truly understand what you are doing, might you not immediately use it, but at your own risk.)
if ($totalrows > 0)
{ // has results
header("Location: yesno.php");
exit(0);
}
else
{ // no result
header("Location: notenrolled.php");
exit(0);
}
You should not use while just to evaluate if there is a record.
while($row = mysql_fetch_array($result))
{
header("Location: yesno.php");
}
Your code always redirects to notenrolled.php because of the codition:
if($totalrows != 0)
{
header("Location: notenrolled.php");
}
//this block will always be true if your $totalrows is greater than 0
The solution: check $totalrows if is greater than 0
if ($totalrows > 0){
header("Location: yesno.php");
} else {
header("Location: notenrolled.php");
}
u can use php function mysql_affected_rows to see number off affected rows in SELECT,
if (mysql_affected_rows() == 0){
header("Location: notenrolled.php");
} else {
header("Location: yesno.php");
}
The correct way to do it is this:
if($totalrows>0)
header("Location: yesno.php");
else
header("Location: notenrolled.php");
try this
if($totalrows == 0)
{
header("Location: notenrolled.php");
die();
}

Categories