2011-08-07 9 views
0

SESSION()에 저장된 변수를 SESSION() 컨트롤러에서 참조하려고하는데 코드에 약간의 문제가 있습니다. 내가 가진 것은 두 개의 테이블, 하나는 기사 용이고 다른 하나는 저자 용입니다. 현재 로그인 한 사용자의 저자 ID가 다음 function userIsLoggedIn() and stored in $_SESSION['id']쿼리에서 SESSION()을 참조하십시오.

에서 참조, function databaseContainsAuthor($email, $password, &$id)에 access.inc.php에서 SQL 쿼리를 통해 내 $id 변수에 저장됩니다> access.inc.php

<?php 
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, $id)) 
     { 
     include 'db.inc.php'; 
      session_start(); 
      $_SESSION['loggedIn'] = TRUE; 
      $_SESSION['email'] = $_POST['email']; 
      $_SESSION['password'] = $password; 
      $_SESSION['id'] = $id; 
      return TRUE; 
     } 
     else 
     { 
      session_start(); 
      unset($_SESSION['loggedIn']); 
      unset($_SESSION['email']); 
      unset($_SESSION['password']); 
      unset($_SESSION['id']); 
      $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']); 
     unset($_SESSION['id']); 
     header('Location: ' . $_POST['goto']); 
     exit(); 
    } 
    session_start(); 
    if (isset($_SESSION['loggedIn'])) 
    { 
     return databaseContainsAuthor($_SESSION['email'], $_SESSION['password'], $_SESSION['id']); 
    } 
} 
function databaseContainsAuthor($email, $password, &$id) 
{ 
    include 'db.inc.php'; 

    $email = mysqli_real_escape_string($link, $email); 
    $password = mysqli_real_escape_string($link, $password); 

    $sql = "SELECT COUNT(*) FROM author 
      WHERE email='$email' AND password='$password'"; 
    $result = mysqli_query($link, $sql); 

    if (!$result) 
    { 
     $error = 'Error searching for author.'; 
     include 'error.html.php'; 
     exit(); 
    } 
    $row = mysqli_fetch_array($result); 

    $sql = "SELECT id FROM author 
      WHERE email='$email'"; 
    $id = mysqli_query($link, $sql); 
    if (!$id) 
    { 
     $error = 'Error searching for id.'; 
     include 'error.html.php'; 
     exit(); 
    }  

    if ($row[0] > 0) 
    { 
     return TRUE; 
    } 
    else 
    { 
     return FALSE; 
    } 
} 
포함

에 저장된 현재 사용자의 ID가 들어있는 변수 $id이 있는데, 내 index.php의 SQL 쿼리에서 SESSION()을 사용하여 저자의 ID를 내 기사 테이블에있는 기사와 함께 삽입하고 싶습니다. 그래서 작가와 저자가 제출 한 기사는 링크되어 있습니다. 난 그냥 index.php를 내 SQL 쿼리에

기사를 $_SESSION['id']를 참조 할 올바른 코드를 구현 약간의 문제가 있어요> index.php를

<?php 
include_once $_SERVER['DOCUMENT_ROOT'] . 
     '/includes/magicquotes.inc.php'; 
include $_SERVER['DOCUMENT_ROOT'] . 
     '/includes/access.inc.php'; 

if (isset($_GET['add'])) 
    if (!userIsLoggedIn()) 
{ 
     include $_SERVER['DOCUMENT_ROOT'] . '/includes/login.inc.html.php'; 
     exit(); 
} 
    else 
{ 
    $pagetitle = 'New Article'; 
    $action = 'addform'; 
    $text = ''; 
    $authorid = ''; 
    $id = ''; 
    $button = 'Add article'; 

    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']); 
    $id = $_SESSION['id']; 

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

    header('Location: .'); 
    exit(); 
} 
include $_SERVER['DOCUMENT_ROOT'] . '/includes/db.inc.php'; 

$result = mysqli_query($link, 'SELECT id, articletext FROM article'); 
if (!$result) 
{ 
    $error = 'Error fetching articles: ' . mysqli_error($link); 
    include 'error.html.php'; 
    exit(); 
} 

while ($row = mysqli_fetch_array($result)) 
{ 
    $articles[] = array('id' => $row['id'], 'text' => $row['articletext']); 
} 

include 'articles.html.php'; 
?> 

내가 참조하기 위해 노력하고있어 SESSION()if (isset($_GET['addform']))이지만, 나는 이것을하기 위해 사용할 라이트 문법인지 확신 할 수 없다. 어떤 도움이라도 대단히 감사하겠습니다!

+4

스크립트 맨 위에 'error_reporting (E_ALL);'을 추가 한 다음 질문을 편집하고 그 오류를 포함 시키십시오 ... –

+0

Lawrence -'error_reporting (E_ALL);을 추가 할 때 오류가없는 것 같습니다. ', 그래서 문법이 정확하다고 생각합니다. 문제는 기사가 제출 될 때입니다. 내 기사 테이블의 authorid 열은 0 일 때 반환하고, 코드가 맞으면 기사를 제출 한 저자의 ID를 반환합니다. – rumspringa00

+0

당신은 OOP 일을 들여다보아야합니다. 무언가를 배울지도 모른다 –

답변

0

코드가 괜찮은 것 같습니다. $ id에 쿼리 바로 전에 예상되는 값이 있는지 확인하십시오. 사용자 ID에 항상 표시되는 기본값 (이 경우 0)이 있어야 할 때마다 0을 반환하기 때문에 쿼리의 바로 전에 값이 있어야합니다.

관련 문제