這裏是我的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
請別人幫我一把。我第一次學習這門語言。
你應該學習參數化查詢,http://php.net/manual/en/mysqli.quickstart.prepared-statements.php – chris85