add news problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
muskan
Forum Newbie
Posts: 3
Joined: Fri Jun 05, 2009 3:44 am

add news problem

Post by muskan »

i have a problem. when i leave blank the title box then it gives an error "Error: News title is a required field. Please fill it." againast title box.its right. now i want that when i leave blank text 1 or text 2 it also gives error that "Error: News text is a required field. Please fill it."
what changes should i do in this code to make this as i want any one help me please its urgent.
following is the code:

Code: Select all

<title>hotnews</title>
<?php
 
//LAST UPDATE
// 27-09-2007
 
include("config.php");
$error = "";
 
  if(isset($_POST['submit']))
  {//begin of if($submit).
      // Set global variables to easier names
      $title = $_POST['title'];
      $text1 = $_POST['text1'];
      $text2 = $_POST['text2'];
 
              //check if (title) field is empty then print error message.
              if(!$title){  //this means If the title is really empty.
                     $error = "Error: News title is a required field. Please fill it.";
                     exit(); //exit the script and don't do anything else.
              }// end of if
 
         //run the query which adds the data gathered from the form into the database
         $result = mysql_query("INSERT INTO news (title, dtime, text1, text2)
                       VALUES ('$title',NOW(),'$text1','$text2')",$connect);
          //print success message.
          echo "<b>Thank you! News added Successfully!<br>You'll be redirected to Home Page after (4) Seconds";
          echo "<meta http-equiv=Refresh content=4;url=index.php>";
                  $done = "TRUE";
  }//end of if($submit).
 
 
  // If the form has not been submitted, display it!
if($done != "TRUE")
  {//begin of else
 
      ?>
      <br>
      <h3>::Add News</h3>
 
      <form method="post" action="<?php echo $_SERVER[PHP_SELF] ?>">
 
      Title: <input name="title" size="40" maxlength="255"><?php echo $error ?>
      <br>
      Text1: <textarea name="text1"  rows="7" cols="30"></textarea>
      <br>
      Text2: <textarea name="text2" rows="7" cols="30"></textarea>
      <br>
      <input type="submit" name="submit" value="Add News">
      </form>
      <?
  }//end of else
  
  
?>
 

please help me its urgent
Last edited by Benjamin on Fri Jun 05, 2009 11:27 am, edited 1 time in total.
Reason: Changed code type from text to php.
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: add news problem

Post by Darhazer »

Code: Select all

 
if(!$title){  //this means If the title is really empty.
                     $error = "Error: News title is a required field. Please fill it.";
                     exit(); //exit the script and don't do anything else.
              }
 
This is the code that generates the error. Obviously you have to duplicate it for the other fields:

Code: Select all

 
if(!$text1){  //this means If the title is really empty.
                     $error = "Error: News text is a required field. Please fill it.";
                     exit(); //exit the script and don't do anything else.
              }
 
User avatar
mikemike
Forum Contributor
Posts: 355
Joined: Sun May 24, 2009 5:37 pm
Location: Chester, UK

Re: add news problem

Post by mikemike »

The exit()'s can't be doing much use here. You're setting a variable then exiting straight away so you never use it...
muskan
Forum Newbie
Posts: 3
Joined: Fri Jun 05, 2009 3:44 am

Re: add news problem

Post by muskan »

i write the following code. but when i add text in text one and in title then it given an error thaat text2 is empty please fill it. but when i enter anything only in text2 it cannot give any error and save it successfully. why whts wrong in my code.
the code is here:

Code: Select all

 
 
<title>hotnews</title>
<?php
 
//LAST UPDATE
// 27-09-2007
 
include("config.php");
 
$error = "";
$error1 = "";
$error2 = "";
 
  if(isset($_POST['submit']))
  {//begin of if($submit).
      // Set global variables to easier names
      $title = $_POST['title'];
      $text1 = $_POST['text1'];
      $text2 = $_POST['text2'];
 
              //check if (title) field is empty then print error message.
              if(!$title){  //this means If the title is really empty.
                     $error = "Error: News title is a required field. Please fill it.";
       
               }// end of if
              
 
              if(!$text1){  //this means If the title is really empty.
                     $error1= "Error: News text1 is a required field. Please fill it.";
            
                }
               if(!$text2){  //this means If the title is really empty.
                     $error2= "Error: News text2 is a required field. Please fill it.";
               
               }      
     
 else 
      {
         //run the query which adds the data gathered from the form into the database
         $result = mysql_query("INSERT INTO news (title, dtime, text1, text2)
                       VALUES ('$title',NOW(),'$text1','$text2')",$connect);
          //print success message.
          echo "<b>Thank you! News added Successfully!<br>You'll be redirected to Home Page after (4) Seconds";
          echo "<meta http-equiv=Refresh content=4;url=index.php>";
                  $done = "TRUE";
      }
  }//end of if($submit).
 
 
  // If the form has not been submitted, display it!
if($done != "TRUE")
  {//begin of else
 
      ?>
      <br>
      <h3>::Add News</h3>
 
      <form method="post" action="<?php echo $_SERVER[PHP_SELF] ?>">
 
      Title: <input name="title" size="40" maxlength="255"><?php echo $error ?>
      <br>
      Text1: <textarea name="text1"  rows="7"cols="30"></textarea><?php echo $error1 ?>
      <br>
      Text2: <textarea name="text2" rows="7" cols="30"></textarea><?php echo $error2 ?>
      <br>
      <input type="submit" name="submit" value="Add News">
      </form>
      <?
  }//end of else
  
  
?>
 
 
 
Last edited by Benjamin on Sun Jun 07, 2009 11:28 pm, edited 1 time in total.
Reason: Changed code type from text to php.
Post Reply