Jump to content

Notice: Undefined offset error - No idea why its happening...


Russia

Recommended Posts

Im having this error and have the not even the slightest clue on how to solve it...

 

 

Notice: Undefined offset: 2 in C:\wamp\www\updat5.php on line 146

 

<?php
$picture = array();
while($row = mysql_fetch_assoc($result))
{
$id = $row['id'];
$picture = array();
switch ($row['icon']) {
    case 1:
        $picture[$id] = '<img src="img/apple.gif" title="apple" alt="apple" />';
        echo $picture[$id];
        break;
        
    case 2:
        $picture[$id] = '<img src="img/banana.gif" title="banana" alt="banana" />';
        echo $picture[$id];
        break;
        
    case 3:
        $picture[$id] = '<img src="img/orange.gif" title="orange" alt="orange" />';
        echo $picture[$id];
        break;
        
    default:
        $picture[$id] = '';
        echo $row['icon'] . " is something other than 1 2 or 3";
        break;
}  
}
?>

<hr>

<?php
echo $picture[2];
?>

 

And line 146 is:

 

echo $picture[2];

 

My next post will be the full code if needed incase the error I have currently cant be fixed with the current snippet/block i posted.

Link to comment
Share on other sites

Updated code:

 

<?php
$picture = array();
while($row = mysql_fetch_assoc($result))
{
$id = $row['id'];
switch ($row['icon']) {
    case 1:
        $picture[$id] = '<img src="img/apple.gif" title="apple" alt="apple" />';
        echo $picture[$id];
        break;      
    case 2:
        $picture[$id] = '<img src="img/banana.gif" title="banana" alt="banana" />';
        echo $picture[$id];
        break;    
    case 3:
      $picture[$id] = '<img src="img/orange.gif" title="orange" alt="orange" />';
        echo $picture[$id];
        break;      
    default:
        $picture[$id] = '';
        echo $row['icon'] . " is something other than 1 2 or 3";
        break;
   } 
}
?>

 

and this is the part that echos that shows the images:

<?php
echo $picture[2];
?>

Link to comment
Share on other sites

The error is happening because you're attempting to access an array element that doesn't exist.  Your array has one item, indexed by whatever value $id contains.  $picture[2] is attempting to access the third (because arrays are zero-indexed) item in the array, which, again, doesn't exist.  Hence an undefined offset.

 

There's a simple fix: $picture['2']

Link to comment
Share on other sites

I had this error:

 

UHQqZ.png

 

So then I moved the code under the table with the form and it stopped working.

 

But you can see the \"banana\"  in the image showing the echoing of the row value.

 

so somehow its not working once i put it under the form.

 

this is the lines of code the error was referring to:

 

<script language="javascript" type="text/javascript">
function showvalue(arg) {



alert(arg);



//arg.visible(false);
}
$(document).ready(function() {
try {





oHandler = $(".mydds").msDropDown().data("dd");





oHandler.visible(true);





//alert($.msDropDown.version);





//$.msDropDown.create("body select");





$("#ver").html($.msDropDown.version);





} catch(e) {





alert("Error: "+e.message);





}
})




</script>

 

 

Link to comment
Share on other sites

Il post the whole script for you here:

 


<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');


mysql_connect("localhost", "", "")or die("cannot connect");
mysql_select_db("test")or die("cannot select DB");
$tbl_name="test_mysql";
$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
$count=mysql_num_rows($result);

if (isset($_POST['Submit'])) {
for($i=0;$i<$count;$i++){
	$month   = $_POST['month'];
	$date    = $_POST['date'];
	$message = $_POST['message'];
	$title   = $_POST['title'];
	$id      = $_POST['id'];
	$icon    = $_POST['icon']; 
	$monthday= $month[$i]."<br>".$date[$i];
	$sql1="UPDATE $tbl_name 
				SET monthday='$monthday', 
					month='$month[$i]', 
					date='$date[$i]', 
					message='" . mysql_real_escape_string($message[$i]) . "', 
					title='" . mysql_real_escape_string($title[$i]) . "',
					icon='$icon[$i]'						
				WHERE id=".$id[$i];

if(!($result1 = mysql_query($sql1)))
	{                   
	"<BR>Error UPDATING $tbl_name ";                              
	exit();                                             
	}                                                   
}
}
$result=mysql_query($sql);
$count=mysql_num_rows($result);


?>



// ORIGINAL SPOT OF THE CODE THAT WORKED PERFECTLY BUT THAT GAVE THE popup ERROR!



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Image Dropdown</title>
<link rel="stylesheet" type="text/css" href="dd.css" />
<script type="text/javascript" src="jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="jquery.dd.js"></script>

</head>
<body>



<form name="form1" method="post" action="updat5.php">
<tr>
<td>
<table width="100%" border="0" cellspacing="1" cellpadding="0">

<tr>
<td align="center"><strong>ID</strong></td>
<td align="center"><strong>Month Date</strong></td>
<td align="center"><strong>Message</strong></td>
<td align="center"><strong>Title</strong></td>
<td align="center"><strong>Icon</strong></td>

</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td align="center"><?php $id[]=$rows['id']; ?><?php echo $rows['id']; ?>
<input type="hidden" id="id" name="id[]" value="<?php echo $rows['id']; ?>">
</td>



<td align="center"><input style="border: 1px solid #C3C3C3;height: 20px;" name="month[]" MAXLENGTH="3" size="3" type="text" id="month" value="<?php echo $rows['month']; ?>">
<input style="border: 1px solid #C3C3C3;height: 20px;" name="date[]" MAXLENGTH="2" size="2" type="text" id="date" value="<?php echo $rows['date']; ?>">
</td>
<td align="center"><input style="border: 1px solid #C3C3C3;height: 20px;" name="message[]" size="70" type="text" id="message" value="<?php echo $rows['message']; ?>"></td>
<td align="center"><input style="border: 1px solid #C3C3C3;height: 20px;" name="title[]" size="70" type="text" id="title" value="<?php echo $rows['title']; ?>"></td>
<td align="center">
<select name="icon[]" style="width:200px" class="mydds">
<option value="1"<?php if ($rows['icon'] == 1) { echo 'selected="selected"'; }  ?> title="icon/icon_phone.gif">Phone</option>
<option value="2"<?php if ($rows['icon'] == 2) { echo 'selected="selected"'; }  ?> title="icon/icon_sales.gif">Graph</option>
<option value="3"<?php if ($rows['icon'] == 3) { echo 'selected="selected"'; }  ?> title="icon/icon_faq.gif">Faq</option>
</select>
</td>
</tr>

<?php
}
?>
<tr>
<td colspan="4" align="center"><br><input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</td>
</tr>
</form>

<hr>




//SO I MOVED IT TO THIS SPOT AND IT STOPPED WORKING AND SAYING ITS MISSING VARIABLES AND INDEXES. 

<?php
$picture = array();
while($row = mysql_fetch_assoc($result))
{
$id = $row['id'];
switch ($row['icon']) {
    case 1:
        $picture[$id] = '<img src="img/apple.gif" title="apple" alt="apple" />';
        echo $picture[$id];
        break;      
    case 2:
        $picture[$id] = '<img src="img/banana.gif" title="banana" alt="banana" />';
        echo $picture[$id];
        break;    
    case 3:
	$picture[$id] = '<img src="img/orange.gif" title="orange" alt="orange" />';
        echo $picture[$id];
        break;      
    default:
        $picture[$id] = '';
        echo $row['icon'] . " is something other than 1 2 or 3";
        break;
} 
}
?>

<?php
echo $picture['2'];
?>

<script language="javascript" type="text/javascript">
function showvalue(arg) {
alert(arg);
//arg.visible(false);
}
$(document).ready(function() {
try {
	oHandler = $(".mydds").msDropDown().data("dd");
	oHandler.visible(true);
	//alert($.msDropDown.version);
	//$.msDropDown.create("body select");
	$("#ver").html($.msDropDown.version);
	} catch(e) {
	alert("Error: "+e.message);
	}
})	
</script>
</body>
</html>

 

If you look in the code I made 2 //comments about the positions of the code at different times.

Link to comment
Share on other sites

Im talking about this code I moved:

 

<?php
$picture = array();
while($row = mysql_fetch_assoc($result))
{
$id = $row['id'];
switch ($row['icon']) {
    case 1:
        $picture[$id] = '<img src="img/apple.gif" title="apple" alt="apple" />';
        echo $picture[$id];
        break;      
    case 2:
        $picture[$id] = '<img src="img/banana.gif" title="banana" alt="banana" />';
        echo $picture[$id];
        break;    
    case 3:

   

   

$picture[$id] = '<img src="img/orange.gif" title="orange" alt="orange" />';
        echo $picture[$id];
        break;      
    default:
        $picture[$id] = '';
        echo $row['icon'] . " is something other than 1 2 or 3";
        break;

   

} 
}
?>

<?php
echo $picture['2'];
?>

 

Also, no I dont have a link because as you an see all my errors are pointing to:

C:\wamp\www\updat5.php

 

Its on my wamp server. But yeah, do you want me to make a video of the error live? You tell me what you want me to record and il post a video on youtube and give you the link.

 

[/code]

Link to comment
Share on other sites

I cleaned up your code a bit.  You had some redundancy, and your update query needed tweaking (you were inadvertently overwriting values, and then trying to access scalar variables as array variables).

 

I'm as baffled as you, to be honest.  The only answer I can see is that you don't have an $id which equals 2.  Double check your db to make sure you have a row whose id is 2.  You could also do a simple test:

 

$isIDGood = (isset($picture[2])) ? "yes" : "no";

echo 'Is $picture[2] valid?  ' . $isIDGood;

 

And, really, it's good practice to check to see if a particular array element is set before calling it directly anyway.

 

---

 

Anyway, here's a slightly cleaned up version for you:

 

<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');

mysql_connect("localhost", "", "")or die("cannot connect");
mysql_select_db("test")or die("cannot select DB");
$tbl_name="test_mysql";
$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
$count=mysql_num_rows($result);

if (isset($_POST['Submit'])){
for($i=0;$i<$count;$i++){
	$month[]   = $_POST['month'][$i];
	$date[]    = $_POST['date'][$i];
	$message[] = $_POST['message'][$i];
	$title[]   = $_POST['title'][$i];
	$id[]      = $_POST['id'][$i];
	$icon[]    = $_POST['icon'][$i]; 

	$monthday  = $month[$i] . "<br>" . $date[$i];

	$sql1 = "UPDATE $tbl_name SET monthday='$monthday', 
				month='{$month[$i]}', 
				date='{$date[$i]}', 
				message='" . mysql_real_escape_string($message[$i]) . "', 
				title='" . mysql_real_escape_string($title[$i]) . "', 
				icon='{$icon[$i]}' WHERE id='{$id[$i]}'";

	if(!($result1 = mysql_query($sql1))){
		"<BR>Error UPDATING $tbl_name ";
		exit();
	}
}
}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<title>Image Dropdown</title>
	<link rel="stylesheet" type="text/css" href="dd.css" />
	<script type="text/javascript" src="jquery-1.3.2.min.js"></script>
	<script type="text/javascript" src="jquery.dd.js"></script>
</head>

<body>
	<form name="form1" method="post" action="updat5.php">
		<table width="100%" border="0" cellspacing="1" cellpadding="0">
			<tr>
				<td align="center"><strong>ID</strong></td>
				<td align="center"><strong>Month Date</strong></td>
				<td align="center"><strong>Message</strong></td>
				<td align="center"><strong>Title</strong></td>
				<td align="center"><strong>Icon</strong></td>
			</tr>

			<?php
				while($rows=mysql_fetch_array($result)){
			?>

			<tr>
				<td align="center">
					<?php $id[]=$rows['id']; ?><?php echo $rows['id']; ?><input type="hidden" id="id" name="id[]" value="<?php echo $rows['id']; ?>">
				</td>
				<td align="center">
					<input style="border: 1px solid #C3C3C3;height: 20px;" name="month[]" MAXLENGTH="3" size="3" type="text" id="month" value="<?php echo $rows['month']; ?>">
					<input style="border: 1px solid #C3C3C3;height: 20px;" name="date[]" MAXLENGTH="2" size="2" type="text" id="date" value="<?php echo $rows['date']; ?>">
				</td>
				<td align="center">
					<input style="border: 1px solid #C3C3C3;height: 20px;" name="message[]" size="70" type="text" id="message" value="<?php echo $rows['message']; ?>">
				</td>
				<td align="center">
					<input style="border: 1px solid #C3C3C3;height: 20px;" name="title[]" size="70" type="text" id="title" value="<?php echo $rows['title']; ?>">
				</td>
				<td align="center">
					<select name="icon[]" style="width:200px" class="mydds">
						<option value="1"<?php if ($rows['icon'] == 1) { echo 'selected="selected"'; }  ?> title="icon/icon_phone.gif">Phone</option>
						<option value="2"<?php if ($rows['icon'] == 2) { echo 'selected="selected"'; }  ?> title="icon/icon_sales.gif">Graph</option>
						<option value="3"<?php if ($rows['icon'] == 3) { echo 'selected="selected"'; }  ?> title="icon/icon_faq.gif">Faq</option>
					</select>
				</td>
			</tr>

			<?php
				}
			?>

			<tr>
				<td colspan="4" align="center">
					<br><input type="submit" name="Submit" value="Submit">
				</td>
			</tr>
		</table>
	</form>

	<hr>

	<?php
		$picture = array();

		while($row = mysql_fetch_assoc($result))
		{
			$id = $row['id'];

			switch ($row['icon']) {
				case 1:
					$picture[$id] = '<img src="img/apple.gif" title="apple" alt="apple" />';
					echo $picture[$id];
					break;      

				case 2:
					$picture[$id] = '<img src="img/banana.gif" title="banana" alt="banana" />';
					echo $picture[$id];
					break;    

				case 3:
					$picture[$id] = '<img src="img/orange.gif" title="orange" alt="orange" />';
					echo $picture[$id];
					break;      

				default:
					$picture[$id] = '';
					echo $row['icon'] . " is something other than 1 2 or 3";
					break;
			} 
		}
	?>

	<?php
		echo $picture[2];
	?>

	<script language="javascript" type="text/javascript">
		function showvalue(arg) {
			alert(arg);
			//arg.visible(false);
		}

		$(document).ready(function() {
			try {
				oHandler = $(".mydds").msDropDown().data("dd");
				oHandler.visible(true);
				//alert($.msDropDown.version);
				//$.msDropDown.create("body select");
				$("#ver").html($.msDropDown.version);
			} 
			catch(e) {
				alert("Error: "+e.message);
			}
		})
	</script>

</body>
</html>

Link to comment
Share on other sites

Why don't you have your icon data stored in another table?  You have a one-to-many relationship there: one icon can have many related entities.  Instead of manually creating the relationship with a PHP switch, put your icon info in a separate table containing an id and path to the icon itself, and run a JOIN to retrieve the aggregated info.  That would save you a lot of work in your script, as you wouldn't have to build an entirely new array just to display the icons.

 

So, you'd keep your current table, and create another for the icons where the 'icon' column of test_mysql is a foreign key containing the id/primary key value of the actual icon table.

 

For more info, take a look at: http://dev.mysql.com/tech-resources/articles/intro-to-normalization.html

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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