SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Aug 2013
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Cannot return to main controller

    I have a form to add new author or edit existing author. The problem is that it will direct me to another directory wherein it should return to the main index controller as specified in the
    Code:
    header('Location: .');
    I examined the code and it looks fine. I'm wondering why am not getting the desired result. Any inputs are highly appreciated.

    Please see below code for your reference:

    index.php
    PHP Code:
    <?php
    //error_reporting(-1);
    //ini_set('display_errors', 1);  
    include_once $_SERVER['DOCUMENT_ROOT'] . '/includes/magicquotes.inc.php';

    //Display author list
    include_once $_SERVER['DOCUMENT_ROOT'] . '/includes/db.inc.php';
    try {
        
    $result $pdo->query('SELECT id, name FROM author');
    } catch (
    PDOException $e){
        
    $error 'Error fetching authors from database! ' $e->getMessage();
        include 
    'error.html.php';
        exit();


    //Loads the form in 'new author' mode
    if (isset($_GET['add']))
    {
        
    $pageTitle 'New Author';
        
    $action 'addform';
        
    $name '';
        
    $email '';
        
    $id '';
        
    $button 'Add Author';

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

    //Inserting new author using the add form
    if (isset($_GET['addform']))
    {
        include 
    $_SERVER['DOCUMENT_ROOT'] . '/includes/db.inc.php';
        try {
            
    $sql 'INSERT INTO author SET 
            name = :name,
            email = :email'
    ;
            
    $s $pdo->prepare($sql);
            
    $s->bindValue(':name'$_POST['name']);
            
    $s->bindValue(':email'$_POST['email']);
            
    $s->execute();
        } catch (
    PDOException $e)
        {
            
    $error 'Error adding new author! ' $e->getMessage();
            include 
    'error.html.php';
            exit(); 
        }
        
    header('Location: .');
        exit();
    }

    //Loading the form in 'edit author' mode
    if (isset($_POST['action']) && $_POST['action'] == 'Edit')
    {
        include 
    $_SERVER['DOCUMENT_ROOT'] . '/includes/db.inc.php';
        try {
            
    $sql 'SELECT id, name, email FROM author WHERE id = :id';
            
    $s $pdo->prepare($sql);
            
    $s->bindValue(':id'$_POST['id']);
            
    $s->execute();
        } catch (
    PDOException $e){
            
    $error 'Error fetching existing author info! ' $e->getMessage();
            include 
    'error.html.php';
            exit();
        }

        
    $row $s->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']))
    {
        include 
    $_SERVER['DOCUMENT_ROOT'] . '/includes/db.inc.php';
        try{
            
    $sql 'UPDATE author SET 
            name = :name,
            email = :email
            WHERE id = :id'
    ;
            
    $s $pdo->prepare($sql);
            
    $s->bindValue(':id'$_POST['id']);
            
    $s->bindValue(':name'$_POST['name']);
            
    $s->bindValue(':email'$_POST['email']);
            
    $s->execute();
        } catch (
    PDOException $e){
            
    $error 'Error updating author! ' $e->getMessage();
            include 
    'error.html.php';
            exit();
        }
        
    header('Location: .');
        exit();
    }


    if (isset(
    $_POST['action']) && $_POST['action'] == 'Delete')
        {
            
    //Get jokes belonging to author
            
    try {
                
    $sql 'SELECT id FROM joke WHERE authorid = :id';
                
    $s $pdo->prepare($sql);
                
    $s->bindValue(':id'$_POST['id']);
                
    $s->execute();
            } catch (
    PDOException $e){
                
    $error 'Error fetching authors with their jokes! ' $e->getMessage();
                include 
    'error.html.php';
                exit();
            }
            
            
    $result $s->fetchAll();

            
    //Delete joke category entries
            
    try {
                
    $sql 'DELETE FROM jokecategory WHERE jokeid = :id';
                
    $s $pdo->prepare($sql);

                
    //For each joke
                
    foreach ($result as $row)
                    {
                        
    $jokeId $row['id'];
                        
    $s->bindValue(':id'$jokeId);
                        
    $s->execute();
                    }
            } catch (
    PDOException $e){
                
    $error 'Error deleting joke category! ' $e->getMessage();
                include 
    'error.html.php';
                exit();
            }

            
    //Delete jokes belonging to author
            
    try {
                
    $sql 'DELETE FROM joke WHERE authorid = :id';
                
    $s $pdo->prepare($sql);
                
    $s->bindValue(':id'$_POST['id']);
                
    $s->execute();
            } catch (
    PDOException $e){
                
    $error 'Error deleting joke from a specific author! ' $e->getMessage();
                include 
    'error.html.php';
                exit();
            }

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

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

    foreach (
    $result as $row){
        
    $authors[] = array(
            
    'id' => $row['id'],
            
    'name' => $row['name']
            ); 


    include 
    'authors.html.php';
    ?>
    authors.html.php
    HTML Code:
    <?php
    include_once $_SERVER['DOCUMENT_ROOT'] . '/includes/helper.inc.php';
    ?>
    <!DOCTYPE html>
    <html lang="en">
    <head>
    	<meta charset="utf-8"/>
    	<title>Manage Authors</title>
    </head>
    <body>
    	<h1>Manage Authors</h1>
    	<p><a href="?add">Add new author</a></p>
    	<ul>
    		<?php foreach($authors as $author): ?>
    			<li>
    				<form action="" method="post">
    					<div>
    						<?php echo htmlout($author['name']); ?>
    						<input type="hidden" name="id" value="<?php echo htmlout($author['id']); ?>"/>
    						<input type="submit" name="action" value="Edit"/>
    						<input type="submit" name="action" value="Delete"/>
    					</div>
    				</form>
    			</li>
    		<?php endforeach; ?>
    	</ul>
    	<p><a href="..">Return to JMS home</a></p>
    </body>
    </html>
    Thanks,
    Karl

  2. #2
    SitePoint Member
    Join Date
    Aug 2013
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What happens here is that whenever I submit the form either its an add or edit request, instead of displaying the result of the query, it will redirect to another directory which is the addform/editform. Anyone from here that can tell what went wrong or what's missing on the code?

    Thanks!


Tags for this Thread

Bookmarks

Posting Permissions

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