SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Member
    Join Date
    Nov 2012
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Chapter 7 Novice to Ninja

    Hello, Im building teh database in novice to Ninja and am getting stcuk. Im at the authours management part, i have the pages set up, its cycles through and goes back to the original page, it just doesnt want to add or edit an author, only delete works. Any ideas, ive ripped the code from the book and wrote it out myself. What can I be missing that wont edit or added to the daatabse? but it will delete.

    Thanks

    PHP Code:
    <?php
    include_once $_SERVER['DOCUMENT_ROOT'] .
        
    '/paranoiddb/includes/magicquotes.inc.php';

    if (isset(
    $_GET['add']))
    {
      
    $pageTitle 'New Band';
      
    $action 'addform';
      
    $name '';
      
    $email '';
      
    $id '';
      
    $button 'Add Band';

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

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

      try
      {
        
    $sql 'INSERT INTO band 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 submitted author.';
        include 
    'error.html.php';
        exit();
      }

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

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

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

      
    $row $s->fetch();

      
    $pageTitle 'Edit Band';
      
    $action 'editform';
      
    $name $row['name'];
      
    $email $row['email'];
      
    $id $row['id'];
      
    $button 'Update band';

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

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

      try
      {
        
    $sql 'UPDATE band 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 submitted author.';
        include 
    'error.html.php';
        exit();
      }

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

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

      
    // Get jokes belonging to author
      
    try
      {
        
    $sql 'SELECT id FROM video WHERE bandid = :id';
        
    $s $pdo->prepare($sql);
        
    $s->bindValue(':id'$_POST['id']);
        
    $s->execute();
      }
      catch (
    PDOException $e)
      {
        
    $error 'Error getting list of jokes to delete.';
        include 
    'error.html.php';
        exit();
      }

      
    $result $s->fetchAll();

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

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

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

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

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

    // Display author list
    include $_SERVER['DOCUMENT_ROOT'] . '/paranoiddb/includes/db.inc.php';

    try
    {
      
    $result $pdo->query('SELECT id, name FROM band ');
    }
    catch (
    PDOException $e)
    {
      
    $error 'Error fetching authors from the database!';
      include 
    'error.html.php';
      exit();
    }

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

    include 
    'bands.html.php';

    ?>
    Last edited by paul_wilkins; Nov 9, 2012 at 02:38. Reason: add [php] tags

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Try and dump out the get and post vars and inspect them carefully.

    PHP Code:
    if (isset($_GET['addform'])) 

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

    // 2 lines of debug you can comment out later
    var_dump($_GET);
    var_dump($_POST);

      try 
      { 
        
    $sql 'INSERT INTO band 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 submitted author.'
        include 
    'error.html.php'
        exit(); 
      } 

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

    The other thing that springs to mind is to check that your table's id is an auto-increment field.

    Sorry, I've no direct experience of this book.

  3. #3
    SitePoint Member
    Join Date
    Nov 2012
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Cups View Post
    Try and dump out the get and post vars and inspect them carefully.

    PHP Code:
    if (isset($_GET['addform'])) 

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

    // 2 lines of debug you can comment out later
    var_dump($_GET);
    var_dump($_POST);

      try 
      { 
        
    $sql 'INSERT INTO band 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 submitted author.'
        include 
    'error.html.php'
        exit(); 
      } 

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

    The other thing that springs to mind is to check that your table's id is an auto-increment field.

    Sorry, I've no direct experience of this book.


    Ive done that,and this is what it gives me, an output of the stuff I'm trying to change, but no change happens...
    [php]
    Joyce Collingwood array(0) { } array(3) { ["name"]=> string(17) "Joyce CollingWOOD" ["email"]=> string(27) "girlsgirlsgirls@hotmail.com" ["id"]=> string(2) "3 " }
    [php]

    Does this look right?

  4. #4
    Foozle Reducer ServerStorm's Avatar
    Join Date
    Feb 2005
    Location
    Burlington, Canada
    Posts
    2,699
    Mentioned
    89 Post(s)
    Tagged
    2 Thread(s)
    Ive done that,and this is what it gives me, an output of the stuff I'm trying to change, but no change happens...
    PHP Code:
    Joyce Collingwood array(0) { } array(3) { ["name"]=> string(17"Joyce CollingWOOD" ["email"]=> string(27"girlsgirlsgirls@hotmail.com" ["id"]=> string(2"3 " 


    Does this look right?
    Yes this has all values you should have.

    Try var_dump (ing) the $_POST inside the try block like:
    PHP Code:
    try
    {
      
    var_dump($_POST);
      
    var_dump($_GET); 
    If you still see that values are outputting then try not binding the fields on the insert like:
    PHP Code:
    try
    {
      
    $sql 'INSET INTO author SET
        name = $_POST['
    name']
        email = $_POST['
    email'];
        $s = $pdo->prepare($sql);
        /* Comment these binded parameters out */
        //$s = bindValue('
    name', $_POST['name']);
        //$s = bindValue('
    email', $_POST['email']);
        $s->execute(); 
    Do the values insert?
    Steve
    ictus==""

  5. #5
    SitePoint Member
    Join Date
    Nov 2012
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I did both, typed it all at at first, then ripped the archive.

  6. #6
    Foozle Reducer ServerStorm's Avatar
    Join Date
    Feb 2005
    Location
    Burlington, Canada
    Posts
    2,699
    Mentioned
    89 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by harryrodd View Post
    I did both, typed it all at at first, then ripped the archive.
    See my massively edited post above.
    ictus==""

  7. #7
    SitePoint Member
    Join Date
    Nov 2012
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It outputs what Im trying to add ,

    Unleash The Archers array(0) { } array(3) { ["name"]=> string(19) "UNLEASH THE ARCHERS" ["email"]=> string(20) "METAL4EVER@email.com" ["id"]=> string(2) "1 " }

  8. #8
    Foozle Reducer ServerStorm's Avatar
    Join Date
    Feb 2005
    Location
    Burlington, Canada
    Posts
    2,699
    Mentioned
    89 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by harryrodd View Post
    It outputs what Im trying to add ,

    Unleash The Archers array(0) { } array(3) { ["name"]=> string(19) "UNLEASH THE ARCHERS" ["email"]=> string(20) "METAL4EVER@email.com" ["id"]=> string(2) "1 " }
    Ok, but did you try not binding the SQL values like I showed above; without the binding does it insert?
    ictus==""

  9. #9
    SitePoint Member
    Join Date
    Nov 2012
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I took the bindings out, still outputs but does not insert.

  10. #10
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by Cups
    The other thing that springs to mind is to check that your table's id is an auto-increment field.
    Is it? Does your table consist of only id, name, email and is id an auto-incremented field?

  11. #11
    SitePoint Member
    Join Date
    Nov 2012
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Cups View Post
    Is it? Does your table consist of only id, name, email and is id an auto-incremented field?
    under the TABLE band, is just id, name and email. id is set to auto increment.


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
  •