2015-09-08 48 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

    更新:它现在显示复选框为活动的,但不会更新记录,当你打不取消 –