Several forms in a table and their id's [SOLVED]

Hi,
I have an HTML table with several submit buttons that pass an id to my controller. But I don’t seem to be sending the correct id’s. I can’t see what is wrong with the form.
managetopic.html.php

<?php include_once $_SERVER['DOCUMENT_ROOT'] . '/artgibney/includes/helpers.inc.php'; 
      include_once $_SERVER['DOCUMENT_ROOT'] . '/artgibney/includes/func.inc.php';
      include $_SERVER['DOCUMENT_ROOT'] . '/artgibney/includes/buildtopics.inc.php';
      include $_SERVER['DOCUMENT_ROOT'] . '/artgibney/includes/buildsubtopics.inc.php';?>
<!DOCTYPE html>
<html lang="en">
  <head>
  <style>
table,th,td
{
border-collapse:collapse;
}
th, td
{
padding:6px;
}
        td { vertical-align:top; text-align: center; width: 40px; overflow: hidden;  white-space: nowrap;}
        tr.separated {
              border-top: 1px solid black;
                     }
</style>
    <meta charset="utf-8">
    <title>Manage Topics and sub-topics</title>
  </head>
  <body>
      <p><a href="..">physCMS home</a> &#8658; <a href=".">Manage Teaching Points</a></p> 
      <?php include $_SERVER['DOCUMENT_ROOT'] . '/artgibney/includes/logout.inc.html.php'; ?>
    <h1>Manage Topics and Sub-topics</h1>
    <form action="index.php" method="post">
     <table>
     <tr>
      <th></th>
      <th></th>
      <th>Topics</th>
      <th></th>
      <th></th>
      <th>Sub-topics</th>
    </tr>
      <?php foreach (array_reverse($topics) as $topic): 
               $i = 0;
                  foreach (array_reverse($subtopics) as $subtopic): 
                       if($topic['id']==$subtopic['topicid']): ?>
            <tr <?php if($i==0): ?> class="separated" <?php endif; ?> >
                 <input type="hidden" name="id" value="<?php echo $topic['id']; ?>">
              <td> 
                  <?php if($i == 0): ?> <input type="submit" name="action" value="Edittopic"> <?php endif; ?>  
              </td>     
              <td>
                  <?php if($i == 0): ?> <input type="submit" name="action" value="Deletetopic"> <?php endif; ?> 
              </td>
              <td> 
                  <?php if($i == 0):
                            htmlout($topic['topic']);
                            $i++;
                            endif; ?>
              </td>
                  <input type="hidden" name="id" value="<?php echo $subtopic['id']; ?>">
              <td><input name="action" type="submit" value="Editsubtopic"></td>
              <td><input name="action" type="submit" value="Deletesubtopic"></td>
              <td>           
                  <?php htmlout($subtopic['subtopic']);?> 
              </td>
            </tr>  
                     <?php   endif; 
                 endforeach;  
         endforeach; ?>
    </table>
   </form>
  </body>
</html> 

subtopics table looks like this,

The topics table looks like this,

The form itself looks like this,

Here is an excerpt from the index.php controller file.

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

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

  $pageTitle = 'Edit a row of the topic table';
  $action = 'edittopicform';
  $name = $row['topic'];
  $id = $row['id'];
  $formid = 'topic';
  $button = 'Update topic';
 
  include 'topicform.html.php';
  exit();
}

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

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

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

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

  exit();
}

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

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

  $pageTitle = 'Edit a row of the sub-topic table';
  $action = 'editformsubtopic';
  $name = $row['subtopic'];
  $id = $row['id'];
  $formid = 'subtopic';
  $button = 'Update sub-topic';

  // Build the list of sub-topic rows
  try
  {
    $result = $pdo->query('SELECT id, subtopic FROM subtopics');
  }
  catch (PDOException $e)
  {
    $error = 'Error fetching list of subtopics.';
    include 'error.html.php';
    echo $e;
    exit();
  }

  foreach ($result as $row)
  {
    $subtopics[] = array('id' => $row['id'], 'subtopic' => $row['subtopic']);
  }

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

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

  header('Location: ?topics');
  exit();
}

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

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

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

Any help would be greatly appreciated,
Thanks,
Shane

Did you mean to $preserve_keys
http://php.net/manual/en/function.array-reverse.php

Hi,

No I don’t think so. I was just reversing the array so that the last input is at the top. But it is not important.
Thanks,
Shane

Previously I had this form as two tables, but when I put them together I have problems. When I click edit or delete, a different topic or subtopic is edited or deleted.
This is what i had before

<table>
     <tr>
      <th></th>
      <th></th>
      <th>Topics</th>
    </tr>
      <?php foreach (array_reverse($topics) as $topic): ?>
        <li>
          <form action="index.php" method="post">
            <tr>
              <input type="hidden" name="id" value="<?php
                  echo $topic['id']; ?>">
              <td><input type="submit" name="action" value="Edittopic"></td>
              <td><input type="submit" name="action" value="Deletetopic"></td>
              <td><?php htmlout($topic['topic']);?></td>
            </tr>
          </form>
        </li>
      <?php endforeach; ?>
    </table>
    </p>
        <? if(countRows('subtopics')): ?>
    <p>
         <table>
         <tr>
      <th></th>
      <th></th>
      <th>Sub-topics</th>
    </tr>
      <?php foreach (array_reverse($subtopics) as $subtopic): ?>
        <li>
          <form action="index.php" method="post">
            <tr>
              <input type="hidden" name="id" value="<?php
                  echo $subtopic['id']; ?>">
              <td><input name="action" type="submit" value="Editsubtopic"></td>
              <td><input name="action" type="submit" value="Deletesubtopic"></td>
              <td><?php htmlout($subtopic['subtopic']);?></td>
            </tr>
          </form>
        </li>
      <?php endforeach; ?>
    </table>
    </p>
          <? endif; ?>

Which looked like this, giving the information in two tables,

But in the first post you can see that it displays the topic along with it’s associated subtopics.
Could the problem be with the name=“id” being the same for the ‘edit’ and ‘delete’ buttons for both topic and subtopic?
Shane

That is very likely, having things with the same name makes it easier to miss problems and more difficult to debug.

Do an print_r() on the post array and echo out $topics[‘id’] and $subtopics[‘id’] in a loop too see what’s being stored in your hidden field.

Yes thanks, good idea. I will do that.
Seems to be only dealing with the id in the last row of each array.
But I will see what output your suggestion gives.
Thanks,
Shane

Post the selection query your using to get the data for the topics and subtopics too.

Hi,
I have it working now. I split the form into two forms within the same table and made a few other non related changes.
But first I used your suggestion,

This is how the form looks now,


The managetopics.html.php

<?php include_once $_SERVER['DOCUMENT_ROOT'] . '/artgibney/includes/helpers.inc.php'; 
      include_once $_SERVER['DOCUMENT_ROOT'] . '/artgibney/includes/func.inc.php';
      include $_SERVER['DOCUMENT_ROOT'] . '/artgibney/includes/buildtopics.inc.php';
      include $_SERVER['DOCUMENT_ROOT'] . '/artgibney/includes/buildsubtopics.inc.php';?>
<!DOCTYPE html>
<html lang="en">
  <head>
  <style>
table,th,td
{
border-collapse:collapse;
}
th, td
{
padding:6px;
}
        td { vertical-align:top; text-align: center; width: 40px; overflow: hidden;  white-space: nowrap;}
        tr.separated {
              border-top: 1px solid black;
                     }
</style>
    <meta charset="utf-8">
    <title>Manage Topics and sub-topics</title>
  </head>
  <body>
      <p><a href="..">physCMS home</a> &#8658; <a href=".">Manage Teaching Points</a></p> 
      <?php include $_SERVER['DOCUMENT_ROOT'] . '/artgibney/includes/logout.inc.html.php'; ?>
          <p><small><font color="red">Warning:</font> Topics are only viewable if subtopics exist and are associated. Deleting a topic will delete all associated sub-topics.</small></p>
    <h2>Manage Topics and Sub-topics</h2>
    <p><a href="?addtopicform">Add Topic</a>   
    <?php if(countRows('topics')): ?>
     &#8658; <a href="?addsubtopicform">Add sub-topic</a>
     <?php endif; ?>
              <?php if((countRows('topics')) && (countRows('subtopics'))): ?>
     <table>
     <tr>
      <th></th>
      <th></th>
      <th>Topics</th>
      <th></th>
      <th></th>
      <th>Sub-topics</th>
      </tr>
      <?php foreach (array_reverse($topics) as $topic): 
               $i = 0;
                  foreach (array_reverse($subtopics) as $subtopic): 
                       if($topic['id']==$subtopic['topicid']): ?>
            <tr <?php if($i==0): ?> class="separated" <?php endif; ?> >
                     <form action="index.php" method="post">
                 <input type="hidden" name="id" value="<?php echo $topic['id']; ?>">
              <td> 
                  <?php if($i == 0): ?> <input type="submit" name="action" value="Edittopic"> <?php endif; ?>
              </td>     
              <td>
                  <?php if($i == 0): ?> <input type="submit" name="action" value="Deletetopic"> <?php endif; ?>
              </td>
              <td> 
                  <?php if($i == 0):
                            htmlout($topic['topic']);
                            $i++;
                            endif; ?>
              </td>
                     </form>
                     <form action="index.php" method="post">
                  <input type="hidden" name="id" value="<?php echo $subtopic['id']; ?>">
              <td><input name="action" type="submit" value="Editsubtopic"></td>
              <td><input name="action" type="submit" value="Deletesubtopic"></td>
              <td>           
                  <?php htmlout($subtopic['subtopic']);?>
              </td>
                     </form>
            </tr>  
                     <?php endif; ?>
                 <?php endforeach;  
              endforeach; ?>
    </table>
    <?php endif; ?>
  </body>
</html>

and the index.php

if (isset($_GET['topics']))
{
  include $_SERVER['DOCUMENT_ROOT'] . '/artgibney/includes/db.inc.php';
  
  include 'managetopics.html.php';
  exit();
}

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

  try
  {
    $sql = 'INSERT INTO topics SET
        topic = :topic,
        id = :id';
    $s = $pdo->prepare($sql);
    $s->bindValue(':topic', $_POST['topic']);
    $s->bindValue(':id', $_POST['id']);
    $s->execute();
  }
  catch (PDOException $e)
  {
    $error = 'Error adding submitted topic.';
    include 'error.html.php';
    echo $e;
    exit();
  }
  header('Location: .');
  exit();
}

if (isset($_GET['addtopicform']))
{
  $pageTitle = 'New Topic';
  $action = 'addtopic';
  $topic = '';
  $id = '';
  $button = 'Add topic';

  include $_SERVER['DOCUMENT_ROOT'] . '/artgibney/includes/db.inc.php';

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

if (isset($_GET['addsubtopicform']))
{
  $pageTitle = 'New sub-topic';
  $action = 'addsubtopic';
  $text = '';
  $topicid = '';
  $id = '';
  $button = 'Add sub-topic';

  include $_SERVER['DOCUMENT_ROOT'] . '/artgibney/includes/db.inc.php';

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

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

    if ($_POST['topic'] == '')
  {
    $error = 'You must choose a topic for this sub-topic.
        Click &lsquo;back&rsquo; and try again.';
    include 'error.html.php';
    exit();
  }

  try
  {
    $sql = 'INSERT INTO subtopics SET
        subtopic = :subtopic,
        topicid = :topicid';
    $s = $pdo->prepare($sql);
    $s->bindValue(':subtopic', $_POST['text']);
    $s->bindValue(':topicid', $_POST['topic']);
    $s->execute();
  }
  catch (PDOException $e)
  {
    $error = 'Error adding submitted subtopic.';
    include 'error.html.php';
    echo $e;
    exit();
  }

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

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

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

  $pageTitle = 'Edit a row of the topic table';
  $action = 'edittopicform';
  $name = $row['topic'];
  $id = $row['id'];
  $formid = 'topic';
  $button = 'Update topic';
 
  include 'topicform.html.php';
  exit();
}

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

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

  // Delete the topic
  try
  {
    $sql = 'DELETE FROM topics WHERE id = :id';
    $s = $pdo->prepare($sql);
    $s->bindValue(':id', $_POST['id']);
    $s->execute();
  }
  catch (PDOException $e)
  {
    $error = 'Error deleting topic.';
    include 'error.html.php';
    exit();
  }
  
    // Delete all associated subtopics of a topic
  try
  {
    $sql = 'DELETE FROM subtopics WHERE topicid = :topicid';
    $s = $pdo->prepare($sql);
    $s->bindValue(':topicid', $_POST['id']);
    $s->execute();
  }
  catch (PDOException $e)
  {
    $error = 'Error deleting associated subtopics of topics.';
    include 'error.html.php';
    echo $e;
    exit();
  }

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

  exit();
}

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

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

  $pageTitle = 'Edit a row of the sub-topic table';
  $action = 'editformsubtopic';
  $name = $row['subtopic'];
  $id = $row['id'];
  $formid = 'subtopic';
  $button = 'Update sub-topic';

  // Build the list of sub-topic rows
  try
  {
    $result = $pdo->query('SELECT id, subtopic FROM subtopics');
  }
  catch (PDOException $e)
  {
    $error = 'Error fetching list of subtopics.';
    include 'error.html.php';
    echo $e;
    exit();
  }

  foreach ($result as $row)
  {
    $subtopics[] = array('id' => $row['id'], 'subtopic' => $row['subtopic']);
  }

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

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

  header('Location: ?topics');
  exit();
}

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

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

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

Thanks for your help and for looking into this,
Shane

Is everything working the way you want it to? In these situation just remember to output the variable or array to see what values they hold.

It is all working great now and I tidied up the buttons giving them better names using

          <td><button name="action" type="submit" value="Editsubtopic">Edit</button></td>
          <td><button name="action" type="submit" value="Deletesubtopic">Delete</button></td>

Thanks,
Shane

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.