Error fetching results

The following scripts were made while following the guidance of the “Build Your Own Database Driven WEb Site Using PHP & MySQL by Kevin Yank (4th Edition)”

I began working on a CMS with the book i have been following. I have got to the point of sorting out all the directory problems and multiple other errors but one error persisted through whatever i attempted to change.

include $_SERVER['DOCUMENT_ROOT'] . 'admin/includes/db.inc.php';
$result = mysqli_query($link, 'SELECT id, user FROM contributor');
if (!$result)
{
	$error = 'Error fetching contributors from database!';
	include 'error.html.php';
	exit();
}

As you can see, the error is “Error fetching contributors from database!”. From my understanding this has resulted from a stable database connection but maybe a problem with a table or values in that table.

The table in my MySQL database is shown as: id, user, email, thus as far as i am able to see the values in my script above should function correctly, but it does not.

I can predict that i will need to provide more code, but as of yet i am unsure which i should provide you with so if you need more code to work out whats going wrong, please let me know.

I’ve just had a look at the code and without testing it looks like the code to update the database is being missed due to these lines (I think):

index.php


if (isset($_GET['editform']))

form.html.php


<form action="?<?php htmlout($action); ?>" method="post">

First thing I don’t know why you have a question mark (?) in the action, a typo? Also the form is set to POST but the code in the index.php is set to GET, maybe chagne it to POST might help.

Like I said, I’ve not tested it nor read the book, so I don’t know the full story behind it. Don’t forget, if the above ideas fails, place the error result code in each page.

I did this and nothing happened, same thing.

What happens when you enable error reporting?

error_reporting(E_ALL); 

Have you tried on ALL your php pages?

That was my point, there is no error.

I click “Edit”. It sends me to the edit page, i enter the changed user and email and then click “Update contributor”. It then sends me back to the contributor list page with all the contributors. The one i was trying to edit remains the same as it was before the edit.

What is the error? Precisely. :slight_smile:

index.php


<?php
include_once $_SERVER['DOCUMENT_ROOT'] .
		'admin/includes/magicquotes.inc.php';

if (isset($_GET['add']))
{
	$pagetitle = 'New Contributor';
	$action = 'addform';
	$user = '';
	$email = '';
	$id = '';
	$button = 'Add contributor';

	include 'form.html.php';
	exit();
}

if (isset($_GET['addform']))
{
	include $_SERVER['DOCUMENT_ROOT'] . 'admin/includes/db.inc.php';

	$user = mysqli_real_escape_string($link, $_POST['user']);
	$email = mysqli_real_escape_string($link, $_POST['email']);
	$sql = "INSERT INTO contributor SET
			user='$user',
			email='$email'";
	if (!mysqli_query($link, $sql))
	{
		$error = 'Error adding submitted contributor.';
		include 'error.html.php';
		exit();
	}

	header('Location: .');
	exit();
}

if (isset($_POST['action']) and $_POST['action'] == 'Edit')
{
	include $_SERVER['DOCUMENT_ROOT'] . 'admin/includes/db.inc.php';

	$id = mysqli_real_escape_string($link, $_POST['id']);
	$sql = "SELECT id, user, email FROM contributor WHERE id='$id'";
	$result = mysqli_query($link, $sql);
	if (!$result)
	{
		$error = 'Error fetching author details.';
		include 'error.html.php';
		exit();
	}
	$row = mysqli_fetch_array($result);

	$pagetitle = 'Edit Contributor';
	$action = 'editform';
	$user = $row['user'];
	$email = $row['email'];
	$id = $row['id'];
	$button = 'Update contributor';

	include 'form.html.php';
	exit();
}

if (isset($_GET['editform']))
{
	include $_SERVER['DOCUMENT_ROOT'] . 'admin/includes/db.inc.php';

	$id = mysqli_real_escape_string($link, $_POST['id']);
	$user = mysqli_real_escape_string($link, $_POST['user']);
	$email = mysqli_real_escape_string($link, $_POST['email']);
	$sql = "UPDATE contributor SET
			user='$user',
			email='$email'
			WHERE id='$id'";
	if (!mysqli_query($link, $sql))
	{
		$error = 'Error updating submitted contributor.';
		include 'error.html.php';
		exit();
	}

	header('Location: .');
	exit();
}

if (isset($_POST['action']) and $_POST['action'] == 'Delete')
{
	include $_SERVER['DOCUMENT_ROOT'] . 'admin/includes/db.inc.php';
	$id = mysqli_real_escape_string($link, $_POST['id']);

	$sql = "SELECT itemid FROM item WHERE contributorid='$id'";
	$result = mysqli_query($link, $sql);
	if (!$result)
	{
		$error = 'Error getting list of items to delete.';
		include 'error.html.php';
		exit();
	}

	while ($row = mysqli_fetch_array($result))
	{
		$itemid = $row[0];

		$sql = "DELETE FROM itemcategory WHERE itemid='$itemid'";
		if (!mysqli_query($link, $sql))
		{
			$error = 'Error deleting category entries for item.';
			include 'error.html.php';
			exit();
		}
	}

	$sql = "DELETE FROM item WHERE contributorid='$id'";
	if (!mysqli_query($link, $sql))
	{
		$error = 'Error deleting items for contributor.';
		include 'error.html.php';
		exit();
	}

	$sql = "DELETE FROM contributor WHERE id='$id'";
	if (!mysqli_query($link, $sql))
	{
		$error = 'Error deleting contributor.';
		include 'error.html.php';
		exit();
	}

	header('Location: .');
	exit();
}

include $_SERVER['DOCUMENT_ROOT'] . 'admin/includes/db.inc.php';
$result = mysqli_query($link, 'SELECT id, user FROM contributor');
if (!$result)

{

    printf("Errormessage: %s\
", mysqli_error($link));

exit();

} 


while ($row = mysqli_fetch_array($result))
{
	$contributors[] = array('id' => $row['id'], 'user' => $row['user']);
}

include 'contributors.html.php';
?>

form.html.php

<?php include_once $_SERVER['DOCUMENT_ROOT'] .
		'admin/includes/helpers.inc.php'; ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
		"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
	<head>
		<title><?php htmlout($pagetitle); ?></title>
		<meta http-equiv="content-type"
				content="text/html; charset=utf-8"/>
	</head>
	<body>
		<h1><?php htmlout($pagetitle); ?></h1>
		<form action="?<?php htmlout($action); ?>" method="post">
			<div>
				<label for="user">User: <input type="text" user="user"
						id="user" value="<?php htmlout($user); ?>"/></label>
			</div>
			<div>
				<label for="email">Email: <input type="text" name="email"
						id="email" value="<?php htmlout($email); ?>"/></label>
			</div>
			<div>
				<input type="hidden" user="user" value="<?php
						htmlout($id); ?>"/>
				<input type="submit" value="<?php htmlout($button); ?>"/>
			</div>
		</form>
	</body>
</html>

contributors.html.php


<?php include_once $_SERVER['DOCUMENT_ROOT'] .
		'admin/includes/helpers.inc.php'; ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
		"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
	<head>
		<title>Manage Contributors</title>
		<meta http-equiv="content-type"
				content="text/html; charset=utf-8"/>
	</head>
	<body>
		<h1>Manage Contributors</h1>
		<p><a href="?add">Add new contributor</a></p>
		<ul>
			<?php foreach ($contributors as $contributor): ?>
				<li>
					<form action="" method="post">
						<div>
							<?php htmlout($contributor['user']); ?>
							<input type="hidden" user="id" value="<?php
									echo $contributor['id']; ?>"/>
							<input type="submit" name="action" value="Edit"/>
							<input type="submit" name="action" value="Delete"/>
						</div>
					</form>
				</li>
			<?php endforeach; ?>
		</ul>
		<p><a href="..">Return to IMS home</a></p>
	</body>
</html>

Hope this helps. Essentially what all this should do is allow for editing of data in the MySQL table, deletion of it and inserting of new data. In the list of contributors, there are two buttons beside each contributor: “Edit” and “Delete”. As i said in my previous post, thesse buttons do nothing as of yet, just seem to refresh the page.

The second problem is that when i attempt to add data to the table it does indeed add data but the contributor’s name does not appear, just the two buttons. When i check the MySQL table, the data shows that there was a new entry added but no user (name) and thus that was why it was only showing the buttons. It appears that the user entered is not recognised.

ok thanks, try this and let me know what happens :slight_smile:


if (!$result)
{
    printf("Errormessage: %s\
", mysqli_error($link));
exit();
}

I wish that were the problem, however i did indeed name it contributor.

are you sure Table ‘contributor’ do exist ?

and not contributors

:slight_smile:

I am now able to both delete and add new entries. I am still however unable to find the reason for why the edit would not be working correctly. Any suggestions from my code?

This will not help with your problem, but it helps me, to get out of the “stuck state” - if that’s you case, and since it seems that you have no clue where is the problem at the first place, I would advice some basic debugging:

I would:

  1. Try to do the query without any PHP - directly. Does it work?
    No: the problem relies on the query. Yes:
  2. Do a var_dump on the mysql result. Does it work?
    No: the problem is within your database php script.
    Yes:
  3. pass to the next level…

If you are stuck, you can start debugging it and see what you get.

That will also help you to get more help here in the forums because, by doing that, you will better isolate your issue, hence, being easier for others to help you out.

Sorry for not being able to directly help you, but maybe this var_dump advice could do something.

K. Regards,
Márcio

yep thanks :slight_smile: show us the code when you have the time for us to help you debug it further

Ok, thanks for this. I have solved the problem regarding that.

Sadly another error has appeared: “Notice: Undefined index: id in C:\wamp\www\admin\contributors\index.php on line 42”. This error appears when i select the “edit” button beside one of the items in the database. When i also continue to change the name of that item the name does not change.

Another problem is with the adding of items to the database. I add an item, it is added to the database but a name does not appear - only the edit and delete options, which do not do anything as stated above.

Thought i’d give you this information, i am going to look into my script further when i have more time but i thought you guys should know the situation for the time being. Appears to be more problems with me having some mis-matched values.

Yes, good spot matey! :tup:
Although I would change


if (isset($_GET['addform']))

to


if (isset($_POST['addform']))

rather than change the form action as all other references are to POST :slight_smile: