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.