,我發現了以下錯誤:PHP函數重新聲明致命錯誤
Fatal error: Cannot redeclare register() (previously declared in
C:\xampp\htdocs\spikes\functions.php:2) in C:\xampp\htdocs\spikes\functions.php on line 20
這很奇怪,因爲我宣佈這只是一次,但錯誤說我重新聲明它。
<?php
function register($username,$password,$email,$firstname,$lastname)
{
$con=connect();
// Check connection
if (mysqli_connect_errno())
echo "Failed to connect to MySQL: " . mysqli_connect_error();
$date = date("Y/m/d");
mysql_set_charset('utf8');
$sql="INSERT INTO userlist (username, password, email, firstname, lastname, joined)
VALUES ('$username', '$password', '$email', '$firstname', '$lastname', '$date')";
if (!mysqli_query($con,$sql))
die('Error2: ' . mysqli_error($con));
mysqli_close($con);
$id = getIdByUserName($username);
createSession($id,$firstname);
}
function check($username,$password,$email,$firstname,$lastname)
{
$error = "";
$con=connect();
if (mysqli_connect_errno())
echo "Failed to connect to MySQL: " . mysqli_connect_error();
$result = mysqli_query($con,"SELECT * FROM userlist WHERE username='".$username."'");
$num_rows = mysqli_num_rows($result);
if ($num_rows>0)
$error = "*השם משתמש קיים במערכת";
$result = mysqli_query($con,"SELECT * FROM userlist WHERE email='".$email."'");
$num_rows = mysqli_num_rows($result);
if ($num_rows>0)
{
if(strlen($error)>0)
$error = $error."<br>"."*האימייל קיים במערכת";
else
$error = "*האימייל קיים במערכת";
}
if(strlen($error)<1)
register($username,$password,$email,$firstname,$lastname);
mysqli_close($con);
return $error;
}
function connect()
{
require ('config.php');
return mysqli_connect($host,$user,$password,$database);
}
function createSession($userid,$firstname)
{
$expire=time()+60*60*24*30;
setcookie("usid", $userid, $expire);
setcookie("usname", $firstname, $expire);
}
function getIdByUserName($username)
{
$con=connect();
if (mysqli_connect_errno())
echo "Failed to connect to MySQL: " . mysqli_connect_error();
$result = mysqli_query($con,"SELECT id FROM userlist WHERE username='".$username."'");
$row = mysqli_fetch_array($result);
$id = $row['id'];
return $id;
}
function getNameByUserName($username)
{
$con=connect();
if (mysqli_connect_errno())
echo "Failed to connect to MySQL: " . mysqli_connect_error();
$result = mysqli_query($con,"SELECT firstname FROM userlist WHERE username='".$username."'");
$row = mysqli_fetch_array($result);
return $row['firstname'];
}
function post($type, $brand, $gender, $size, $hand, $isNew, $price, $desc, $imgname1, $imgname2, $name, $userid, $phone)
{
$con=connect();
// Check connection
if (mysqli_connect_errno())
echo "Failed to connect to MySQL: " . mysqli_connect_error();
$date = date("Y/m/d");
mysql_set_charset('utf8');
$sql="INSERT INTO spikes (type, brand, gender, size, hand, new, price, description, imgname1, imgname2, date, name, userid, phone)
VALUES ('$type', '$brand', '$gender', '$size', '$hand', '$isNew', '$price', '$desc', '$imgname1', '$imgname2', '$date', '$name', '$userid', '$phone')";
if (!mysqli_query($con,$sql))
{
die('Error3: ' . mysqli_error($con));
}
mysqli_close($con);
}
function getFieldById($userid,$field)
{
$con=connect();
if (mysqli_connect_errno())
echo "Failed to connect to MySQL: " . mysqli_connect_error();
$result = mysqli_query($con,"SELECT $field FROM userlist WHERE id=$userid ");
$row = mysqli_fetch_array($result);
return $row[$field];
}
function getActivePosts()
{
$con=connect();
if (mysqli_connect_errno())
echo "Failed to connect to MySQL: " . mysqli_connect_error();
$result = mysqli_query($con,"SELECT * FROM spikes");
$num_rows = mysqli_num_rows($result);
mysqli_close($con);
return $num_rows;
}
function getUserActivePosts($id)
{
$con=connect();
if (mysqli_connect_errno())
echo "Failed to connect to MySQL: " . mysqli_connect_error();
$result = mysqli_query($con,"SELECT * FROM spikes WHERE userid=$id");
$num_rows = mysqli_num_rows($result);
mysqli_close($con);
return $num_rows;
}
function getAmountRegistered()
{
$con=connect();
if (mysqli_connect_errno())
echo "Failed to connect to MySQL: " . mysqli_connect_error();
$result = mysqli_query($con,"SELECT * FROM userlist");
$num_rows = mysqli_num_rows($result);
mysqli_close($con);
return $num_rows;
}
?>
我附加了其他功能,因爲我在註冊功能中使用了其中的一些功能。
**編輯
我包括文件從這個來源只有一次:
<?php
if(isset($_POST["username"]))
{
include('functions.php');
$error = check($_POST["username"],$_POST["password"],$_POST["email"],$_POST["firstname"],$_POST["lastname"]);
if(strlen($error) > 0)
{
echo $error;
}
else
{
echo "XXX";
}
}
?>
我敢打賭,你在這個文件中包含兩次這個文件 –
我真的不明白這樣的問題。 PHP爲您提供了完整的信息:您可以在第20行的C:\ xampp \ htdocs \ spikes \ functions.php中重新聲明register()函數,PHP甚至可以爲您提供**精確的位置: 'C:\ xampp \ htdocs \ spikes \ functions.php,在第2行**這是SAD ** :(對不起,我的靈魂正在尖叫:( –
)給函數提供更多描述性名稱總是一個好主意。顯然不夠描述。 – Veda