Jump to content

Please Fix The Errors In The Code


laado

Recommended Posts

following is a code of edit_news.php

<title>Edit News</title><?php//LAST UPDATE// 27-09-2007include("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.eif the title field is empty error will displayedif the text1 field is empty error will displayedif the text2 field is empty error will displayedif numaric data is add in the title filed error will displayedif numaric data is add in the text1 filed error will displayedif numaric data is add in the text2 filed error will displayedfor example see add_news.php code.

<title>hotnews</title><?php//LAST UPDATE// 27-09-2007include("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    ?>

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...