Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8429947
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T05:28:33+00:00 2026-06-10T05:28:33+00:00

Im about to create a simple code for adding news…(small project of mine) When

  • 0

Im about to create a simple code for adding news…(small project of mine)
When I click submit i get a success text, and a rediret back to the page but there is no data added and if I leave the form empty-the error message does not appear.

…and is the mysql_real_escape_string gonna save me some trouble?

<?php 
include('connect_db.php');
if(isset($_POST['submit']))
{
    $title = mysql_real_escape_string(htmlspecialchars($_POST['title']));
    $text = mysql_real_escape_string(htmlspecialchars($_POST['text']));

    if ($title == '' || $text == '')
    {

         // generate error message
         $error = 'ERROR: Please fill in all required fields!';
    }

    $result = mysql_query("INSERT INTO news ('id', 'date', 'title', 'text')
                           VALUES ('NULL', NOW(),'$title','$text')",$conn);

    echo "<b>Thank you!<br>You'll be redirected in (4) secs...";
    echo "<meta http-equiv=Refresh content=4;url=add.php>";

    } else {

    echo "<form method='post' action='add.php'>
          <legend>Add news</legend>
          <label>Title</label>
          <input type='text' name='title'>
          <label>Text</label>
          <textarea rows='5' name='text'></textarea>
          <br />
          <button type='submit' name='submit' class='btn'>Submit</button>
          </form>";
}?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T05:28:34+00:00Added an answer on June 10, 2026 at 5:28 am

    Your code is a little unfinished.

    <?php 
    
    // connect to the database
    include('connect_db.php');
    
    
    if(isset($_POST['submit']))
    
    {
    
        // htmlspecialchars is needed when displaying HTML to the user from an input, not for inserting into a database. mysql_real_escape_string is plenty for this purpose.
    
        $title = mysql_real_escape_string($_POST['title']);
        $text = mysql_real_escape_string($_POST['text']);
    
        if ($title == '' || $text == '')
    
        {
    
            // You have generated an error but you are not displaying it anywhere.
    
            // generate error message
            echo 'ERROR: Please fill in all required fields!';
    
           // You will want to either send the error in a query string to this page again and display it above the form or re-echo the form here.
    
        }else{
    
            // Don't submit the data if there is an error.
    
            // ID should be auto-increment in your database, don't set it here even if you set it NULL, you can also have MySQL apply the current time rather than here.
    
            $result = mysql_query("INSERT INTO news (`title`, `text`)
            VALUES ('$title','$text')");
    
            echo "<b>Thank you!<br>You'll be redirected in (4) secs...";
            echo "<meta http-equiv=Refresh content=4;url=add.php>";
    
        }
    
    } else {
    
        echo "<form method='post' action='add.php'>
        <legend>Add news</legend>
        <label>Title</label>
        <input type='text' name='title'>
        <label>Text</label>
        <textarea rows='5' name='text'></textarea>
        <br />
        <button type='submit' name='submit' class='btn'>Submit</button>
        </form>";
    
    }
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a simple about page that uses JQuery click to
I am about to create a simple demo in flash where I have 3
I've created a very simple Enterprise Application project with about 7 entity beans and
I am about to create a light version of an app of mine. My
I'm trying to create a sample RPC program to learn more about it. What
About to create a form using Zend Form, all the form elements should have
I am about to create a ViewModel to pass some data to a View.
I was about to create a trim function in javascript, but as i don't
I'm about to create an API for a existing .NET application and want to
I'm trying to add an extension method to my MVC 2 project without success

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.