2010-12-16 86 views
1

好吧讓我在這裏解釋一下自己:MySQL查詢正在運行但未運行

我正在製作一個基於在線文本的遊戲。我有一個頁面,3件事情都可能發生:

  • 他們可以創建一個位置
  • 可以編輯的位置
  • 可以刪除的位置
  • 到目前爲止,我已經創造了崗位工作。我接着刪除了一個職位。一切都很好,我沒有錯誤,沒有警告等。當我跑了它,它回到了它應該在腳本刪除位置後的屏幕上跑。只有在查詢運行後纔會來到這裏。

    好吧,什麼都沒有發生,經過3個小時的嘗試廢話後,我來到你們的B/C我在我的最後一站。我仍然沒有嚴重錯誤,沒有任何事情使它失敗:這是我的代碼。

    <?php 
    //In the include file is the connection to the db 
    include("library/new_library.php"); 
    
    //Below is the session id, gets their position id from the DB, than grabs whether or not they can edit the company 
    $user_id = $_SESSION['user_id']; 
    $sql = "SELECT ID, PositionID FROM users WHERE ID = '$user_id'"; 
    $query = mysql_query($sql); 
    while($row = mysql_fetch_assoc($query)) 
    { 
        $position = $row['PositionID']; 
    } 
    $sql = "SELECT * FROM tblCPositions WHERE PositionID = '$position'"; 
    $query = mysql_query($sql); 
    while($row = mysql_fetch_assoc($query)) 
    { 
        $editCompany = $row['Edit_Company']; 
    } 
    
    
    //Next I check for position edit and if they try to put in the position id of a position the company does not control it gives them a "nice" message. 
    $company = $_SESSION['company']; 
    if($_GET['pidedit']){ 
        $position = $_GET['pidedit']; 
        $sql = "SELECT * FROM tblCPositions WHERE PositionID = '$position'"; 
        $query = mysql_query($sql); 
        while($row = mysql_fetch_assoc($query)) 
        { 
         if($row['CompanyID'] != $company) 
         { 
          $warning = "<div class='warning'>You are trying to edit a position that does not belong to your company. DO NOT TRY TO CHEAT THE SYSTEM!</div>"; 
         } 
         else 
         { 
          $positionArray[] = array(ID => $row['PositionID'], name => $row['Name'], hire => $row['Hire'], fire => $row['Fire'], bid => $row['Contract'], edit => $row['Edit_Company'], finances => $row['Finances']); 
         } 
        } 
    } 
    
    //Here I check for $_GET delete 
    elseif($_GET['piddelete']) 
    { 
        $position = $_GET['piddelete']; 
        $sql = "SELECT * FROM tblCPositions WHERE PositionID = '$position'"; 
        $query = mysql_query($sql); 
        while($row = mysql_fetch_assoc($query)) 
        { 
         if($row['CompanyID'] != $company) 
         { 
          $warning = "<div class='warning'>You are trying to delete a position that does not belong to your company. DO NOT TRY TO CHEAT THE SYSTEM!</div>"; 
         } 
        } 
    } 
    else 
    { 
        $sql = "SELECT * FROM tblCPositions WHERE CompanyID = '$company'"; 
        $query = mysql_query($sql); 
        $number = mysql_num_rows($query); 
        $numberLeft = 12 - $number; 
        while($row = mysql_fetch_assoc($query)) 
        { 
         $positionArray[] = array(ID => $row['PositionID'], name => $row['Name'], hire => $row['Hire'], fire => $row['Fire'], bid => $row['Contract'], edit => $row['Edit_Company'], finances => $row['Finances']); 
        } 
    } 
    
    // 
    if($_POST['submitNewPosition']) 
    { 
        $name = $_POST['positionName']; 
        $hire = $_POST['hire']; 
        $fire = $_POST['fire']; 
        $bid = $_POST['bid']; 
        $edit = $_POST['edit']; 
        $finances = $_POST['finances']; 
        $cid = $_SESSION['company']; 
        $sql = "INSERT INTO tblCPositions(CompanyID, Name, Hire, Fire, Contract, Edit_Company, Finances) VALUES ('$cid','$name','$hire','$fire','$bid','$edit','$finances')"; 
        $query = mysql_query($sql); 
        if($query) 
        { 
         header("location: view_company.php?newp=success"); 
        } 
    } 
    
    //Haven't finished this section yet 
    if($_POST['submitEditPosition']) 
    { 
        $name = $_POST['positionName']; 
        $fire = $_POST['hire']; 
        $fire = $_POST['fire']; 
        $bid = $_POST['bid']; 
        $edit = $_POST['edit']; 
        $finances = $_POST['finances']; 
    } 
    
    //This this is my problem area, this is where it says its running the query but its not. 
    if(isset($_POST['deletePosition'])) 
    { 
        $deleteID = $_GET['piddelete']; 
        $deleteSql = "DELETE FROM tblCPositions WHERE PositionID = '$deleteID'"; 
        $deleteQuery = mysql_query($deleteSql); 
        if($deleteQuery) 
        { 
         header("location: view_company.php?delete=success"); 
        } 
        if(!$deleteQuery) 
        { 
         header("location: view_company.php?delete=failure"); 
        } 
    } 
    

    UPDATE -

    好了,所以我得到它的工作的問題是什麼我忘了,這種形式只是意味着是一個「是或否的形式」,所以我在做後期才交提交按鈕,窗體上沒有其他東西。我忘記了action =「file.php」(我有)忘記傳遞get變量,所以一旦我改變它的行動=「file.php?piddelete = 12」它的工作。

    感謝大家的幫助,我真的很感激它。

    +3

    必須說非常有趣的標題:) – codaddict 2010-12-16 04:32:00

    +1

    呃,你有檢查溼地error_reporting = E_ALL&〜E_NOTICE&〜E_DEPRECATED設置? – lock 2010-12-16 04:34:13

    +0

    是的,但我想不出任何其他方式來解釋它,我會寫回錯誤報告。 – jefffan24 2010-12-16 04:38:36

    回答

    2

    10對1您的變量$_GET['piddelete'];爲空。當你這樣做時,你會得到什麼:

    var_dump($_GET['piddelete']); 
    

    禁用標題重定向,以便您可以看到輸出。

    編輯

    或者,正如尼克指出的那樣,你可以添加die()陳述你的查詢:

    $deleteQuery = mysql_query($deleteSql) or die(mysql_error()); 
    

    如果查詢仍在運行,在腳本不會死,和位置爲仍然未刪除,則應檢查查詢,可能會成功刪除0行。嘗試在die($deleteSql);處查殺並通過MySQL的控制檯運行查詢。

    /編輯

    此外,我不得不給你介紹我的好朋友SQL injection attack。在將它們交給MySQL服務器之前,您應該過濾所有包含在$_POST$_GET超全局星線中的數據。使用mysql_real_escape_string()

    嘗試神交此:

    whatever.com/your_url.php?pidedit=x'%3B%20DROP%20TABLE%20tblCPositions%3B%20-- 
    

    如果我執行你的應用程序,查詢字符串,你tblCPositions表將被丟棄。

    +0

    只需檢查並設置它,在它的url設置,我可以在按鈕要刪除頁面的頁面上回顯$ _GET變量。 – jefffan24 2010-12-16 04:48:16

    +0

    那個錯誤報告怎麼樣?另外,您的上述腳本與刪除按鈕位於同一頁面上嗎?如果沒有,(我猜這不是因爲你正在做重定向),你需要檢查GET變量是否設置在* that *腳本上,而不是前面的那個。禁用標題重定向,在上面的腳本中回顯'GET'變量,然後單擊按鈕。 – Stephen 2010-12-16 04:48:57

    +0

    是的,我試圖讓沒有它的東西先工作,然後我添加mysql_real_escape_string ...我知道它不正常,但它對我來說大部分時間:) 嗯,實際上得到不是來自窗體。它來自我輸入的文本,所以它是在同一頁面上,但它不是來自表格。 – jefffan24 2010-12-16 04:53:13