0

I am working on a Content Management System. This particular page will be a page that the content manager can use to rate the performance of content and should have 5 radio buttons that allow the CM to rate the content 1-5 and a comment box that allows the CM to insert comments into the table. The page is linked to an index page that will GET the ID of the particular content to be reviewed. The radio buttons are not showing up and many errors are showing up such as " undefined index" of three variables- blogpostID, comment, and opt radio. Can anyone help me with this code? I first need to fix the radio buttons because they are not showing up. Then I need to correctly connect the data to MySQL so the rating and comment can be posted into the DB and so it knows to GET the ID of the blog post because that is the PK.

<!DOCTYPE html>
<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" integrity="sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M" crossorigin="anonymous">

    <link rel="stylesheet" type="text/css" href="css/style.css">


<!-- Navbar Code -->
<?php  
  require_once('PHP_functions/CM_navbar.php');
?>

<!-- End of Navbar -->

<div class="center spacer">
  <h3 id = "main-title"> Rate Blog </h3>
</div>

<form action ='' method = "POST">
<div class="container">
  <div class="row">
    <div class="form-group">
      <label for="radio-inline-group">Rating:</label>
        <label class="radio-inline"><input type="radio" name="optradio" value="1">1</label>
        <label class="radio-inline"><input type="radio" name="optradio" value="2">2</label>
        <label class="radio-inline"><input type="radio" name="optradio" value="3">3</label>
        <label class="radio-inline"><input type="radio" name="optradio" value="4">4</label>
        <label class="radio-inline"><input type="radio" name="optradio" value= "5">5</label>
    </div>
  </div>

  <div class="row">
    <div class="form-group">
      <label for="comment">Comment:</label>
      <textarea class="form-control" rows="5" id = "comment" name="comment"></textarea>
    </div>
  </div>

  <div class="row">
      <button type="button" class="btn btn-warning">Request Revision</button>
      <button type="button" class="btn btn-success">Approve</button>
  </div>
</div>

<?php
$name_of_table = "Blog";
$id = $_GET['blogID'];
$rating = $_POST['optradio'];
$managerComments = $_POST['comment'];

$query = "UPDATE" .$name_of_table. "SET rating = :optradio, managerComments = :comment WHERE blogID = :blogID";
$TableInfo = getRows($query); 

foreach($TableInfo as $data)
    {
      echo '
      <tr>
        <th>'.$data['ID'].'</th>
        <td>'.$data['title'].'</td>
        <td>'.$data['client'].'</td>
        <td>'.$data['freelancer'].'</td>
        <td>'.$data['Draft Due'].'</td>
        <td>'.$data['Draft Received'].'</td>
        <td>'.$data['onTime'].'</td>
        <td><a href="./CM_reviewBlog.php?id='.$data['ID'].'">Rate</a></td>
      </tr>';
    }

?>
</form>

</body>
</html>



    <!-- Optional JavaScript -->
    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.11.0/umd/popper.min.js" integrity="sha384-b/U6ypiBEHpOf/4+1nzFpr53nxSS+GLCkfwBdFNTxtclqqenISfwAzpKaMNFNmj4" crossorigin="anonymous"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/js/bootstrap.min.js" integrity="sha384-h0AbiXch4ZDo7tp9hKZ4TsHbi047NrKGLO3SEJAg45jXxnGIfYzk4Si90RDIqNm1" crossorigin="anonymous"></script>
  </body>
</html>
Sam Thach
  • 27
  • 3
  • *Can anyone help me with this code? I first need to fix the radio buttons because they are not showing up.* - you first need to show the exact error message and take a look at the exact code that throws it. As a hint: Your `$query` has 3 placeholders and you're passing that string to `getRows()` - that function likely tries to bind those parameters (unsuccessfully), but it's not shown here so that's a guess at best. – ccKep Nov 26 '17 at 05:05
  • 1
    your query has missed some spaces `"UPDATE " .$name_of_table. " SET ..."` after `UPDATE` and before `SET` – Shankar Nov 26 '17 at 05:09

1 Answers1

0

Currently your SQL statement is executed each time the page is visited. However on your first visit it shouldn't be executed since the user hasn't set the values yet. This is also the reason why you get "undefined index" errors because the $_POST array is empty, no form has been sent. Also, it doesn't make sense to use getRows() (whatever it does, it's not a php build in function) on an UPDATE query.

To fix your problem you have to executed the UPDATE query only when there is actually an HTML form sent by the user. You can do this with isset() to see if the $_POST array is filled with the values you need.

if (isset($_POST['optradio'], $_POST['comment'])) {
    // run UPDATE query here
}
// normal SELECT query hiere

Additionally, when you want to keep using $_GET['blogID'] in your code then your HTML form must send this information as well. To do so you specify the URI you want to call in the action="" attribute of the <form> tag. It should look something like this:

<form action="yourPage.php?blogID=<?php echo (int)$_GET['blogId']; ?>" method="post">
    ...
</form>

This way the blogID value is still in $_GET but the form values are in $_POST.

Progman
  • 16,827
  • 6
  • 33
  • 48