Kevin Yanks PHP Chapter 7 -Admin - Authors

I’m working through the Kevin Yanks PHP book and am stuck at Chapter 7, Admin, Authors. The add screen displays and works well, but the Update function does not display the screen after clicking the EDIT button. I’m sure I’m missing something, but not sure what (using PDO).

If you post your code here, we could take a look at it and see where there might be a problem. Just highlight the code you have pasted and use the </> icon above to format your code for easier reading.

As I said, the add authors presents the form correctly (I appreciate your insights)

//          ///// EDIT AUTHORS //////
// if (isset($_GET['Edit']))

 if (isset($_POST['action']) && $_POST['action'] == 'Edit')
{
  include $_SERVER['DOCUMENT_ROOT'] . '/includes/db.inc.php';
//  
try {   
$dbh = new PDO($hostname, $username, $password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    } 
catch (PDOException $e) 
{
    echo 'Initial PDO Connection failed: ' . $e->getMessage().'<br><br>';
}
//
//Connecting to your database
  try
  {
    $sql = "SELECT id, name, email FROM author WHERE id = '$id'";
    $stmt = $dbh->prepare($sql);
    $stmt->bindValue(':id', $_POST['id']);
    $stmt->execute();
  }
  catch (PDOException $e)
  {
    $error = 'Error fetching author details.';
    include 'error.html.php';
    exit();
  }
  $row = $stmt->fetch();

  $pageTitle = 'Edit Author';
  $action = 'editform';
  $name = $row['name'];
  $email = $row['email'];
  $id = $row['id'];
  $button = 'Update author'; 

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

if (isset($_GET['editform']))
try
  {
    $sql = 'UPDATE author SET
        name = :name,
        email = :email
        WHERE id = :id';
    $stmt = $pdo->prepare($sql);
    $stmt->bindValue(':id', $_POST['id']);
    $stmt->bindValue(':name', $_POST['name']);
    $stmt->bindValue(':email', $_POST['email']);
    $stmt->execute();
  }
  catch (PDOException $e)
  {
    $error = 'Error updating submitted author.';
    include 'error.html.php';
    exit();
  }

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

The block contained in the conditional

if (isset($_GET['editform']))

should be enclosed in { }

if (isset($_GET['editform'])) {
   // Some code here
}

The mixed use of GET and POST has confused more than one of the books readers.

The idea being that GET only gets stuff while POST does stuff.

Ok, let me play with that a bit. I appreciate your advice.

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