Quantcast
Channel: PHP Freaks: PHP Help
Viewing all articles
Browse latest Browse all 13200

Can't insert form data into database

$
0
0

I am working through a Create a CMS tutorial and I am on the very last step, but I am hung up.  My code is below.  It is supposed to pull up a blank form when you want to add a new blog post and it is supposed to populate the field when you want to edit a previous blog post.  That all works fine.  The edit a blog post functionality works as it should.  My problem is when I submit a new blog post, the data does not get sent to the database.  Any ideas where I may have gone wrong?

 

Aaron

 

 

<?php ## Blog Manager?>

<?php
  if (isset($_POST['submitted']) == 1) {
   if ($_GET['id'] == '') {
    $q = "INSERT INTO blog (title, date, body) VALUES ('$_POST[title]', '$_POST[date]', '$_POST[body]'";
    }else {    
    $q = "UPDATE blog SET title = '$_POST[title]', date = '$_POST[date]', body = '$_POST[body]' WHERE id = '$_POST[id]'";
    }           
   $r = mysqli_query($dbc, $q);
  }
?>
<h2>ATOM.CMS Blog Manager</h2>

<div class="col sidebar">

 <ul class="nav_side">
   
     <li><a href="?page=blog">+ Add Post</a></li>
     
     <?php
   
   $q = "SELECT * FROM blog ORDER BY date ASC";
   $r = mysqli_query($dbc, $q);
   
   if ($r) {
    
    while ($link = mysqli_fetch_assoc($r)) {
     
     echo '<li><a href="?page=blog&id='.$link['id'].'">'.$link['title'].'</a></li>';
     
     }
   }
  ?>   
     
    </ul>


</div>
<div class="col editor">
<h1> 
    <?php
   
  if (isset($_GET['id'])) {
  
   $q = "SELECT * FROM blog WHERE id = '$_GET[id]' LIMIT 1";
     
   $r = mysqli_query($dbc, $q);   
   $opened = mysqli_fetch_assoc($r); 
  
   echo 'Editing: '.$opened['title'];
   
   } else {
    
    echo 'Add a New Blog Post';
   
   }  
  ?>
</h1>

   
        <form action="?page=blog&id=<?php if (isset($_GET['id'])){echo $opened['id'];} ?>" method="post">
       
        <table class="gen_form">
       
            <tr>
                <td class="gen_label"><label>Blog title: </label></td>
                <td><input class="gen_input" type="text" size="30" name="title" value="<?php if (isset($_GET['id'])){echo $opened['title'];} ?>" /></td>       
            </tr>
         <tr>
          <td class="gen_label"><label>Blog date: </label></td>
          <td><input class="gen_input" type="text" size="30" name="date" value="<?php if (isset($_GET['id'])){echo $opened['date'];} ?>"/></td>       
          </tr>
            <tr>
             <td class="gen_label"></td>
            </tr>            
             <tr>
              <td colspan="2" class="gen_label"><label>Blog body: </label></td>
             </tr>
             <tr>
                <td colspan="2"><textarea id="page_body" name="body" cols="90" rows="24"><?php if (isset($_GET['id'])){echo $opened['body'];} ?></textarea></td>
             </tr>
             <tr>
                <td colspan="2"><input class="gen_submit"  type="submit" name="submit" value="Save Changes" /></td>
             </tr>
        
            <input type="hidden" name="submitted" value="1" />
            <input type="hidden" name="id" value="<?php if (isset($_GET['id'])){echo $opened['id'];} ?>" />
                 
        </table>
       
        </form>
 
</div>


Viewing all articles
Browse latest Browse all 13200

Trending Articles