2013-10-22 95 views
-4

,我發現了以下錯誤: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"; 
    } 
} 
?> 
+2

我敢打賭,你在這個文件中包含兩次這個文件 –

+3

我真的不明白這樣的問題。 PHP爲您提供了完整的信息:您可以在第20行的C:\ xampp \ htdocs \ spikes \ functions.php中重新聲明register()函數,PHP甚至可以爲您提供**精確的位置: 'C:\ xampp \ htdocs \ spikes \ functions.php,在第2行**這是SAD ** :(對不起,我的靈魂正在尖叫:( –

+0

)給函數提供更多描述性名稱總是一個好主意。顯然不夠描述。 – Veda

回答

1

您包括/需要的文件兩次。第20行是函數聲明的結束}。

0

這意味着你已經有一個叫register()的功能。

也許你包含同一個文件兩次或有一個怪胎copypasta事故。檢查您的代碼是否有任何雙重功能。

0

更改您的需要語句require_once "<file>";

你也可以換登記在下面的代碼,以減輕你的迫在眉睫的問題:

if(!function_exists("register")) { 
    function register($username,$password,$email,$firstname,$lastname) { 
     // contents here 
    } 
} 

我敢打賭,你可能會包括functions.phpconfig.php內。

2

include() :包括每次調用時文件

include_once() :整個文檔中包含文件一次

require():使用時,文件是必不可少的,工作方式與包括()

require_once():一次整份文件

include_once() & require_once()避免重複

include() & require()創建複製

按照錯誤,PHP提供了完整的和最佳的錯誤報告,以瞭解與地方的錯誤發生的問題。

相關問題