'Edit' and 'Delete' forms not working... help!

Hi,
The following should use the same form.html.php for ‘edit’ and ‘delete’. But when I click on the ‘edit’ and ‘delete’ button nothing seems to happen.
This first file just sends an empty variable ‘courses’ to index.php
notes.html.php

<?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 Daily Notes</title>
  </head>
  <body>
    <h1>Manage Daily Notes</h1>
    <p><a href="?addcourseform">Add course</a></p>
	<? if(countRows('courses')): ?>
 	<p><a href="?addlearnerform">Add learner</a></p>
	<? endif; ?>
	<? if(countRows('learners')): ?>
 	<p><a href="?courses">Manage courses and learners</a></p>
	<? endif; ?>
    <p><a href="..">Return to physCMS home</a></p>
    <?php include '../logout.inc.html.php'; ?>
  </body>
</html>

index.php (excerpt)

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

  // Build the list of course rows
  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();
}

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

  try
  {
    $sql = 'SELECT id, course FROM courses WHERE id = :id';
    $s = $pdo->prepare($sql);
    $s->bindValue(':id', $_POST['id']);
    $s->execute();
  }
  catch (PDOException $e)
  {
    $error = 'Error fetching course row details.';
    include 'error.html.php';
    exit();
  }
  $row = $s->fetch();

  $pageTitle = 'Edit courses';
  $action = 'editformcourse';
  $course = $row['course'];
  $id = $row['id'];
  $button = 'Update course';

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

if (isset($_GET['editformcourse']))
{
  include $_SERVER['DOCUMENT_ROOT'] . '/artgibney/includes/db.inc.php';
  
  try
  {
    $sql = 'UPDATE courses SET
        course = :course
        WHERE id = :id';
    $s = $pdo->prepare($sql);
    $s->bindValue(':id', $_POST['id']);
    $s->bindValue(':course', $_POST['course']);
    $s->execute();
  }
  catch (PDOException $e)
  {
    $error = 'Error updating submitted course.';
    include 'error.html.php';
    exit();
  }

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

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

  // Delete the joke
  try
  {
    $sql = 'DELETE FROM courses WHERE id = :id';
    $s = $pdo->prepare($sql);
    $s->bindValue(':id', $_POST['id']);
    $s->execute();
  }
  catch (PDOException $e)
  {
    $error = 'Error deleting course.';
    include 'error.html.php';
    exit();
  }

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

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

managecourses.html.php

<?php include_once $_SERVER['DOCUMENT_ROOT'] . '/artgibney/includes/helpers.inc.php'; ?>
<!DOCTYPE html>
<html lang="en">
  <head>
  <style>
table,th,td
{
border:1px solid black;
border-collapse:collapse;
}
th, td
{
padding:6px;
}
        td { width: 50px; overflow: hidden; }
</style>
    <meta charset="utf-8">
    <title>Manage Courses and Learners</title>
  </head>
  <body>
    <h1>Manage Courses and Students</h1>
     <table>
     <tr>
  <th></th>
  <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="Edit"></td>
              <td><input type="submit" name="action" value="Delete"></td>
              <td><?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>

form.html.php

<?php include_once $_SERVER['DOCUMENT_ROOT'] .
    '/artgibney/includes/helpers.inc.php'; ?>
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title><?php htmlout($pageTitle); ?></title>
    <style type="text/css">
    textarea {
      display: block;
      width: 100%;
    }
    </style>
  </head>
  <body>
    <h1><?php htmlout($pageTitle); ?></h1>
    <form action="?<?php htmlout($action); ?>" method="post">
      <div>
        <label for="course">Course: <input type="text" id="course" name="course" value="<?php htmlout($course); ?>"></label>
      </div>
      <div>
        <input type="hidden" name="id" value="<?php
            htmlout($id); ?>">
        <input type="submit" value="<?php htmlout($button); ?>">
      </div>
    </form>
  </body>
</html>

I can’t see what is wrong with this code.
I would really appreciate if anyone could have a look at it and perhaps there is something obvious that I have done wrong.
Thanks,
Shane

first there are several severe HTML errors (<li> as child of <table>, <tr> as child of neither <table> nor <tbody>).

further there is no indication on what happens in helpers.inc.php as this is the only php file involved in the form submit.

also note that the value of a submit button is not necessarily submitted.

Dormilich,
Thanks for your reply.
The helpers.inc.php file just has the function htmlout() and more in it, that is all. Here it is anyway.

<?php
function html($text)
{
  return htmlspecialchars($text, ENT_QUOTES, 'UTF-8');
}

function htmlout($text)
{
  echo html($text);
}

function markdown2html($text)
{
  $text = html($text);

  // strong emphasis
  $text = preg_replace('/__(.+?)__/s', '<strong>$1</strong>', $text);
  $text = preg_replace('/\\*\\*(.+?)\\*\\*/s', '<strong>$1</strong>', $text);

  // emphasis
  $text = preg_replace('/_([^_]+)_/', '<em>$1</em>', $text);
  $text = preg_replace('/\\*([^\\*]+)\\*/', '<em>$1</em>', $text);

  // Convert Windows (\\r\
) to Unix (\
)
  $text = str_replace("\\r\
", "\
", $text);
  // Convert Macintosh (\\r) to Unix (\
)
  $text = str_replace("\\r", "\
", $text);

  // Paragraphs
  $text = '<p>' . str_replace("\
\
", '</p><p>', $text) . '</p>';
  // Line breaks
  $text = str_replace("\
", '<br>', $text);

  // [linked text](link URL)
  $text = preg_replace(
      '/\\[([^\\]]+)]\\(([-a-z0-9._~:\\/?#@!$&\\'()*+,;=%]+)\\)/i',
      '<a href="$2">$1</a>', $text);

  return $text;
}

function markdownout($text)
{
  echo markdown2html($text);
}

I can’t see any HTML errors with the table in form.html.php
Thanks,
Shane

I know that there is a bit of code missing in index.php.
In the if() statement ‘Edit’ I need to build the list of courses. The updated version is below.

<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/artgibney/includes/magicquotes.inc.php';
require_once $_SERVER['DOCUMENT_ROOT'] . '/artgibney/includes/access.inc.php';
if (!userIsLoggedIn())
{
	include '../login.html.php';
	exit();
}
if (!userHasRole('Content Editor'))
{
	$error = 'Only Content Editors may access this page.';
	include '../accessdenied.html.php';
	exit();
}

.....................................

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

  // Build the list of course rows
  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();
}

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

  try
  {
    $sql = 'SELECT id, course FROM courses WHERE id = :id';
    $s = $pdo->prepare($sql);
    $s->bindValue(':id', $_POST['id']);
    $s->execute();
  }
  catch (PDOException $e)
  {
    $error = 'Error fetching course row details.';
    include 'error.html.php';
    exit();
  }
  $row = $s->fetch();

  $pageTitle = 'Edit courses';
  $action = 'editformcourse';
  $course = $row['course'];
  $id = $row['id'];
  $button = 'Update course';

  // Build the list of course rows
  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 'form.html.php';
  exit();
}

if (isset($_GET['editformcourse']))
{
  include $_SERVER['DOCUMENT_ROOT'] . '/artgibney/includes/db.inc.php';
  
  try
  {
    $sql = 'UPDATE courses SET
        course = :course
        WHERE id = :id';
    $s = $pdo->prepare($sql);
    $s->bindValue(':id', $_POST['id']);
    $s->bindValue(':course', $_POST['course']);
    $s->execute();
  }
  catch (PDOException $e)
  {
    $error = 'Error updating submitted course.';
    include 'error.html.php';
    exit();
  }

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

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

  // Delete the course
  try
  {
    $sql = 'DELETE FROM courses WHERE id = :id';
    $s = $pdo->prepare($sql);
    $s->bindValue(':id', $_POST['id']);
    $s->execute();
  }
  catch (PDOException $e)
  {
    $error = 'Error deleting course.';
    include 'error.html.php';
    exit();
  }

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

include 'notes.html.php';

This code is copied from another form I use which does exactly the same thing but with a different table.
So why is it not working here.
What am i missing?
I have been through it all day.
Just to summarise, The problem that I am having is that the form is not being created when I hit ‘edit’ or ‘delete’.
Thanks,
Shane
PS The code is from the sitepoint book ‘PHP and MYSQL Novice to Ninja’ by Kevin Yank

I can’t see any HTML errors with the table in form.html.php

I wasn’t talking about form.html.php either.

You were mentioning Edit/Delete forms. the only one matching that is managecourses.html.php. and I have no idea where index.php comes into play so I have to assume that it doesn’t play any role at all.

Hi Domilich,
The index.php is definitely needed. For example when the managecourses.html.php runs, a user can hit the ‘Edit’ button. This should pass the value of ‘id’ and set the $_POST variable to ‘Edit’. Then in the index.php file, if this isset, so that the if (isset($_POST[‘action’]) and $_POST[‘action’] == ‘Edit’)) statement in index.php will run. Which in this case sets up the variables for the form and sets the $action variable so that when the ‘submit’ is pressed, another if() statement in index.php runs, this time, if(isset($_POST(‘editformcourse’)) which updates the database with the new data from the form.html.php. So index.php is absolutely integral to setting up form.html.php and to getting data to and from the database.
Shane

In managecourses.html.php, is this line correct?


          <form action="" method="post">

Doesn’t that mean that on submit it will open itself, rather than the index page?

Hi droopsnoot,
You are absolutely correct, this is where the problem is.
I put in index.php and it is working.

<form action="index.php" method="post"> 

Not quite doing what I’d like but at least it is working.
I would like it to return to the managecourses.html.php after clicking in ‘Delete’ and after clicking on ‘Submit’ in the form.html.php.
But at least it is going to the form now and I can edit and delete the data.
Thanks for your help, very much appreciated.
Shane

you could redirect to managecourses after your edit/delete is done.

Hi Dormilich,
That works! Thanks, using

  header('Location: managecourses.html.php');

at the end of the if() statement which sends to data to the DB after the edit form has been submitted.
For example,

if (isset($_GET['editformlearner']))
{
  include $_SERVER['DOCUMENT_ROOT'] . '/artgibney/includes/db.inc.php';
  
  try
  {
    $sql = 'UPDATE learners SET
        learner = :learner
        WHERE id = :id';
    $s = $pdo->prepare($sql);
    $s->bindValue(':id', $_POST['id']);
    $s->bindValue(':learner', $_POST['learner']);
    $s->execute();
  }
  catch (PDOException $e)
  {
    $error = 'Error updating submitted learner.';
    include 'error.html.php';
    exit();
  }

  header('Location: managecourses.html.php');
  exit();
}

That is perfect now, doing exactly what I want.
Thanks,
Shane