Jump to content

undefined index error


j0seph

Recommended Posts

I am running a script on my website to edit a piece a news record located in database table news. The same page displays records within the news table, retrieves the selected record and displays the data in a form and updates modified data to the database.

My code displays the news and the edit form succesfully but the update button will not update the record to the database.

When I include a debugging file i receive this message.

 

When the edit link next to the title of each news record is displayed I pass the edit command and the news_id through the browser.

 

An error occurred in script /home/joea8764/public_html/testing/html/includes/config.inc on line 11: Undefined index: cmdAn error occurred in script /home/joea8764/public_html/testing/html/includes/config.inc on line 11: Undefined index: cmd

 

My code is as follows

 

	<? 
require_once ('../mysql_connect.php'); // Connect to the database.

//If cmd has not been initialized
if(!isset($cmd)) 
{
   //display all the news
   $result = mysql_query("select * from news order by news_id"); 
   
   //run the while loop that grabs all the news scripts
   while($r=mysql_fetch_array($result)) 
   { 
	  //grab the title and the ID of the news
	  $subject=$r["subject"];//take out the title
	  $news_id=$r["news_id"];//take out the id

	 //make the title a link
	  echo "<p>$subject - <a href='news_manage.php?cmd=edit&news_id=$news_id'>Edit?</a> - <a href='#'>Delete?</a></p>";
	}
}
?>

<?
if($_GET["cmd"]=="edit" || $_POST["cmd"]=="edit")
{
   if (!isset($_POST["submit"]))
   {
	  $news_id = $_GET["news_id"];
	  $sql = "SELECT * FROM news WHERE news_id=$news_id";
	  $result = mysql_query($sql);        
	  $myrow = mysql_fetch_array($result);
	  ?>
	  
	  <form action="news_manage.php" method="post">
	  <input type=hidden name="id" value="<?php echo $myrow["news_id"] ?>">
   
	  Title:<INPUT TYPE="TEXT" NAME="title" VALUE="<?php echo $myrow["subject"] ?>" SIZE=30><br>
	  Message:<TEXTAREA NAME="message" ROWS=10 COLS=30><? echo $myrow["story"] ?></TEXTAREA><br>
   
	  <input type="hidden" name="cmd" value="edit">
   
	  <input type="submit" name="submit" value="submit">
   
	  </form>
   
<? } ?>

<?
   if ($_POST["$submit"])
   {
   	  $news_id = $_POST["news_id"];
	  $subject = $_POST["subject"];
	  $story = $_POST["story"];

	  $sql = "UPDATE news SET subject='$subject',story='$story' WHERE news_id=$news_id";

	  $result = mysql_query($sql);
	  echo "This news article has been updated!";
   }
}
?>

 

I am just not able to work out what is going wrong.

Thanks in advanced

Link to comment
https://forums.phpfreaks.com/topic/45933-undefined-index-error/
Share on other sites

Thats solved the error that arose when the records were listed but when the link is clicked to edit a record a new error is thrown up

 

An error occurred in script /home/joea8764/public_html/testing/html/includes/config.inc on line 11: Undefined variable: submitAn error occurred in script /home/joea8764/public_html/testing/html/includes/config.inc on line 11: Undefined index:

 

does this just require more isset statements ??

Link to comment
https://forums.phpfreaks.com/topic/45933-undefined-index-error/#findComment-223129
Share on other sites

Just turn off those notice messages.

 

error_reporting(E_ALL ^ E_NOTICE);

 

If that is not ideal for you, than I suggest you look at the code I provided you and re-do your code according to it. Because before you call an index of an "unknown" array you should check to make sure that there is a value at that arrays index or else the notice is thrown. It does not mean anything other than information telling you that you "SHOULD" check the index of the array before you call it.

Link to comment
https://forums.phpfreaks.com/topic/45933-undefined-index-error/#findComment-223147
Share on other sites

Archived

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

×
×
  • 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.