2

I am writing code to update a database and the checkbox to select whether a entry is active is having a few problems. It does correctly update something to being active when clicked, but because when the page is loaded it is unchecked even though it is active you cannot make it inactive unless you go directly to the database and do this. I am a little lost on what to do, here is the code for the checkbox/active. Also as a side note it is displaying other correct data being pulled from the database just not the checkbox

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

And i know that it isn't visible because it isn't the entire code but these are contained in php tags i.e. .

Whole code:

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


?>
  • Can you post your HTML code as well ? Anything else that you might have for your application? Are there any errors that you might be getting ? – Maximus2012 Sep 08 '15 at 21:02
  • Also, make sure that you have error reporting turned on for your code: http://stackoverflow.com/questions/845021/how-to-get-useful-error-messages-in-php – Maximus2012 Sep 08 '15 at 21:03
  • Additionally, you might need to explicitly check for the `checked` attribute value of your checkbox: http://stackoverflow.com/questions/7851868/whats-the-proper-value-for-a-checked-attribute-of-an-html-checkbox – Maximus2012 Sep 08 '15 at 21:04
  • post the full code. try print_r($row) and see whether the returned data is what you are expecting? are you using check box to insert that same data? – aimme Sep 08 '15 at 21:05
  • Can you also post the code where you are actually executing your update query `$end_string` ? – Maximus2012 Sep 08 '15 at 21:08
  • I don't even understand why you have two queries ?!?! – Strawberry Sep 08 '15 at 21:12
  • 1
    Advice: use PDO or mysqli instead of mysql. your code is vulnerable to sql injection – aimme Sep 08 '15 at 21:17
  • Are you sure the control is going to this part of the code: `if($row["active"] != $active) {` ? – Maximus2012 Sep 08 '15 at 21:19
  • use checked attribute in checkbox to make it checked, are you using active=1 or something like that? i don't see the checkbox?? – aimme Sep 08 '15 at 21:21
  • i am using the active=1 i believe, the database isn't returning an error so i am fairly sure it has to do with pulling the data from the database – Megan Travis Sep 08 '15 at 21:38

1 Answers1

0

You said "because when the page is loaded it is unchecked even though it is active [in the database]". This leads me to believe that the problem isn't in updating the database, it's in either retrieving the data from the database or displaying the input in the HTML.

The retrieval should look something like:

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

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

Then displaying the input is like:

<input type="checkbox" name="active" <?php echo $active ? "checked" : ""; ?> />
DACrosby
  • 11,116
  • 3
  • 39
  • 51
  • Okay i think the problem has to do with pulling the data here is the code that i edited from your post for the input type `
  • />
  • ` i will continue to work with what you gave you as i just need to adapt it to what i already have and hopefully it will work thanks – Megan Travis Sep 08 '15 at 22:00
  • UPDATE: it is displaying the checkbox now for the ones that are active, but won't update the record when you hit to uncheck it – Megan Travis Sep 08 '15 at 22:27