2012-10-16 46 views
1

這是自助餐廳的代碼,這是登錄名。PHP中的SQL查詢錯誤

<?php 

$userna = 'root'; 
$paso = ''; 
$mach = 'localhost'; 
$db ='cafeteria'; 

session_start(); 

// GET PAGES RECORD FROM LOG TABLE: *********| Only the first time though: 
if (isset($_SESSION['log']) != 'logging') 
{ 
    // Here, just creating a string: 
    $pages_record = ""; 
    $insert_query = ''; 
    //  Get saved pages from the database: 
    $connection = mysqli_connect($mach,$userna,$paso,$db) or die ("Error in log-page script: AB-1 - query: $insert_query." . mysqli_error($connection)); 
    mysqli_select_db($connection,'cafeteria'); 

    //  Query string to pull all pages from table record: 
    $get_pages_query = "select * from log-page"; 

    //  Query the database, and save result: 
    $query_pages_result = mysqli_query($connection, $get_pages_query); 

    //  Check number of results returned: 
    $num_of_results = ''; 
    $num_of_results = mysql_num_rows($query_pages_result); 
    if ($num_of_results > 0) 
    { 
     // Loop through the result array: Each time, one row, and then the next one ... 
     for ($row = 0; $row < $num_of_results; $row++) 
     { 
      // Getting one row: 
      $get_row = mysqli_fetch_array($query_pages_result); 
      // Extracting just the page name from the row: 
      $one_page = substr($get_row["page"],strripos($get_row["page"],"/") + 1); 
      // Adding this page name to the string created previously: 
      if ($row == 0) 
      { 
       $pages_record .= $one_page; 
      } 
      else 
      { 
       $pages_record .= ",".$one_page; 
      }   
     } 

     // Once all pages have been read and saved to the string 
     // now we save it to the session: 
     $_SESSION['logpages'] = $pages_record; 
     $_SESSION['log'] = 'logging'; // This just tells us, we are logging pages to the database. 
    } 
    else 
    { 
     // There are no pages in the table: 
     $_SESSION['logpages'] = ""; 
     $_SESSION['log'] = 'logging'; // This just tells us, we are logging pages to the database. 
    } 
} 

// Check if page is already in session list. 
$pages_array = array(); 
if (strlen(isset($_SESSION['logpages'])) > 0) 
{ 
    // string variable that holds all pages separated by commas: 
    $pages_string = $_SESSION['logpages']; 

    // creating an Array to hold all pages already logged in server: 
    if (strstr($pages_string, ",")) 
    { 
     $pages_array = explode(",", $pages_string); 
    } 
    else // just means there's only one page in the record 
    { 
     // so, we push it inside the array. 
     array_push($pages_array, $pages_string); 
    } 

    // current page: [ We are extracting only the page, not the entire url, Exmp: login.php ] 
    $current_page = substr($_SERVER['PHP_SELF'],strripos($_SERVER['PHP_SELF'],"/") + 1); 

    // Check if current_page is in the array already: 
    if (!in_array($current_page, $pages_array)) 
    { 
     // IF is NOT in the array, then add it: 
     array_push($pages_array, $current_page); 

     // Add it to the Session variable too: 
     $pages_string = implode(",", $pages_array); 

     // Re-save it to SESSION: 
     $_SESSION['logpages'] = $pages_string; 

     // Now, add it to the database table "log-page"" 
     $connection = mysqli_connect($mach,$userna,$paso,$db) or die ("Unable to connect!"); 
     mysqli_select_db($connection,'cafeteria'); 

     // Query to insert page description into the table: 
     // [ date - time - page - user ] 
     $insert_query = "INSERT INTO log-page 
      (`date`, `time`, `page`, `user`) VALUES 
      ('".date("Y-m-d")."', '".date("H:i:s")."', '".$_SERVER['PHP_SELF']."', '".(isset($_SESSION['SESSION_UNAME']))."')"; 
      mysqli_select_db($connection,'cafeteria'); 

     // INSERTING INTO DATABASE TABLE: 
     mysqli_query($connection, $insert_query) or die ("Error in log-page script: AB-2 - query: $insert_query." . mysqli_error($connection)); 
     // Done! 
    } 
    else 
    { 
     // IF it IS in the list, just SKIP. 
    } 
} 
else 
{ 
    // means, that there are absolutely no pages saved in the database, basically this is the first log: 
    $_SESSION['logpages'] = substr($_SERVER['PHP_SELF'],strripos($_SERVER['PHP_SELF'],"/") + 1);  

    // Now, add it to the database table "log-page"" 
    $connection = mysqli_connect($mach,$userna,$paso,$db) or die ("Unable to connect!"); 
    mysqli_select_db($connection,'cafeteria'); 

    // Query to insert page description into the table: 
    // [ date - time - page - user ] 
    $insert_query = "INSERT INTO log-page 
     (date, time, page, user) VALUES 
     ('".date("Y-m-d")."', '".date("H:i:s")."', '".$_SERVER['PHP_SELF']."', '".(isset($_SESSION['SESSION_UNAME']))."')"; 
    mysqli_select_db($connection,'cafeteria'); 
    // INSERTING INTO DATABASE TABLE: 
    mysqli_query($connection,$insert_query) or die ("Error in log-page script: AB-2 - query: $insert_query." . mysqli_error($connection)); 
    // Done!  
} 

?> 

但現在我收到此錯誤:使用XAMPP 1.8.1 PHP

Error in log-page script: AB-2 - query: INSERT INTO log-page (date , time , page , user) VALUES ('2012-10-16', '16:58:44', '/caf/pages/index.php', '').You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-page (date , time , page , user) VALUES ('2012-10-16', '16:58:44' at line 1

林:5.4.7。它不會讓我登錄既不是管理員也不是出納員

+0

嘗試'''日期\''而不是'日期'。使用引號。日期是一個規格。單詞在MYSQL中。 –

+0

您的用戶提交的內容是否允許爲空?對於xampp,你是否更改root密碼並且無法登錄到phpmyadmin? –

+0

不,它不允許用戶null。根密碼是空的 – jjumar

回答

0

嘗試逃離現場名稱

(`date`, `time`, `page`, `user`) 
5

在反引號括起來表名這樣的(省略了查詢的其餘部分):

$insert_query = "INSERT INTO `log-page` (`date`, `time`, `page`, `user`) ... "; 

否則MySQL會嘗試將-解釋爲一個減號,在這種情況下失敗。

所示編輯

在最後插入,也列名應包含在反引號:

$insert_query = "INSERT INTO `log-page` (`date`, `time`, `page`, `user`) VALUES ..."; 
+0

是的,破折號是無效的列名稱,因此它必須包括反引號... – feeela

+0

我做到了,現在我有這個問題警告:mysql_num_rows()期望參數1是資源,布爾給出在第47行的C:\ xampp \ htdocs \ caf \ pages \ log.php – jjumar

+0

@jjumar看起來像另一個查詢中的同一個錯誤:在'log_page'周圍添加反引號:'$ get_pages_query =「select * from log-page ';' – Sirko

0

這已經有一段時間,因爲我看着MySQL的文檔,但它看起來是抱怨表名「log-page」。嘗試引用該表名稱。

+0

我做到了,現在我有這個問題警告:mysql_num_rows()期望參數1是資源,在第47行C:\ xampp \ htdocs \ caf \ pages \ log.php中給出的布爾值 – jjumar