Jump to content

PHP not INSERTing int the table


camdenite

Recommended Posts

Hi, I am completely lost as to why this is not creating new info on to the db. The script submits as if I don't fill out the form I get the errors. If I do I just get a blank page an it doesn't update the db... Nothing in the error log. I have been through this script but I am lost as to why it doesn't work and why there are no errors - wierd. Please help.

 

<?php

  require_once ('mysql_connect.php'); // Connect to the db.

 

// Check if the form has been submitted.

if (isset($_POST['submitted'])) {

 

  $errors = array(); // Initialise error array.

 

  // Check for a page name.

  if (empty($_POST['heading'])) {

      $errors[] = 'You forgot to enter a Blog Header.';

  } else {

      $header = escape_data($_POST['heading']);

  }

 

  // Check for a page title.

  if (empty($_POST['first_para'])) {

      $errors[] = 'You forgot to enter a First Paragraph.';

  } else {

      $first_para = escape_data($_POST['first_para']);

  }

 

  // Check for Page Content.

  if (empty($_POST['entry'])) {

      $errors[] = 'You forgot to enter an Entry.';

  } else {

      $entry = escape_data($_POST['entry']);

  }

 

  if (empty($errors)) { // If everything's OK.

 

      // Create the page in the database.

     

      /* Check for an existing page with the same name.

      $query = "SELECT blog_id FROM blogb WHERE heading='$heading'";

      $result = mysql_query($query);

      if (mysql_num_rows($result) == 0) {  */

     

        // Make the query.

        $query = "INSERT INTO blogb (blog_id, date1, date2, heading, category_id, first_para, entry, more, image1, image2, image3, image4, pub2 ) VALUES ('$blog_id', '$date1', '$date2', '$heading', '$category_id', '$first_para', '$entry', '$more', '$image1', '$image2', '$image3', '$image4', '$pub2')";

        $result = @mysql_query ($query); // Run the query.

        if ($result) { // If it ran OK.

       

            // Redirect the page_created.php page.

            // Start defining the URL.

            $url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']);

           

            // Check for a trailing slash.

            if ((substr($url, -1) == '/') OR (substr($url, -1) == '\\') ) {

              $url = substr ($url, 0, -1); // Chop off the slash.

            }

           

            // Add the page.

            $url .= '/blog_created.php';

           

            header("Location: $url");

            exit();

           

        } else { // If it did not run ok.

            $errors[] = 'The page could not be registered due to a system error. We appologise for any inconvenience.'; // Public message.

            $errors[] = mysql_error() . '<br /><br />Query: ' . $query; // Debugging message.

        }

       

      } else { // Page already exists.

        $errors[] = 'This page already exists.';

      }

     

  /* } // End of empty if (empty(errors)) IF. */

 

 

} else { // Form has not been submitted.

 

  $errors = NULL;

 

} // End of main submit conditional.

 

// Begin the page now.

$page_title = 'Blog Creator';

 

if (!empty($errors)) { // Print any error messages.

  echo '<h1>Error!</h1>

  <p>The following error(s) occurred:<br />';

  foreach ($errors as $msg) { // Print each error.

      echo " - $msg<br />\n";

  }

  echo '</p><p>Please try again.</p>';

}

 

// Create the form.

echo '<form action="create_blog.php" method="post">

<table width="500px">

<tr>

<td>Date: <b>'.$row['date2'].'</b><br /></td></tr>

<tr>

<td>Heading: <br />

<input type="text" name="heading" size="70"  value="'.$row['heading'].'" />

</td>

</tr>

<tr>

<td>Category Code<br />

(enter 2-digit code from list on right):<br />

<input type="text" name="category_id" size="5"  value="'.$row['category_id'].'" /></td></tr>

<tr>

<td valign="top">First Paragraph: <br />

<textarea name="first_para" cols="60" rows="6" />'.$row['first_para'].'</textarea></td>

</tr>

<tr>

<td valign="top">Entry: <br />

<textarea name="entry" cols="60" rows="10" />'.$row['entry'].'</textarea></td>

</tr>

<tr>

<td valign="top">More: <br />

<textarea name="more" cols="60" rows="10" />'.$row['more'].'</textarea></td>

</tr>

<tr>

<td>First Image: <br />

<input type="text" name="image1" size="70"  value="'.$row['image1'].'" />

</td>

</tr>

<tr>

<td>Second Image: <br />

<input type="text" name="image2" size="70"  value="'.$row['image2'].'" />

</td>

</tr>

<tr>

<td>Third Image: <br />

<input type="text" name="image3" size="70"  value="'.$row['image3'].'" />

</td>

</tr>

<tr>

<td>

 

</tr>

</tr>

<tr>

<td>

<input type="submit" name="submitted" value="Submit" style="background:#ff0 none;" />

<input type="hidden" name="submitted" value="TRUE" />

</td>

</tr>

</table>

</form>'

?>

Link to comment
Share on other sites

Also, remove the @ in front of the mysql_query() statement and any place else that you have them in your code to suppress errors (they also suppress the logging of errors to the error log.)

 

On a live server you should have the display_errors setting OFF to prevent the output of errors to the browser but they will still be logged (assuming that your logging settings are on.) Error_reporting should be set to E_ALL on both a live server and on a development system. On a learning/development system display_errors should be ON so that you get immediate feedback when an error occurs.

Link to comment
Share on other sites

Hi, Thanks for that. I got a few errors pointing to lines like:

 

<input type="text" name="heading" size="70"  value="'.$row['heading'].'" />

 

I don't think I have anything in the script fetching the records - while ($row = mysql_fetch_assoc($result)) { - but I don't know where to put it.

 

Cheers

Link to comment
Share on other sites

You need to define what the code is supposed to do.

 

The name of the page is apparently create_blog.php and the form processing code is INSERTING a record into the database. One would assume that the values="..." in the form fields would be empty or not present because the form is for creating a new entry, not editing an existing one.

Link to comment
Share on other sites

Cheers

The code is to update table for a blog. The problem seems to be defining the rows - which I just thought I don't need to do. I am posting rows, or trying to, with this:

 

<input type="text" name="category_id" size="5"  value="'.$row['category_id'].'" /></td></tr>

 

Which doesn't make any sense.

 

I have to do something like:

 

<input type="text" name="category_id" size="5"  value="'. if (isset($_POST['pname'])) echo $_POST['pname'] .'" /></td></tr>

 

Does that make sense?

Link to comment
Share on other sites

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.