Jump to content


Photo

Undefined Offset


  • Please log in to reply
8 replies to this topic

#1 Rob56

Rob56

    Newbie

  • New Members
  • Pip
  • 4 posts

Posted 17 November 2012 - 08:16 AM

The following code works, but I get the the error 'undefined offset' on the items of the while list.

All the items are in the database.

What is the problem?

<?
include ("db_connect.php");
$query = "SELECT * FROM rooster
";; 
$resultaat = ""; 
$resultaat = mysql_query($query, $db);
mysql_close($db);
?>

<?php
while(list($roosterid, $ma, $kls,$ge,$f101,$extra, $urid, $urma, $urclass, $urge, $urf101) = mysql_fetch_row($resultaat)){
  
 echo "<P>$kls";
 
 echo "<BR><a href=\"make.php?id=$roosterid\">Fill in</a><br>";
}
?> 


#2 Pikachu2000

Pikachu2000

    I hate everything.

  • Staff Alumni
  • 11,378 posts
  • LocationFuture Independent Republic of Texas
  • Age:106

Posted 17 November 2012 - 09:39 AM

I've tried to replicate the error and I can't make it happen. Is that the actual code you're using? Have you included all the line(s) that any error messages point to?
"Java" is to "Javascript" about the same as "fun" is to "funeral".

Why $_SERVER['PHP_SELF'] is bad. || Why ORDER BY RAND() is bad || Every problem can be solved with rm -rf *

Random Quote: "

#3 PFMaBiSmAd

PFMaBiSmAd

    Advanced Member

  • Staff Alumni
  • 16,767 posts
  • LocationColorado, U.S.A.

Posted 17 November 2012 - 09:45 AM

You likely have more items in your list() statement than what there are columns in your query. Posting the actual error message(s) would help pin down what is occurring.
Signature: (not a comment about anything you posted unless specifically indicated)
Debugging step #1: To get past the garbage-out equals garbage-in stage in your code, you must check that the inputs to your code are what you expect.

Programming is just problem solving, but it is done in another language. You must learn enough of the programming language you are using to be able to read and write code.

#4 Pikachu2000

Pikachu2000

    I hate everything.

  • Staff Alumni
  • 11,378 posts
  • LocationFuture Independent Republic of Texas
  • Age:106

Posted 17 November 2012 - 09:48 AM

And naturally, that's about the only thing I didn't try, lol. But yeah, that seems to cause the same symptoms when I run it that way.
"Java" is to "Javascript" about the same as "fun" is to "funeral".

Why $_SERVER['PHP_SELF'] is bad. || Why ORDER BY RAND() is bad || Every problem can be solved with rm -rf *

Random Quote: "

#5 Rob56

Rob56

    Newbie

  • New Members
  • Pip
  • 4 posts

Posted 17 November 2012 - 10:32 AM

Thank you for your help,

This is the code. Nothing is missing, now


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
	 "[url="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd[/url]">
<html xmlns="[url="http://www.w3.org/1999/xhtml"]http://www.w3.org/1999/xhtml[/url]" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf8_unicode_ci" />
<title></title>
<link rel= "stylesheet" href="menu_style.css" type="text/css" />
<link href="opmaak.css" rel="stylesheet" type="text/css"/>
</head>
<?php
include 'navigatie/nav6.inc';
?>
<div id="container">
<div id="header3">


<html>
<head>
<?
include ("db_connect.inc.php");
$query = "SELECT * FROM rooster
";; // de SQL-query die wordt uitgevoerd
$resultaat = ""; // hierin wordt het resultaat van de query opgeslagen
$resultaat = mysql_query($query, $db);
mysql_close($db);
?>

<h2>Rooster</h2>
<hr>
<?php
while(list($roosterid, $ma, $kls,$ge,$f101,$extra, $urid, $urma, $urclass, $urge, $urf101) = mysql_fetch_row($resultaat)){

echo "<P>$kls";

echo "<BR><a href=\"make.php?id=$roosterid\"></a><br>Fill in";
}
?>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
		"[url="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"]http://www.w3.org/TR...tml1-strict.dtd[/url]">
<html xmlns="[url="http://www.w3.org/1999/xhtml"]http://www.w3.org/1999/xhtml[/url]" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf8_unicode_ci" />
<title></title>
<link rel= "stylesheet" href="menu_style.css" type="text/css" />
<link href="opmaak.css" rel="stylesheet" type="text/css"/>
</head>
</head>
<?php
include 'navigatie/nav6.inc';
?>
<div id="container">
<div id="header3">
  

<html>
<head>
</head>
<body>
<?php
include ("db_connect.inc.php");
// controleren of pagina zichzelf heeft aangeroepen
// via hidden field " bevestiging" van formulier
if (isset($_POST["bevestiging"])){
// query samenstellen
// LET OP: in werkelijkheid nog validatie uitvoeren van de gegevens in $_POST
$query = "UPDATE rooster SET
  
  
  
ma = '". $_POST["ma"] ."',
kls = '". $_POST["kls"] ."',
  
goe = '". $_POST["ge"] ."',
f101 = '". $_POST["f101"] ."'
WHERE roosterid='" .$_POST["id"] ."'";
mysql_query($query);

$query = "UPDATE tw SET
  
  
  
urma = '". $_POST["urma"] ."',
urclass = '". $_POST["urclass"] ."',
urge = '". $_POST["urge"] ."',
urf101 = '". $_POST["urf101"] ."'
WHERE urid='" .$_POST["id"] ."'";
mysql_query($query);



echo "<h2>Rooster</h2>De volgende opdracht is uitgevoerd: <b>$query</b><br><hr>\n";
echo "Record nummer " .$_POST["id"] . " is bijgewerkt<br>\n";
echo "<a href=\"wijzigen.php\">Terug naar wijzigen.</a>";
}
else{
// pagina heeft zichzelf nog niet aangeroepen, gegevens inlezen
$query="SELECT * FROM rooster INNER JOIN tw ON roosterid=urid WHERE roosterid='". $_GET["id"] ."'";
$resultaat = mysql_query($query);
while (list($roosterid, $ma, $kls,$ge,$f101,$extra, $urid, $urma, $urclass, $urge, $urf101  ) = mysql_fetch_row($resultaat)){
  
$vn=$ma;
$vm=$kls;
$aa=$ge;
$ab=$f101;  
$ac=$urge;
$ad=$urf101;
  }
?>
<form action="<?php echo($_SERVER["PHP_SELF"]);?>" method="post">

<input type="hidden" name="bevestiging" value="1">
<input type="hidden" name="id" value="<?php echo $_GET["id"];?>">

<h2>wijzigen</h2>
Kls:<input type="text" name="kls"
  value="<?php echo$vm;?>" size="50""><br>

Dag
<input type="checkbox" name="ma" <?php if (!empty($vn)) {echo 'checked="checked"';} ?> value="ma""/>ma
<br>Eerste
<input type="checkbox" name="ge" <?php if (!empty($aa)) {echo 'checked="checked"';} ?> value="ge""/>ge
<input type="checkbox" name="f101" <?php if (!empty($ab)) {echo 'checked="checked"';} ?> value="f101""/>101
<br>Tweede
<input type="checkbox" name="urge" <?php if (!empty($ac)) {echo 'checked="checked"';} ?> value="ge""/>ge
<input type="checkbox" name="urf101" <?php if (!empty($ad)) {echo 'checked="checked"';} ?> value="utf101""/>101
<hr>
<input type="Submit" value="Registreer de gegevens.">
</form>
<?php
}// else-blok afsluiten
?>
</body>
</html>
</div>



Edited by Rob56, 17 November 2012 - 10:34 AM.


#6 jcbones

jcbones

    Advanced Member

  • Gurus
  • 2,442 posts
  • LocationNorth Carolina

Posted 17 November 2012 - 11:54 AM

Post the structure of your database table you are extracting from.

#7 Rob56

Rob56

    Newbie

  • New Members
  • Pip
  • 4 posts

Posted 17 November 2012 - 12:21 PM

Here they are.

-- phpMyAdmin SQL Dump
-- version 3.3.0
-- http://www.phpmyadmin.net
--
-- Machine: localhost
-- Genereertijd: 17 Nov 2012 om 18:16
-- Serverversie: 5.1.44
-- PHP-Versie: 5.2.13
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
--
-- Database: `rooster`
--
-- --------------------------------------------------------
--
-- Tabelstructuur voor tabel `rooster`
--
CREATE TABLE IF NOT EXISTS `rooster` (
`roosterid` int(4) NOT NULL AUTO_INCREMENT,
`ma` text CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`kls` text NOT NULL,
`ge` text NOT NULL,
`f101` text NOT NULL,
`extra` text NOT NULL,
PRIMARY KEY (`roosterid`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
--
-- Gegevens worden uitgevoerd voor tabel `rooster`
--


-- phpMyAdmin SQL Dump
-- version 3.3.0
-- http://www.phpmyadmin.net
--
-- Machine: localhost
-- Genereertijd: 17 Nov 2012 om 18:17
-- Serverversie: 5.1.44
-- PHP-Versie: 5.2.13
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
--
-- Database: `rooster`
--
-- --------------------------------------------------------
--
-- Tabelstructuur voor tabel `tw`
--
CREATE TABLE IF NOT EXISTS `tw` (
`urid` int(4) NOT NULL AUTO_INCREMENT,
`urma` text CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`urclass` text NOT NULL,
`urge` text NOT NULL,
`urf101` text NOT NULL,
`extra` text NOT NULL,
PRIMARY KEY (`urid`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
--
-- Gegevens worden uitgevoerd voor tabel `tw`
--


errors:

first code
Notice: Undefined offset: 10 in D:... on line 40

Notice: Undefined offset: 9 in D:.. on line 40

Notice: Undefined offset: 8 in D:... on line 40

Notice: Undefined offset: 7 in D:... on line 40

Notice: Undefined offset: 6 in D:... on line 40
second code
Notice: Undefined index: f101 in D:... on line 47

Notice: Undefined index: urma in D:... on line 59

Notice: Undefined index: urclass in D:... on line 61

Notice: Undefined index: urf101 in D:... on line 65

#8 Pikachu2000

Pikachu2000

    I hate everything.

  • Staff Alumni
  • 11,378 posts
  • LocationFuture Independent Republic of Texas
  • Age:106

Posted 17 November 2012 - 12:24 PM

Your table only contains 6 fields. Your list() is looking for 11 fields. Anything beyond 6 will be undefined.
"Java" is to "Javascript" about the same as "fun" is to "funeral".

Why $_SERVER['PHP_SELF'] is bad. || Why ORDER BY RAND() is bad || Every problem can be solved with rm -rf *

Random Quote: "

#9 Rob56

Rob56

    Newbie

  • New Members
  • Pip
  • 4 posts

Posted 17 November 2012 - 12:54 PM

Thank you very much, I forgot the INNER JOIN ON roosterid=twid. There are no errors Undefined offset anymore.

But the Undefined index in the second code stays.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com