dtp19819 2016-01-11 18:43
浏览 6
已采纳

脚本标题的提前结束?

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.

  • 写回答

2条回答 默认 最新

  • douma5954 2016-01-11 19:09
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘