脚本标题的提前结束?

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');
    }
 }
 ?>