Jump to content

Undefined Index


shadow_soldier

Recommended Posts

I'm trying to make a documentation area for my site and to get the data from a MySQL database I use the following function:

function getDocument($docname) {
	// connect to database
	mysql_connect("localhost", "root", "root") or die(mysql_error());
	mysql_select_db("clientarea") or die(mysql_error());

	// get data
	$sql = mysql_query("SELECT doc FROM spprtdocs WHERE docname='$docname'");
	$gotdoc = mysql_fetch_assoc($sql);

	// echo result
	echo($gotdoc[$docname]);

	// close db con
	mysql_close(mysql_connect("localhost", "root", "root"));
}

 

However, when I run the script by running <? getDocument('testdoc'); ?> I get this error:

Notice: Undefined index: testdoc in /Applications/MAMP/htdocs/projects/Clients/modules/system/areacore.php on line 105

However, another similar function works correctly:

function getData($data) {
	// cookie info
	$aca = $_COOKIE['aca'];
	$acap = $_COOKIE['acap'];

	// connect to database
	mysql_connect("localhost", "root", "root") or die(mysql_error());
	mysql_select_db("clientarea") or die(mysql_error());

	// get data
	$sql = mysql_query("SELECT $data FROM users WHERE username='$aca'");
	$gotinfo = mysql_fetch_assoc($sql);

	// echo result
	echo($gotinfo[$data]);

	mysql_close(mysql_connect("localhost", "root", "root"));
}

 

I've checked all the MySQL details and other things that could go wrong. Also, if I disable error reporting in php.ini, it just doesnt work at all. Any help would be greatly appreciated.

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

This is line 105:

echo($gotdoc[$docid]);

in the getDocument() function.

 

I'm not passing the variable for getDocument() using $_POST. Should I be? Like I said, it works fine for getData().

 

EDIT: Sorry made a typo. Line 105 is this:

echo($gotdoc[$docname]);

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

Your problem comes from these lines:

<?php
	$sql = mysql_query("SELECT doc FROM spprtdocs WHERE docname='$docname'");
	$gotdoc = mysql_fetch_assoc($sql);

	// echo result
	echo($gotdoc[$docname]);
?>

In the select statement you are saying to get the field "doc", but you are trying to get what ever is passed to the function in the variable $docname, if that's not "doc", you have a problem. Change the select statement to:

<?php
	$sql = mysql_query("SELECT $docname FROM spprtdocs WHERE docname='$docname'");
?>

Which doesn't really make sense, or change the whole block to

<?php
	$sql = mysql_query("SELECT doc FROM spprtdocs WHERE docname='$docname'");
	$gotdoc = mysql_fetch_assoc($sql);

	// echo result
	echo($gotdoc['doc']);
?>

 

Ken

 

Link to comment
https://forums.phpfreaks.com/topic/98146-undefined-index/#findComment-502126
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.