2015-09-08 51 views
2

我正在編寫更新數據庫的代碼,並且選中某個條目是否處於活動狀態的複選框存在一些問題。它可以正確地更新某些內容以便在單擊時處於活動狀態,但是因爲當頁面加載時,即使它處於活動狀態,也不會使其處於非活動狀態,除非直接進入數據庫並執行此操作。我有點迷路了,這裏是checkbox/active的代碼。另外,作爲一個方面說明它會顯示其他正確的數據從數據庫只是沒有複選框複選框始終顯示錯誤值(未選中)

$result = mysql_query("SELECT * FROM Words where Title= '$Title'"); 
    $row = mysql_fetch_array($result); 

    $qry_string = "UPDATE Words SET"; 
    if($row['Title'] != $Title) 
    { 
    $end_string .= " Title = '$Title'"; 
    } 


    $_REQUEST["active"] == "on"?$active=1:$active=0; 

    if($row["active"] != $active) 
    { 
    if(isset($end_string)) $end_string .= ", "; 
     $end_string .= "active = " . $active; 
    } 

被拉而且​​我知道它是不可見的,因爲它不是整個代碼,但這些都包含在PHP標籤即。

整個代碼:

</div> 

    <div id="main-content" style="padding-bottom: 30000px;margin-bottom: -30000px;" class="col-2-3"> 
      <div class="wrap-col"> 
      <div align="center"><h2>Amazing Health Advances Content Management Systems <br/><br/>Update Words</h2></div> 
      <?php 
$ID = mysql_escape_string($_POST['ID']); 
$Title = mysql_escape_string($_POST['Title']); 
$Author = mysql_escape_string($_POST['Author']); 
$Display_Date = mysql_escape_string($_POST['Display_Date']); 
$Word = mysql_escape_string($_POST['Word']); 
$Date_Created = mysql_escape_string($_POST['Date_Created']); 
$Email = mysql_escape_string($_POST['Email']); 
$From = mysql_escape_string($_POST['From']); 
$image = mysql_escape_string($_POST['image']); 
$URL = mysql_escape_string($_POST['URL']); 
$category = mysql_escape_string($_POST['category']); 
$active = mysql_escape_string($_REQUEST['active']); 

    if(isset($_REQUEST["delete"])) { 
    mysql_query("DELETE FROM Words WHERE Title= '$Title'"); 
    echo mysql_error(); 
    echo 'This Article has been successfully removed from the database. <br><br>'; 
    echo '<a href=index.html>Return to the Articles Page</a><p>'; 
    echo '<a href=art_list.php>Return to the Articles List</a>'; 
    exit; 
    } 

    if($_REQUEST["Title"] == "") 
    { 
    $EString = 'You must include a Title.'; 
    } 

    if($_REQUEST["Author"] == "") 
    { 
    $EString .= '<br>You must include an Author.'; 
    } 

    if($_REQUEST["Word"] == "") 
    { 
    $EString .= '<Br>You must include an Article'; 
    } 

    if(isset($EString)) 
    { 
    echo $EString; 
    echo '<br><br>Please use your browsers back button to correct the above errors.'; 
    exit; 
    } 


    $result = mysql_query("SELECT * FROM Words where Title= '$Title'"); 
$row = mysql_fetch_array($result); 

$qry_string = "UPDATE Words SET"; 
    if($row['Title'] != $Title) 
    { 
    $end_string .= " Title = '$Title'"; 
    } 

    if($row['Author'] != $Author) 
    { 
    if(isset($end_string)) 
    { 
     $end_string .= ", Author = '$Author'"; 
    } 
    else 
    { 
     $end_string = " Author = '$Author'"; 
    } 
    } 

    if($row['Display_Date'] != $Display_Date) 
    { 
    if(isset($end_string)) 
    { 
     $end_string .= ", Display_date = '$Display_Date'"; 
    } 
    else 
    { 
     $end_string = " Display_date = '$Display_Date'"; 
    } 
    } 

if($row['Word'] != $Word) 
    { 
    echo 'Alter Word<br>'; 
    if(isset($end_string)) 
    { 
     $end_string .= ", Word = '$Word'"; 
    } 
    else 
    { 
     $end_string = " Word = '$Word'"; 
    } 
    } 

    if($row['Date_Created'] != $Date_Created) 
    { 
    if(isset($end_string)) 
    { 
     $end_string .= ", Date_Created = '$Date_Created'"; 
    } 
    else 
    { 
     $end_string = " Date_Created = '$Date_Created'"; 
    } 
    } 

    if($row['Email'] != $Email) 
    { 
    if(isset($end_string)) 
    { 
     $end_string .= ", Email = '$Email'"; 
    } 
    else 
    { 
     $end_string = " Email = '$Email'"; 
    } 
    } 

    if($row['Source'] != $From) 
    { 
    if(isset($end_string)) 
    { 
     $end_string .= ", Source = '$From'"; 
    } 
    else 
    { 
     $end_string = " Source = '$From'"; 
    } 
    } 

    if($row['URL'] != $URL) 
    { 
    if(isset($end_string)) 
    { 
     $end_string .= ", URL = '$URL'"; 
    } 
    else 
    { 
     $end_string = " URL = '$URL'"; 
    } 
    } 

if($row['image'] != $image) 
    { 
    if(isset($end_string)) 
    { 
     $end_string .= ", image = '$image'"; 
    } 
    else 
    { 
     $end_string = " image = '$image'"; 
    } 
    } 


    if($row['category'] != $category) 
    { 
    if(isset($end_string)) 
    { 
     $end_string .= ", category = '$category'"; 
    } 
    else 
    { 
     $end_string = " category = '$category'"; 
    } 
    } 


    $_REQUEST["active"] == "on"?$active=1:$active=0; 

    if($row["active"] != $active) 
    { 
    if(isset($end_string)) $end_string .= ", "; 
     $end_string .= "active = " . $active; 
    } 

    if(!isset($end_string)) 
    { 
    echo 'There was no information to be updated.<br><br>'; 
    echo '<a href=index.html>Return to the Articles Page'; 
    exit; 
    } 

    $qry_string = $qry_string.$end_string." WHERE Title = '$Title'"; 


    if(mysql_query($qry_string) == FALSE) 
    echo mysql_error(); 
    echo '<a href=index.html>Return to the Articles Page</a><p>'; 
    echo '<a href=art_list.php>Return to the Articles List</a>'; 
    { 
    echo 'There was an error attempting to update the database.<br>'; 
    echo 'Please contact the system administrator with the following information:'; 
    echo "<br><br>Query String -> $qry_string"; 
    echo "<br/><br/>mysql_error();"; 
    echo '<a href=index.html>Return to the Articles Page</a><p>'; 
    echo '<a href=art_list.php>Return to the Articles List</a>'; 
    exit; 
    } 

    echo 'The database has been successfully updated<br><br>'; 
    echo '<a href=index.html>Return to the Articles Page</a><P>'; 
    echo '<a href=art_list.php>Return to the Articles List</a>'; 


?> 
+0

您可以發佈您的HTML代碼呢?還有什麼你可能有你的應用程序?是否有任何錯誤,你可能會得到? – Maximus2012

+0

此外,請確保您的代碼已打開錯誤報告:http://stackoverflow.com/questions/845021/how-to-get-useful-error-messages-in-php – Maximus2012

+0

此外,您可能需要明確檢查複選框的checked屬性值:http://stackoverflow.com/questions/7851868/whats-the-proper-value-for-a-checked-attribute-of-an-html-checkbox – Maximus2012

回答

0

你說"because when the page is loaded it is unchecked even though it is active [in the database]"。這使我相信問題不在於更新數據庫,而是從數據庫檢索數據或在HTML中顯示輸入。

檢索應該是這個樣子:

$result = mysql_query("SELECT * FROM Words where ..."); 
$row = mysql_fetch_array($result); 

$active = $row["active"] ? 1 : 0; 

然後顯示輸入是這樣的:

<input type="checkbox" name="active" <?php echo $active ? "checked" : ""; ?> /> 
+0

好吧,我認爲問題與拉取數據有關從我的帖子編輯輸入類型爲'

  • />
  • '我會繼續使用你給我的東西,因爲我只是需要以適應它我已經有的,並希望它會工作,謝謝 –

    +0

    更新:它現在顯示覆選框爲活動的,但不會更新記錄,當你打不取消 –