2011-08-06 26 views
0

在我的數據庫我有一個表,「文章」列「authorid」和一個表,「作者」列「ID」。這兩個表由這些列連接起來,因此當用戶提交文章時,「文章」表會在「authorid」列中顯示作者的ID時接收文章。但是,我無法使用當前用戶標識更新「authorid」列。每當用戶提交文章時,「authorid」列將返回0而不是顯示在「author」的「id」列中定義的id。獲取當前用戶登錄的數據

**PHP** 
    if (isset($_GET['add'])) 
     if (!userIsLoggedIn()) 
     include $_SERVER['DOCUMENT_ROOT'] . '/includes/login.inc.html.php'; 
     exit(); 
    } 
     include 'form.html.php'; 
     exit(); 

    if (isset($_GET['addform'])) 
    { 
     include $_SERVER['DOCUMENT_ROOT'] . '/includes/db.inc.php'; 

     $text = mysqli_real_escape_string($link, $_POST['text']); 
     $author = mysqli_real_escape_string($link, $_POST['author']); 

     $sql = "INSERT INTO article SET 
       articletext='$text', 
       articledate=CURDATE(), 
       authorid='$author'"; 
     if (!mysqli_query($link, $sql)) 
     { 
      $error = 'Error adding submitted article: ' . mysqli_error($link); 
      include 'error.html.php'; 
      exit(); 
     } 

     header('Location: .'); 
     exit(); 
    } 

我想我需要在一個數組來選擇用戶數據並存儲,然後存儲在$筆者變量數組,但我不能確定如何讓登錄的當前用戶的用戶數據,這是我userIsLoggedIn功能:

function userIsLoggedIn() 
{ 
    if (isset($_POST['action']) and $_POST['action'] == 'login') 
    { 
     if (!isset($_POST['email']) or $_POST['email'] == '' or 
      !isset($_POST['password']) or $_POST['password'] == '') 
     { 
      $GLOBALS['loginError'] = 'Please fill in both fields'; 
      return FALSE; 
     } 

     $password = md5($_POST['password'] . 'chainfire db'); 

     if (databaseContainsAuthor($_POST['email'], $password)) 
     { 
      session_start(); 
      $_SESSION['loggedIn'] = TRUE; 
      $_SESSION['email'] = $_POST['email']; 
      $_SESSION['password'] = $password; 
      return TRUE; 
     } 
     else 
     { 
      session_start(); 
      unset($_SESSION['loggedIn']); 
      unset($_SESSION['email']); 
      unset($_SESSION['password']); 
      $GLOBALS['loginError'] = 
        'The specified email address or password was incorrect.'; 
      return FALSE; 
     } 
    } 

    if (isset($_POST['action']) and $_POST['action'] == 'logout') 
    { 
     session_start(); 
     unset($_SESSION['loggedIn']); 
     unset($_SESSION['email']); 
     unset($_SESSION['password']); 
     header('Location: ' . $_POST['goto']); 
     exit(); 
    } 

    session_start(); 
    if (isset($_SESSION['loggedIn'])) 
    { 
     return databaseContainsAuthor($_SESSION['email'], $_SESSION['password']); 
    } 
} 

兩個表是通過在「文章」「AUTHORID」和「ID」的「作者」加入,你可以看到,我不能讓「AUTHORID」與「ID」,以更新,因爲,就像我上面所說的那樣,我不知道如何獲取當前登錄用戶的用戶ID並將其存儲在$authors中。

**article table** 
id articletext  articledate  authorid 
1 Test article 2011-08-05  0 

**author table** 
id name email   password 
1 user [email protected] d8a6582c02d188df9ad89a6affa412f7 

任何幫助將不勝感激,謝謝!

+0

你確定的AuthorID的成功發佈,例如$ _POST ['author']實際上包含這個值? – Pelshoff

+0

我認爲你的儀式,我的問題是如何獲得當前用戶的登錄ID?你的UserIsLoggedIn()函數中的 – rumspringa00

+0

,$ _SESSION ['loggedIn'] = TRUE附近創建一個$ _SESSION ['userid'] = $ id(你必須爲他們的id做一個db查找)。不要忘記在下面的「else」語句中取消設置$ _SESSION ['userid'] – neokio

回答

0

對於初學者,你的大括號有點奇怪。試試...

if (isset($_GET['add'])) { 
    if (!userIsLoggedIn()) { 
     include $_SERVER['DOCUMENT_ROOT'] . '/includes/login.inc.html.php'; 
     exit(); 
    } 
    include 'form.html.php'; 
    exit(); 
} 

...不知道這會解決您的問題,但因爲它的立場,你的代碼將exit;之前它曾經擊中if (isset($_GET['addform']))

+0

我的錯,這就是我的錯,只是一個錯字 – rumspringa00

+0

嘗試@ Pelshoff的建議。如果$ _POST ['author']沒有輸出值,form.html.php中的form可能有問題 – neokio

+0

謝謝,我想我需要一種方法來選擇當前用戶標識 – rumspringa00

相關問題