Hi,
I have a table which displays values from a table.
But I would like to be able to directly edit the table.
Basically I want each cell of the table to be a form.
Then one submit button would post all the values to the DB.
I have attempted this but know that it is incorrect as the I have no way to distinguish the values in the form in each cell.
Here is my index.php excerpt
if (isset($_GET['courses']))
{
include $_SERVER['DOCUMENT_ROOT'] . '/artgibney/includes/db.inc.php';
// the following try is probably not correct
try
{
$sql = 'SELECT id, course FROM courses WHERE id = :id';
$s = $pdo->prepare($sql);
$s->bindValue(':id', $_POST['id']);
$s->bindValue(':course', $_POST['course']);
$s->execute();
}
catch (PDOException $e)
{
$error = 'Error fetching course.';
include 'error.html.php';
exit();
}
$row = $s->fetch();
$pageTitle = 'Manage courses';
$action = 'editcourse';
$text = $row['course'];
$id = $row['id'];
$button = 'Update course';
// Build the list of courses
try
{
$result = $pdo->query('SELECT id, course FROM courses');
}
catch (PDOException $e)
{
$error = 'Error fetching list of courses.';
include 'error.html.php';
exit();
}
foreach ($result as $row)
{
$courses[] = array('id' => $row['id'], 'course' => $row['course']);
}
include 'managecourses.html.php';
exit();
}
The form then is,
<?php include_once $_SERVER['DOCUMENT_ROOT'] . '/artgibney/includes/helpers.inc.php'; ?>
<?php include_once $_SERVER['DOCUMENT_ROOT'] . '/artgibney/includes/func.inc.php'; ?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Manage Courses</title>
</head>
<body>
<h1>Manage Courses</h1>
<table>
<tr>
<th></th>
<th>Courses</th>
</tr>
<?php foreach (array_reverse($courses) as $course): ?>
<li>
<form action="" method="post">
<tr>
<input type="hidden" name="id" value="<?php
echo $course['id']; ?>">
<td><input type="submit" name="action" value="Delete"></td>
<td><input type="text" name="name" id="name" value="<?php htmlout($course['course']); ?>"></td>
</tr>
</form>
</li>
<?php endforeach; ?>
</table>
<p><a href="..">Return to physCMS home</a></p>
<?php include '../logout.inc.html.php'; ?>
</body>
</html>
I have not yet attempted to write the next part which sends the values in the table to the DB.
Any help would be greatly appreciated,
Thanks,
Shane