Jump to content
hikingwithu2

I can't find the reason for this error, please help me find it.

Recommended Posts

I am writing a CRUD and this is a test page to combine 3 separate files into one page. If I comment out the final "else" section entirely the first "if" section and the "else if" section work fine, no errors. After I remove the comment marks for the final "else" section I get this error:

PHP Parse error:  syntax error, unexpected '?>' in /public_html/crud/test.php on line 139 (<- the line 139 the error points to is the final php closing tag before the closing body tag)

It should be obvious, simple, but I can't find any reason why it is throwing out that error and breaking the page (it is just a blank white page).

I'm sure somebody can scan through the code and see what I'm missing.

Thanks.

<!DOCTYPE html>
<html>
  <head>
    <?php
    require_once "../HeaderStuff.php";
    require_once "../connection.php";
    ?>
    <style>
    table { width: 100%; }
    th {background-color: #f1f1c1;}
    .margin {margin-top: 4em;}
    </style>
  </head>
  <body style="background-color:<?php echo $bgColor; ?>">
<?php
$choice = $_GET['choice'];
$id = $_GET['id'];
if ($choice == 'blogsEdit')
  {
?>
  <table class="w3-table-all">
    <thead>
      <tr>
        <th class="textBig w3-padding-32 w3-left-align">Website Administration - Edit Blog</th>
      </tr>
    </thead>
  </table>
  <?php
  $sql = "SELECT id, cat, Fdate, title, para1, story, pic, picAlt FROM blogs WHERE id = $id";
  $result = mysqli_query($link, $sql);
  if (mysqli_num_rows($result) > 0)
    { // output data of each row
    while($row = mysqli_fetch_assoc($result))
      { // Display each field of the records.
        $id=$row['id']; // the blog id
        $cat=$row['cat']; // the blog category
        $Fdate=$row['Fdate']; // the blog date
        $title=$row['title']; // short title of the blog
        $para1=$row['para1']; // the introduction paragraph
        $story=$row['story']; // the rest of the blog after the intro paragraph
        $pic=$row['pic']; // picture for the blog
        $picAlt=$row['picAlt']; // alt text for the picture
  ?>
        <fieldset>
          <form action="blogsUpdate.php" method="post">
            <table class="w3-table-all">
              <tr>
                <th>Category:</th>
                <td><?php echo $cat; ?></td>
              </tr>
              <tr>
                <th>Blog Date:</th>
                <td><input type="text" name="Fdate" value="<?php echo $Fdate; ?>" /></td>
              </tr>
              <tr>
                <th>Blog Title</th>
                <td><input type="text" name="title" size="150" value="<?php echo $title; ?>" /></td>
              </tr>
              <tr>
                <th>Intro Paragraph</th>
                <td><textarea name="para1" id="para1" cols="150" rows="5"><?php echo $para1; ?></textarea></td>
              </tr>
              <tr>
                <th>Story</th>
                <td><textarea name="story" id="story" cols="150" rows="10"><?php echo $story; ?></textarea></td>
              </tr>
              <tr>
                <th>Picture File Name</th>
                <td><input type="text" name="pic" size="50" value="<?php echo $pic; ?>" /></td>
              </tr>
              <tr>
                <th>Picture Alt Text</th>
                <td><input type="text" size="150" name="picAlt" value="<?php echo $picAlt; ?>" /></td>
              </tr>
              <tr>
                <input type="hidden" name="id" value="<?php echo $id; ?>" />
                <td colspan="2"><button type="submit">Save Changes</button></td>
              </tr>
            </table>
          </form>
        </fieldset>
  <?php
      }
    } 
    else
    {
      echo "0 results";
    }
  }
  else if ($choice == 'lessonsEdit')
  { 
  ?>
  <table class="w3-table-all">
    <thead>
      <tr>
        <th class="textBig w3-padding-32 w3-left-align">Website Administration - Edit Lesson</th>
      </tr> 
    </thead>
  </table>        
  <?php 
    $sql = "SELECT id, cat, Fname FROM blogs WHERE id = $id";
    $result = mysqli_query($link, $sql);
    if (mysqli_num_rows($result) > 0) 
      { // output data of each row
      while($row = mysqli_fetch_assoc($result)) 
        { // Display each field of the records.
        $id=$row['id']; // the lesson id
        $cat=$row['cat']; // the lesson category
        $Fname=$row['Fname']; // the file name
  ?>
        <fieldset>          
          <form action="lessonsUpdate.php" method="post">
            <table class="w3-table-all">
              <tr>
                <th>Category:</th> 
                <td><?php echo $cat; ?></td>
              </tr>             
              <tr>
                <th>File Name:</th>
                <td><input type="text" name="Fname" value="<?php echo $Fname; ?>" /></td>
              </tr>               
              <tr>
                <input type="hidden" name="id" value="<?php echo $id; ?>" />
                <td colspan="2"><button type="submit">Save Changes</button></td>
              </tr>
            </table>
          </form>
        </fieldset>
  <?php
        }
      } 
      else
      {
        echo "0 results";
      }
  }
  else ($choice == 'writsEdit')
  {
  ?> 
  <table class="w3-table-all">
    <thead>
      <tr>
        <th class="textBig w3-padding-32 w3-left-align">Website Administration - Edit Writing</th>
      </tr> 
    </thead>
  </table>        
  <?php 
    $sql = "SELECT id, cat, title, story FROM blogs WHERE id = $id";
    $result = mysqli_query($link, $sql);
    if (mysqli_num_rows($result) > 0) 
      { // output data of each row
      while($row = mysqli_fetch_assoc($result)) 
        { // Display each field of the records.
        $id=$row['id']; // the writing id
        $cat=$row['cat']; // the category
        $title=$row['title']; // title of the writing
        $story=$row['story']; // the poem or story
  ?>
        <fieldset>          
          <form action="writsUpdate.php" method="post">
            <table class="w3-table-all">
              <tr>
                <th>ID:</th>
                <td><?php echo $id; ?></td>
              </tr>
              <tr>
                <th>Category:</th>
              <td><?php echo $cat; ?></td>
              </tr>
              <tr>
                <th>Title</th>
                <td><input type="text" name="title" size="150" value="<?php echo $title; ?>" /></td>
              </tr>   
              <tr>
                <th>Story</th>
                <td><textarea name="story" id="story" cols="150" rows="10"><?php echo $story; ?></textarea></td>
              </tr>
              <tr>
                <input type="hidden" name="id" value="<?php echo $id; ?>" />
                <td colspan="2"><button type="submit">Save Changes</button></td>
              </tr>
            </table>
          </form>
        </fieldset>
  <?php
        }
      } 
      else 
      {
        echo "0 results";
      }
  }
  ?>
</body>
</html>

 

Edited by hikingwithu2

Share this post


Link to post
Share on other sites

This line is missing an "if"

else ($choice == 'writsEdit')

 

Share this post


Link to post
Share on other sites

Thanks, that does indeed fix it, but I don't understand why it is happening. After all, the form of the process is (from this W3 site) -

if (condition) {
    code to be executed if this condition is true;
} elseif (condition) {
    code to be executed if first condition is false and this condition is true;
} else {
    code to be executed if all conditions are false;
} 

Which is how my code was written, using the third condition as only "else".

Edited by hikingwithu2

Share this post


Link to post
Share on other sites

Oh, I just noticed something! The last "else" has no condition, is this the reason why my code didn't work? Because my final "else" had a condition? If true, then do I need to add a final "else" with no condition? (The page is working now without it.) But, what is the correct thing to do?

Share this post


Link to post
Share on other sites

If you need another condition, use else if(), if not then just use else. Neither of them are required, you just use them if you need them.

  • Thanks 1

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...