2016-06-01 46 views
2

这里是我的login.php我无法PHP中多次执行相同功能

require_once 'connect.inc.php'; 
require_once 'core.inc.php'; 

if(isset($_POST['username']) && isset($_POST['password'])){ 
    $username=$_POST['username']; 
    $password=$_POST['password']; 
    $password_hash=md5($password); 
    if(!empty($username) && !empty($password)){ 
     $query="SELECT `id` FROM `login` WHERE `username`='$username' AND `password`='$password_hash'"; 
     if($query_run = mysqli_query($stat,$query)){ 
      $query_num_rows = mysqli_num_rows($query_run); 
      if($query_num_rows == 0){ 
       echo 'You have entered wrong username or password !'; 
      } 
      else if($query_num_rows == 1){ 
       function mysqli_result($res, $row, $field) { 
        $res->data_seek($row); 
        $datarow = $res->fetch_array(); 
        return $datarow[$field]; 
       } 
       $user_id= mysqli_result($query_run, 0,'id'); 
       $_SESSION['user_id'] = $user_id; 
       header('Location: index.php'); 
      } 
     } 
     else 
     { 
      echo 'You have entered wrong username or password !'; 
     } 
    }else { 
     echo 'You must have to enter username and password'; 
    } 
} 
<form action="<?php echo $server; ?>" method="POST"> 
    Username: 
    <input type="text" placeholder="Enter the userid" name="username"> 
    Password: 
    <input type="password" placeholder="Enter your password" name="password">  
    <input type="submit" value="Log in"> 

下面的代码是针对的index.php

require_once 'core.inc.php'; 
require_once 'connect.inc.php'; 

if(get_status()){ 
$firstname=getinfo('firstname'); 
$lastname=getinfo('lastname'); 
echo 'You are logged in ,'.$firstname.' '.$lastname.''; 
echo "<br><a href='logout.php'>Logout</a><br>"; 
} 
else { 
include_once 'login.php'; 
} 

下面的代码是代码对于core.inc.php

session_start(); 
ob_start(); 
$server =$_SERVER['SCRIPT_NAME']; 
@$ref_url=$_SERVER['HTTP_REFERER']; 
function get_status(){ 
    if(isset($_SESSION['user_id']) && !empty($_SESSION['user_id'])){ 
     return true; 
    } 
    else 
     return false; 
} 

function getinfo($field){ 
    $get_query="SELECT `$field` FROM `login` WHERE `id`='".$_SESSION['user_id']."'"; 
    require 'connect.inc.php'; 
    if($query_run=mysqli_query($stat,$get_query)){ 
      function mysqli_result($res, $row, $f) { 
      $res->data_seek($row); 
      $datarow = $res->fetch_array(); 
      return $datarow[$f]; 
     } 
     if($query_result=mysqli_result($query_run,0,$field)){ 
      return $query_result; 
     } 
    } 
} 

在上述代码(在的index.php)当我使用$lastname=getinfo('lastname'); after using $firstname=getinfo('firstname');。它给了我以下错误。 Fatal error:Cannot redeclare mysqli_result() (previously declared in C:\xampp\htdocs\good\core.inc.php:18) in C:\xampp\htdocs\good\core.inc.php on line 18

请别人帮我一把。我第一次学习这门语言。

+0

你应该学习参数化查询,http://php.net/manual/en/mysqli.quickstart.prepared-statements.php – chris85

回答

2

定义功能mysqli_result功能外getinfo。然后从getinfo调用它。

由于您已在getinfo中定义了该函数,因此在调用第一次getinfo时将执行函数定义mysqli_result。当您第二次拨打getinfo时,mysqli_result的功能定义将再次执行。但它已经被定义,你会得到这个错误。

0

我想你有javascript的开发背景。

但在PHP中,我们声明功能不同的方式。

因此,例如,您core.php中应该是这样的:

session_start(); 
ob_start(); 
$server =$_SERVER['SCRIPT_NAME']; 
@$ref_url=$_SERVER['HTTP_REFERER']; 
function get_status(){ 
    if(isset($_SESSION['user_id']) && !empty($_SESSION['user_id'])){ 
     return true; 
    } 
    else 
     return false; 
} 

function mysqli_result($res, $row, $f) { 
    $res->data_seek($row); 
    $datarow = $res->fetch_array(); 
    return is_array($datarow)?$datarow[$f]:false; 
} 

function getinfo($field){ 
    $get_query="SELECT `$field` FROM `login` WHERE `id`='".$_SESSION['user_id']."'"; 
    require 'connect.inc.php'; 
    if($query_run=mysqli_query($stat,$get_query)){ 
     if($query_result=mysqli_result($query_run,0,$field)){ 
      return $query_result; 
     } 
    } 
} 

现在我们知道,我们在core.php

所以如果任何PHP文件将require_once 'core.inc.php';我们不需要再声明daclared mysqli_result功能这个功能。在你的情况下index.php你叫include_once 'login.php';login.php你试图重新声明mysqli_result这是多余的。

else if($query_num_rows == 1){ 
    $user_id= mysqli_result($query_run, 0,'id'); 
    $_SESSION['user_id'] = $user_id; 
    header('Location: index.php'); 
} 

我猜你只是初学者在PHP和其他开发人员可以提供一些其他的建议:所以你login.php片段进行改造。但我的回答只是关于你的问题,为什么你有一个错误 Cannot redeclare mysqli_result()

0

请所有善良的缘故,使用PDO

$query = "SELECT id FROM login WHERE username=? AND password=?"; 
$stmt = $pdo->prepare($query); 
$stmt->execute([$username, $password]); 
$user_id = $stmt->fetchColumn(); 
if($user_id) { 
    $_SESSION['user_id'] = $user_id; 
    header('Location: index.php'); 
    exit; 
} else { 
    echo 'You have entered wrong username or password !'; 
} 

此。是。所有。您需要的代码。这几行代替了所有可怕的混乱,试图重新设置一个死的mysql_result函数。