Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    kdm
    kdm is offline
    New to the CF scene
    Join Date
    Apr 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Edit form not working

    I'm sorry if this is really obvious or stupid but I'm having a major issue with this. I have a form where the user can both add and edit data that's stored in a database. This is the code for the form:
    Code:
    <?php include_once $_SERVER['DOCUMENT_ROOT'] .
        '/stats/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>
      <div align="center">
      <?php /*include header*/include $_SERVER['DOCUMENT_ROOT'] .
    '/stats/includes/header.inc.html.php'; ?>
        <h1><?php htmlout($pageTitle); ?></h1>
        <form action="?<?php htmlout($action); ?>" method="post">
          <div>
            <label for="hometeam">Home Team:</label>
            <select name="hometeam" id="hometeam">
              <option value="">Select one</option>
              <?php foreach ($hometeams as $team): ?>
                <option value="<?php htmlout($team['id']); ?>"<?php
                    if ($team['id'] == $hometeam)
                    {
                      echo ' selected';
                    }
                    ?>><?php htmlout($team['name']); ?></option>
              <?php endforeach; ?>
            </select>
          </div>
          <div>
            <label for="awayteam">Away Team:</label>
            <select name="awayteam" id="awayteam">
              <option value="">Select one</option>
              <?php foreach ($awayteams as $team): ?>
                <option value="<?php htmlout($team['id']); ?>"<?php
                    if ($team['id'] == $awayteam)
                    {
                      echo ' selected';
                    }
                    ?>><?php htmlout($team['name']); ?></option>
              <?php endforeach; ?>
            </select>
          </div>
          <div>
            <label for="ground">Ground:</label>
            <select name="ground" id="ground">
              <option value="">Select one</option>
              <?php foreach ($grounds as $team): ?>
                <option value="<?php htmlout($team['id']); ?>"<?php
                    if ($team['id'] == $ground)
                    {
                      echo ' selected';
                    }
                    ?>><?php htmlout($team['name']); ?></option>
              <?php endforeach; ?>
            </select>
          </div>
          <div>
            <label for="format">Format:</label>
            <select name="format" id="format">
              <option value="">Select one</option>
              <?php foreach ($formats as $team): ?>
                <option value="<?php htmlout($team['id']); ?>"<?php
                    if ($team['id'] == $format)
                    {
                      echo ' selected';
                    }
                    ?>><?php htmlout($team['name']); ?></option>
              <?php endforeach; ?>
            </select>
          </div>
          <div>
          	<label for="season">Season:<input type="number" name="season"
            	id="season" value="<?php htmlout($season); ?>"></label>
          </div>
           <div>
          	<label for="round">Round:<input type="text" name="round"
            	id="round" value="<?php htmlout($round); ?>"></label>
          </div> 
          <div>
            <label for="winner">Winner:</label>
            <select name="winner" id="winner">
              <option value="">Select one</option>
              <?php foreach ($winners as $team): ?>
                <option value="<?php htmlout($team['id']); ?>"<?php
                    if ($team['id'] == $winner)
                    {
                      echo ' selected';
                    }
                    ?>><?php htmlout($team['name']); ?></option>
              <?php endforeach; ?>
            </select>
          </div>
          <div>
          	<label for="margin">Margin:<input type="number" name="margin"
            	id="margin" value="<?php htmlout($margin); ?>"></label>
            <label for="victorytype"></label>
            <select name="victorytype" id="victorytype">
              <option value="" disabled="disabled">Select one</option>
              <option value="Draw">Draw</option>
              <?php foreach ($victorytypes as $team): ?>
                <option value="<?php htmlout($team['id']); ?>"<?php
                    if ($team['id'] == $victorytype)
                    {
                      echo ' selected';
                    }
                    ?>><?php htmlout($team['victorytype']); ?></option>
              <?php endforeach; ?>
            </select>
          </div> 
          <div>
            <label for="motm">Man Of The Match:</label>
            <select name="motm" id="motm">
              <option value="">Select one</option>
              <?php foreach ($players as $ground): ?>
                <option value="<?php htmlout($ground['id']); ?>"<?php
                    if ($ground['id'] == $motm)
                    {
                      echo ' selected';
                    }
                    ?>><?php echo($ground['usertitle']); ?></option>
              <?php endforeach; ?>
            </select>
          </div>
          <div>
            <input type="hidden" name="id" value="<?php
                htmlout($id); ?>">
            <input type="submit" value="<?php htmlout($button); ?>"><input type="button" value="Cancel" onClick="history.go(-1);return true;">
          </div>
        </form>
        <?php /*include footer*/include $_SERVER['DOCUMENT_ROOT'] .
    '/stats/includes/footer.inc.html.php'; ?>
    </div>
      </body>
    </html>
    And that's all good. Then I have the script for database interactivity and I can add perfectly fine. This is the code for the add form:
    Code:
    <?php
    include_once $_SERVER['DOCUMENT_ROOT'] .
        '/stats/includes/magicquotes.inc.php';
    
    if (isset($_GET['add']))
    {
      $pageTitle = 'New Match';
      $action = 'addform';
      $hometeam = '';
      $awayteam = '';
      $ground = '';
      $format = '';
      $season = '';
      $round = '';
      $winner = '';
      $margin = '';
      $victorytype = '';
      $motm = '';
      $id = '';
      $button = 'Add match';
    
      include $_SERVER['DOCUMENT_ROOT'] . '/stats/includes/db.inc.php';
    
      // Build the list of teams
      try
      {
        $result = $pdo->query('SELECT id, name FROM cricket_teams');
      }
      catch (PDOException $e)
      {
        $error = 'Error fetching list of teams.';
        include 'searchform.html.php';
        exit();
      }
    
      foreach ($result as $row)
      {
        $hometeams[] = array('id' => $row['id'], 'name' => $row['name']);
    	$awayteams[] = array('id' => $row['id'], 'name' => $row['name']);
    	$winners[] = array('id' => $row['id'], 'name' => $row['name']);
      }
    // Build the list of grounds
      try
      {
        $result = $pdo->query('SELECT id, name FROM cricket_grounds');
      }
      catch (PDOException $e)
      {
        $error = 'Error fetching list of grounds.';
        include 'searchform.html.php';
        exit();
      }
      
       foreach ($result as $row)
       {
    	$grounds[] = array('id' => $row['id'], 'name' => $row['name']);
       }
       
       // Build the list of formats
      try
      {
        $result = $pdo->query('SELECT id, name FROM cricket_formats');
      }
      catch (PDOException $e)
      {
        $error = 'Error fetching list of formats.';
        include 'searchform.html.php';
        exit();
      }
      
       foreach ($result as $row)
       {
    	$formats[] = array('id' => $row['id'], 'name' => $row['name']);
       }
       
      // Build the list of players
      try
      {
        $result = $pdo->query('SELECT id, user.usertitle FROM cricket_players INNER JOIN user ON user.userid=cricket_players.id');
      }
      catch (PDOException $e)
      {
        $error = 'Error fetching list of players.';
        include 'searchform.html.php';
        exit();
      }
      
       foreach ($result as $row)
       {
    	$players[] = array('id' => $row['id'], 'usertitle' => $row['usertitle']);
       }
       
       //Build the list of victory types
       try
       {
    	   $result = $pdo->query('SELECT distinct victorytype, victorytype as id FROM cricket_matches');
       }
       catch (PDOException $e)
       {
    	   $error = 'Error';
    	   include 'searchform.html.php';
    	   exit();
       }
       foreach ($result as $row)
       {
    	   $victorytypes[] = array('id' => $row['id'], 'victorytype' => $row['victorytype']);
       }
       
      include 'form.html.php';
      exit();
    }
    
    if (isset($_GET['addform']))
    {
      include $_SERVER['DOCUMENT_ROOT'] . '/stats/includes/db.inc.php';
    
      if ($_POST['hometeam'] == '')
      {
        $error = 'You must choose a home team for this match.
            Click &lsquo;back&rsquo; and try again.';
        include 'searchform.html.php';
        exit();
      }
    
      try
      {
        $sql = 'INSERT INTO cricket_matches SET
            hometeam = :hometeam,
            awayteam = :awayteam,
    		ground = :ground,
    		format = :format,
    		season = :season,
    		round = :round,
    		winner = :winner,
    		margin = :margin,
    		victorytype = :victorytype,
    		motm = :motm';
        $s = $pdo->prepare($sql);
        $s->bindValue(':hometeam', $_POST['hometeam']);
        $s->bindValue(':awayteam', $_POST['awayteam']);
    	$s->bindValue(':ground', $_POST['ground']);
    	$s->bindValue(':format', $_POST['format']);
    	$s->bindValue(':season', $_POST['season']);
    	$s->bindValue(':round', $_POST['round']);
    	$s->bindValue(':winner', $_POST['winner']);
    	$s->bindValue(':margin', $_POST['margin']);
    	$s->bindValue(':victorytype', $_POST['victorytype']);
    	$s->bindValue(':motm', $_POST['motm']);
        $s->execute();
      }
      catch (PDOException $e)
      {
        $error = 'Error adding submitted match.';
        include 'searchform.html.php';
        exit();
      }
    
      header('Location: .');
      exit();
    }
    Again, it all works fine. However when I try to edit, it comes up as a blank form, looking exactly as it would if I was adding new. Here's the edit code (follows straight on from the add above in the file):
    Code:
    if (isset($_POST['action']) and $_POST['action'] == 'Edit')
    {
      include $_SERVER['DOCUMENT_ROOT'] . '/stats/includes/db.inc.php';
    
      try
      {
        $sql = 'SELECT id, hometeam, awayteam, ground, format, season, round, winner, margin, victorytype, motm FROM cricket_matches WHERE id = :id';
        $s = $pdo->prepare($sql);
        $s->bindValue(':id', $_POST['id']);
        $s->execute();
      }
      catch (PDOException $e)
      {
        $error = 'Error fetching match details.';
        include 'searchform.html.php';
        exit();
      }
      
      $row = $s->fetch();
    
      $pageTitle = 'Edit Match';
      $action = 'editform';
      $hometeam = $row['hometeam'];
      $awayteam = $row['awayteam'];
      $ground = $row['ground'];
      $format = $row['format'];
      $season = $row['season'];
      $round = $row['round'];
      $winner = $row['winner'];
      $margin = $row['margin'];
      $victorytype = $row['victorytype'];
      $motm = $row['motm'];
      $id = $row['id'];
      $button = 'Update match';
      
      // Build the list of teams
      try
      {
        $result = $pdo->query('SELECT id, name FROM cricket_teams');
      }
      catch (PDOException $e)
      {
        $error = 'Error fetching list of teams.';
        include 'searchform.html.php';
        exit();
      }
    
      foreach ($result as $row)
      {
        $hometeams[] = array('id' => $row['id'], 'name' => $row['name']);
    	$awayteams[] = array('id' => $row['id'], 'name' => $row['name']);
    	$winners[] = array('id' => $row['id'], 'name' => $row['name']);
      }
      // Build the list of grounds
      try
      {
        $result = $pdo->query('SELECT id, name FROM cricket_grounds');
      }
      catch (PDOException $e)
      {
        $error = 'Error fetching list of grounds.';
        include 'searchform.html.php';
        exit();
      }
      
       foreach ($result as $row)
       {
    	$grounds[] = array('id' => $row['id'], 'name' => $row['name']);
       }
       
       // Build the list of formats
      try
      {
        $result = $pdo->query('SELECT id, name FROM cricket_formats');
      }
      catch (PDOException $e)
      {
        $error = 'Error fetching list of formats.';
        include 'searchform.html.php';
        exit();
      }
      
       foreach ($result as $row)
       {
    	$formats[] = array('id' => $row['id'], 'name' => $row['name']);
       }
       
       // Build the list of players
      try
      {
        $result = $pdo->query('SELECT id, user.usertitle FROM cricket_players INNER JOIN user ON cricket_players.username=user.userid');
      }
      catch (PDOException $e)
      {
        $error = 'Error fetching list of players.';
        include 'searchform.html.php';
        exit();
      }
      
       foreach ($result as $row)
       {
    	$players[] = array('id' => $row['id'], 'usertitle' => $row['usertitle']);
       }
       
       //Build the list of victory types
       try
       {
    	   $result = $pdo->query('SELECT distinct victorytype, victorytype as id FROM cricket_matches');
       }
       catch (PDOException $e)
       {
    	   $error = 'Error';
    	   include 'searchform.html.php';
    	   exit();
       }
       foreach ($result as $row)
       {
    	   $victorytypes[] = array('id' => $row['id'], 'victorytype' => $row['victorytype']);
       }
    	
      include 'form.html.php';
      exit();
    }
    
    if (isset($_GET['editform']))
    {
      include $_SERVER['DOCUMENT_ROOT'] . '/stats/includes/db.inc.php';
    
      if ($_POST['hometeam'] == '')
      {
        $error = 'You must choose a hometeam for this match.
            Click &lsquo;back&rsquo; and try again.';
        include 'searchform.html.php';
        exit();
      }
    
      try
      {
        $sql = 'UPDATE cricket_matches SET
            hometeam = :hometeam,
            awayteam = :awayteam,
    		ground = :ground,
    		format = :format,
    		season = :season,
    		round = :round,
    		winner = :winner,
    		margin = :margin,
    		victorytype = :victorytype,
    		motm = :motm
            WHERE id = :id';
        $s = $pdo->prepare($sql);
        $s->bindValue(':id', $_POST['id']);
        $s->bindValue(':hometeam', $_POST['hometeam']);
        $s->bindValue(':awayteam', $_POST['awayteam']);
    	$s->bindValue(':ground', $_POST['ground']);
    	$s->bindValue(':format', $_POST['format']);
    	$s->bindValue(':season', $_POST['season']);
    	$s->bindValue(':round', $_POST['round']);
    	$s->bindValue(':winner', $_POST['winner']);
    	$s->bindValue(':margin', $_POST['margin']);
    	$s->bindValue(':victorytype', $_POST['victorytype']);
    	$s->bindValue(':motm', $_POST['motm']);
        $s->execute();
      }
      catch (PDOException $e)
      {
        $error = 'Error updating submitted match.';
        include 'searchform.html.php';
        exit();
      }
    
      header('Location: .');
      exit();
    }
    
    if (isset($_POST['action']) and $_POST['action'] == 'Delete')
    {
      include $_SERVER['DOCUMENT_ROOT'] . '/stats/includes/db.inc.php';
    
      // Delete the joke
      try
      {
        $sql = 'DELETE FROM matches WHERE id = :id';
        $s = $pdo->prepare($sql);
        $s->bindValue(':id', $_POST['id']);
        $s->execute();
      }
      catch (PDOException $e)
      {
        $error = 'Error deleting match.';
        include 'searchform.html.php';
        exit();
      }
    
      header('Location: .');
      exit();
    }
    Any help would be very much appreciated, it's probably something really obvious but I'm pulling my hair out over this

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,608
    Thanks
    24
    Thanked 647 Times in 646 Posts
    I really do not see anything that will allow you to delete from a database in your form. You should have radio boxes to select this for you.
    Also this:
    PHP Code:
    if (isset($_POST['action']) and $_POST['action'] == 'Edit'
    should not be used. You send info identified by a name attribute. 'action' in your form is not a information attribute, but the name of the php file you are sending the form to. Pick something different, like 'del' and 'ed' for the buttons:
    PHP Code:
    <input type="radio" name="group1" value="ed"Edit<br>
    <
    input type="radio" name="group1" value="del"Delete<br>
    <
    input type="radio" name="group1" value="upd" checkedUpdate<br
    And in the php file:
    PHP Code:
    <?php
    if (isset($_POST['group1'])){
        if(
    $_POST['group1'] == 'ed'){Do your edit stuff here}
        if(
    $_POST['group1'] == 'del'){Do your delete stuff here}
        if(
    $_POST['group1'] == 'upd'){Do your update stuff here}
    }
    ?>
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •