Jump to content

Please Fix The Errors In The Cod

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
7 replies to this topic

#1
muskan

muskan

    Newbie

  • Members
  • Pip
  • 5 posts
following is a code of edit_news.php

<title>Edit News</title>


<?php


include("config.php");


$error =  "";

$error1 = "";

$error2 = "";

$error3 = "";

$error4 = "";

$error5 = "";


   $newsid = $_GET['newsid'];



   if(isset($_POST['submit']))

  {


      $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

              elseif ((bool) preg_match('/[0-9]/', $title)) {

                 $error3 = "Error: News title must not contain numbers. Please refill 


it, with all numbers removed.";

                 

               }


              elseif(!$text1){  //this means If the title is really empty.

                     $error1= "Error: News text1 is a required field. Please fill it.";

            

                }

              elseif ((bool) preg_match('/[0-9]/', $text1)) {

                 $error4 = "Error: News title must not contain numbers. Please refill 


it, with all numbers removed.";

                 

               } 


              elseif(!$text2){  //this means If the title is really empty.

                     $error2= "Error: News text2 is a required field. Please fill it.";

               

               }      

     elseif ((bool) preg_match('/[0-9]/', $text2)) {

                 $error5 = "Error: News title must not contain numbers. Please refill 

it with all numbers removed.";


         $result = mysql_query("UPDATE news SET title='$title', text1='$text1', 


text2='$text2' ",$connect);


          echo "<b>Thank you! News UPDATED Successfully!<br>You'll be redirected to Home 


Page after (4) Seconds";

          echo "<meta http-equiv=Refresh content=4;url=index.php>";


}


     else          

if($newsid)

{

        $result = mysql_query("SELECT * FROM news WHERE newsid='$newsid' ",$connect);

        while($myrow = mysql_fetch_assoc($result))

             {

                $title = $myrow["title"];

                $text1 = $myrow["text1"];

                $text2= $myrow["text2"];

}

?>

<br>

<h3>::Edit News</h3>


<form method="post" action="<?php echo $PHP_SELF ?>">

<input type="hidden" name="newsid" value="<? echo $myrow['newsid']?>">


Title: <input name="title" size="40" maxlength="255" value="<? echo $title; ?>"><?php 


echo $error ?><?php echo $error3 ?>

<br>

Text1: <textarea name="text1"  rows="7" cols="30"><? echo $text1; ?></textarea><?php 


echo $error1 ?><?php echo $error4 ?>

<br>

Text2: <textarea name="text2" rows="7" cols="30"><? echo $text2; ?></textarea> <?php 


echo $error2 ?><?php echo $error5 ?>

<br>

<input type="submit" name="submit" value="Update News">

</form>

<?

              }//end of while loop


  


  }//end else


?>


I want to display error same as display in add_news.php file i.e

if the title field is empty error will displayed
if the text1 field is empty error will displayed
if the text2 field is empty error will displayed

if numaric data is add in the title filed error will displayed
if numaric data is add in the text1 filed error will displayed
if numaric data is add in the text2 filed error will displayed

for example see add_news.php code.
<title>hotnews</title>

<?php


include("config.php");


$error = "";

$error1 = "";

$error2 = "";

$error3 = "";

$error4 = "";

$error5 = "";


  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

              elseif ((bool) preg_match('/[0-9]/', $title)) {

                 $error3 = "Error: News title must not contain numbers. Please refill it, with all numbers removed.";

                 

               }


              elseif(!$text1){  //this means If the title is really empty.

                     $error1= "Error: News text1 is a required field. Please fill it.";

            

                }

              elseif ((bool) preg_match('/[0-9]/', $text1)) {

                 $error4 = "Error: News title must not contain numbers. Please refill it, with all numbers removed.";

                 

               } 


              elseif(!$text2){  //this means If the title is really empty.

                     $error2= "Error: News text2 is a required field. Please fill it.";

               

               }      

     elseif ((bool) preg_match('/[0-9]/', $text2)) {

                 $error5 = "Error: News title must not contain numbers. Please refill it, with all numbers removed.";

                 

               }

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 ?> <?php echo $error3 ?>

      <br>

      Text1: <textarea name="text1"  rows="7"cols="30"></textarea><?php echo $error1 ?> <?php echo $error4 ?>

      <br>

      Text2: <textarea name="text2" rows="7" cols="30"></textarea><?php echo $error2 ?> <?php echo $error5 ?>

      <br>

      <input type="submit" name="submit" value="Add News">

      </form>

      <?

  }//end of else

  

  

?>


#2
TcM

TcM

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 11,147 posts
So what is the problem? the errors are not showing?

#3
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
Did you read an consider my suggestions in your last thread about this?

http://forum.codecal...html#post157985
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#4
muskan

muskan

    Newbie

  • Members
  • Pip
  • 5 posts
yes the errors are not showing.

#5
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
the use of elseif will make your system only show one error at a time, is that what you want?
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#6
muskan

muskan

    Newbie

  • Members
  • Pip
  • 5 posts
the given code cannot display error

#7
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
if we can't discuss what it is you want, I can't help you
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#8
muskan

muskan

    Newbie

  • Members
  • Pip
  • 5 posts
i just want that as in the add_news.php file the errors display same errors display in the edit_news.php file. i donot know how to do this.please tell me