Page 1 of 1

Two same buttons, two different results

Posted: Fri Jun 26, 2009 4:59 pm
by pkunzipula
Hello Experts!

I have been grinding away at one module for the past week, and I'm down to just a handful of problems.

One of which is this: I have a minor CMS with a page for Adding stories and another for Editing stories. Each has two button -- Submit and Cancel, both of which should do their thing and return them to the main story list page.

My Edit page works perfectly. My Add page Cancel button does nothing at all. Form HTML is identical, but the PHP is different, though I don't have the wherewithal to know what is affecting it so.

Here is my Edit (working) PHP code:

Code: Select all

<?php
include('includes/connection.inc.php');
include('includes/corefuncs.inc.php');
// remove backslashes
nukeMagicQuotes();
// initialize flags
$OK = false;
$done = false;
 // create database connection
$conn = dbConnect('admin');
// get details of selected record
if (isset($_GET['storyID']) && !$_POST) {
  // prepare SQL query
  $sql = 'SELECT storyID, headline, story
          FROM news WHERE storyID = ?';
  // initialize statement
  $stmt = $conn->stmt_init();
  if ($stmt->prepare($sql)) {
    // bind the query parameters
    $stmt->bind_param('i', $_GET['storyID']);
    // bind the results to variables
    $stmt->bind_result($storyID, $headline, $story);
    // execute the query, and fetch the result
    $OK = $stmt->execute();
    $stmt->fetch();
    }
  }
  
// if form has been submitted, update record
if (array_key_exists('update', $_POST)) {
  $sql = 'UPDATE news SET headline = ?, story = ?
          WHERE storyID = ?';
  $stmt = $conn->stmt_init();
  if ($stmt->prepare($sql)) {
    $stmt->bind_param('ssi', $_POST['headline'], $_POST['story'], $_POST['storyID']);
    $done = $stmt->execute();
    }
}
 
// redirect page on success or if $_GET['storyID']) not defined
[b]if ($done || array_key_exists('cancel_edit',$_POST) || !isset($_GET['storyID'])) {
  header('Location: http://localhost/Cougars/storyList.php');[/b]  exit;
  }
// display error message if query fails
if (isset($stmt) && !$OK && !$done) {
  echo $stmt->error;
  }
?>
And here is my Add (broken) code

Code: Select all

<?php
if (array_key_exists('insert',$_POST)) {
include('includes/connection.inc.php');
include('includes/corefuncs.inc.php');
//remove backslashes
nukeMagicQuotes();
//initialize flag
$OK = false;
//create database connection
$conn = dbConnect('admin');
//create SQL
$sql = 'INSERT INTO news (headline, story, created)
        VALUES(?,?,NOW())';
//initialize prepared statement
$stmt = $conn->stmt_init();
if ($stmt->prepare($sql)) {
    //bind parameters and execute statement
    $stmt->bind_param('ss',$_POST['headline'],$_POST['story']);
    $OK = $stmt->execute();
    }
[b]//redirect if successful or displayerror
if ($OK || array_key_exists('cancel_insert',$_POST)) {[/b]  
header('Location: http://localhost/Cougars/storyList.php');
    exit;
    }
    else {
        echo $stmt->error;
        }
    }
?> 


I appreciate any help, because I hope to someday answer and not just ask.

Arlen

Re: Two same buttons, two different results

Posted: Fri Jun 26, 2009 5:12 pm
by pkunzipula
WAIT! I think I figured this one out. Maybe I should just walk away from the computer once in awhile.