Notice Undefine Offset PHP - 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');
}
}

Related

Show index page if $_GET["page"] isn't set

I'm making a CMS with functions to add your own pages. However, I'm stuck.
As you can see, I've posted my code below. This code creates the pages which have been put in the database.
Problem is, I want to show the index page if $_GET['page'] isn't set. Index page is ID 1. The user cannot delete ID 1 from the backend.
$stmt = $dbConnection->prepare('SELECT * FROM paginas');
$stmt->execute();
$result = $stmt->get_result();
if(mysqli_num_rows($result) > 0) {
while ($row = $result->fetch_assoc()) {
if(isset($_GET['page']) && $_GET['page'] == $row['name']) {
?>
// content content...
<?php
} else {
?>
// show ID 1 content...
<?php
}
}
} else {
echo "The user has not created any pages yet!";
}
How can I do this?
This should work:
if (isset($_POST["page"]) && !empty($_POST["page"])) {
if($_GET['page'] === $row['name']){
print_r($row); // show data specific to id if page is set
}
}else{
//echo "Page is not set";
if($row['id'] == 1){ //show data of id=1 if page is not set
print_r($row);
}
}

how to check whether a value is present in database or not in php

i know this is simple..but i am unable to find the error..i have a login page where i take the input from user i.e. the username and password.then another page i am checking whether the values are present in the database or not.but neither it is giving me an error nor its working..moreover its not entering the first if condition..if ($result->num_rows > 0)
<body style="background-color:lightgrey;">
<?php
include('custdb.php');
session_start();
$uname=$_POST['username'];
$pass=$_POST['password'];
$sql = "SELECT * FROM `info` WHERE `username`='".$uname."';";
//echo $sql;
$result = $conn->query($sql);
echo"1";
echo $result;
if ($result->num_rows > 0)
{
echo"1";
while($row = $result->fetch_assoc())
{
if($uname==$row["username"])
{
header("location:custprofile.php");
}
else
{
header("Location:custindex.php");
}
}
}
else
{
echo "invalid input";
echo '<h4 align="left">LOGIN </h4>';
}
?>
Try to change this portion of your code,
$uname=$_POST['username'];
$pass=$_POST['password'];
$sql = "SELECT * FROM `info` WHERE username='".$uname."' AND password='".$pass."'";

echo function is not showing any output

<?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.

Premature End of Script Headers?

I have a problem with the code, it is the premature execution error when using header.
Code:
<?php
session_start();
require 'config.php';
$prepend = "<span class='welcome'>";
$append = "</span>";
if (!isset($_SESSION['name'])) {
header("Location: login.php");
}
echo $prepend."Здравей ".$_SESSION['name'].$append."</br>";
if (isset($_POST['submit']))
{
$newname = mysql_real_escape_string($_POST['newname']);
$newpass = mysql_real_escape_string($_POST['newpass']);
$oldpass = mysql_real_escape_string($_POST['oldpass']);
$checkPass = "SELECT pass from admin WHERE pass = '$_POST[oldpass]'";
$rs = mysqli_query($connect,$checkPass);
$data = mysqli_fetch_array($rs, MYSQLI_NUM);
if ($data > 0)
{
$query = "UPDATE admin SET pass ='".$_POST['newpass']."',name ='".$_POST['newname']."'" ;
$result = mysqli_query($connect, $query);
if ($result === true)
{
echo "Update sucessfuly!";
}
}
else {
header('Location: admin.php?failed=1');
}
}
?>
The first time when you open the page the else part is performed immediately and I can not understand why.
First you have 2 weird lines in your code:
$rs = mysqli_query($connect,$checkPass);
$data = mysqli_fetch_array($rs, MYSQLI_NUM);
Those function don't exist, in fact you probably used the mysql_...() ones, as it seems confirmed by the previous statements.
Now when you execute
$data = mysql_fetch_array($rs, MYSQLI_NUM);
then $data is an array (the next record returned) or FALSE (when no more record exist. And this statement should belong to a loop.
Anyway, in the current form of your code, when you execute if ($data > 0), it can't return anything significative since $data is an array.
So you must refactor all this piece of code according to your need (I guess you want to control that pass was really found by the previous query).
the first time you open page, the else part is executed because the session variables are not set, you need to set session variables first.
$_SESSION['sessionName']= $value;
you must have done this on some other page, if so, then please share the code.
and try using
if(mysqli_num_row($data)>0)
{
$query = "UPDATE admin SET
pass='".$_POST['newpass']."',name='".$_POST['newname']."'" ;
$result = mysqli_query($connect, $query);
if ($result === true)
{
echo "Update sucessfuly!";
}
}
else{
header('Location: admin.php?failed=1');
}
}
?>

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