Jump to content

Can't insert form data into database


Go to solution Solved by achilles1971,

Recommended Posts

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>

Link to comment
https://forums.phpfreaks.com/topic/275479-cant-insert-form-data-into-database/
Share on other sites

At the top where it says:

 

<?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);
  }
?>

 

 

I would try !$_GET['id'] in your first if statement. Turn on error reporting while you are still in the developing stage as well. 

This will work 100%

 

$title = $_POST[title];

$date  = $_POST[date];

$body  = $_POST[body];

 

$q = "INSERT INTO blog (title, date, body) VALUES ('$title', $date', '$body')";

 

except the array keys (title, date and body) should be in quotes and you should sanitize user-inputs with mysqli_real_escape_string before using in a query




$title = $_POST[title];

$date = $_POST[date];

$body = $_POST[body];

$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("real", $con);


$sql = "INSERT INTO blog (title, date, body) VALUES ('.$title.','. $date.', '.$body.')";

if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record added";

mysql_close($con)

Spraban9, why would anyone want a query that looks like this

 

INSERT INTO blog (title, date, body) VALUES ('.titledata.','. datedata.', '.bodydata.')

with periods added to the start and end of all the string values?

 

Not to mention the lack of any sanitization of inputs

Edited by Barand
  • Solution

Okay...here is the solution:

 

<?php ## Blog Manager?>

<?php
  if (isset($_POST['submitted']) == 1) {
   if ($_GET['id'] == '') {
    if ($_POST['title'] =='' || $_POST['body'] == '') {
      header('Location: index.php?page=blog');     
     } else {     
      $title = $_POST['title'];
      $date  = $_POST['date'];    
      $body  = $_POST['body'];
     
     $q = "INSERT INTO blog (title, date, body) VALUES ('$title', '$date', '$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>
 <?php  ?>

</div>

 

Two problems: 

 

1. The timestamp being inserted into the database is 0000-00-00 00:00:00

2. The body content is being inserted into the database with paragraph tags around it (I am using Redactor WYSIWYG for the body field).

 

What could be causing these two issues?

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.