复选框始终显示错误值(未选中)

时间:2015-09-08 21:01:15

标签: php mysql

我正在编写代码以更新数据库,并选中复选框以选择条目是否处于活动状态是否存在一些问题。它确实正确地将某些内容更新为单击时处于活动状态,但是因为当页面加载时,即使它处于活动状态,也会取消选中它,除非您直接转到数据库并执行此操作,否则无法使其处于非活动状态。我有点迷失方向,这里是复选框/活动的代码。另外作为旁注,它显示的是从数据库中提取的其他正确数据而不是复选框

$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>';


?>

1 个答案:

答案 0 :(得分: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" : ""; ?> />