954,576 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

PHP Undefined Variable Error

Hi All,

Im currently working on 'localhost'. Whenever i run the following all i get is


Notice: Undefined variable: myrow in C:\wamp\www\testbin\edit\edit.php on line 59
<?

//connect to mysql
//change user and password to your mySQL name and password
mysql_connect("localhost","----","----");

//select which database you want to edit
mysql_select_db("silverlink");

//If cmd has not been initialized
if(!isset($cmd))
{
//display all the news
$result = mysql_query("select * from indexinfo WHERE id = 1");

//run the while loop that grabs all the news scripts

//make the title a link
echo "<a href='edit.php?cmd=edit&id=$id'>$Title - Edit</a>";
echo "";

if($_GET["cmd"]=="edit" || $_POST["cmd"]=="edit")
{
if (!isset($_POST["submit"]))
{
$id = 1;
$sql = "SELECT * FROM indexinfo WHERE id=$id";
$result = mysql_query($sql);
$myrow = mysql_fetch_array($result);
?>

<form action="edit.php" method="post">
<input type=hidden name="id" value="<?php echo $myrow["id"] ?>">

Title:<INPUT TYPE="TEXT" NAME="Title" VALUE="<?php echo $myrow["Title"] ?>" SIZE=30>
Subtitle:<INPUT TYPE="TEXT" NAME="Subtitle" VALUE="<?php echo $myrow["Subtitle"] ?>" SIZE=30>
Content:<TEXTAREA NAME="Content" ROWS=10 COLS=30><? echo $myrow["Content"] ?></TEXTAREA>
Author:<INPUT TYPE="TEXT" NAME="Author" VALUE="<?php echo $myrow["Author"] ?>" SIZE=30>
Date:<INPUT TYPE="TEXT" NAME="Date" VALUE="<?php echo $myrow["Date"] ?>" SIZE=30>

<input type="hidden" name="cmd" value="edit">

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

</form>

<? } ?>
<?
if ($_POST["$submit"])
{
$Title = $_POST["Title"];
$Subtitle = $_POST["Subtitle"];
$Content = $_POST["who"];
$Author = $_POST["Author"];
$Date = $_POST["Date"];

$sql = "UPDATE indexinfo SET Title='$Title',Subtitle='$Subtitle',Content='$Content', Author='$Author', Date='$Date' WHERE id=$id";

$result = mysql_query($sql);
echo "Thank you! Information updated.";
}
}
?>

whiteyoh
Posting Pro in Training
479 posts since Jun 2009
Reputation Points: 15
Solved Threads: 15
 

Whiteyoh,

Which is line 59?

Airshow

Airshow
WiFi Lounge Lizard
Moderator
2,683 posts since Apr 2009
Reputation Points: 321
Solved Threads: 372
 

it is all of the form PHP echos

whiteyoh
Posting Pro in Training
479 posts since Jun 2009
Reputation Points: 15
Solved Threads: 15
 

Probably after line: $myrow = mysql_fetch_array($result);

If the query fails, $myrow will be false instead of an array.

pritaeas
Posting Expert
Moderator
5,480 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
 

Whiteyoh,

I agree with Pritaeas.

You can test the theory by inserting after $myrow = mysql_fetch_array($result);

if(!$myrow){
	$myrow = array(
		'Title' => 'testTitle',
		'Subtitle' => 'testSubtitle',
		'Content' => 'testContent',
		'Author' => 'testAuthor',
		'Date' 	 => 'testDate'
	)
}


Airshow

Airshow
WiFi Lounge Lizard
Moderator
2,683 posts since Apr 2009
Reputation Points: 321
Solved Threads: 372
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You