SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2013
    Posts
    35
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Novice to Ninja Chapter 4: Inserting Data into the database

    I'm following the examples in the book except that I've substituted different names for my db, etc.

    My database name is films
    The column I'm trying add data to is Title
    I named the folder in htdocs addfilm
    instead of jokes.html.php I have a flle named films.html.php and I substituted $films and $title in this line: <?php foreach ($films as $title): ?> though netbeans indicates that the variable $films seems to be uninitilized (not sure what that means)
    in the form.html.php file I substituted titletext for joketext
    Here's part of the code for my index.php file:
    PHP Code:
    if (isset($_GET['addfilm']))
    {
      include 
    'form.html.php';
      exit();
    }

    try
    {
      
    $pdo = new PDO('mysql:host=localhost;dbname=films''films''*****');
      
    $pdo->setAttribute(PDO::ATTR_ERRMODEPDO::ERRMODE_EXCEPTION);
      
    $pdo->exec('SET NAMES "utf8"');
    }
    catch (
    PDOException $e)
    {
      
    $error 'Unable to connect to the database server.';
      include 
    'error.html.php';
      exit();
    }

    if (isset(
    $_POST['Title']))
    {
      try
      {
        
    $sql 'INSERT INTO films SET
            titletext = :titletext,
            titledate = CURDATE()'
    ;
        
    $s $pdo->prepare($sql);
        
    $s->bindValue(':titletext'$_POST['titletext']);
        
    $s->execute();
      }
      catch (
    PDOException $e)
      {
        
    $error 'Error adding submitted title: ' $e->getMessage();
        include 
    'error.html.php';
        exit();
      }

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

    try
    {
      
    $sql 'SELECT Title FROM films';
      
    $result $pdo->query($sql);
    }
    catch (
    PDOException $e)
    {
      
    $error 'Error fetching films: ' $e->getMessage();
      include 
    'error.html.php';
      exit();
    }

    while (
    $row $result->fetch())
    {
      
    $films[] = $row['Title'];  // netbeans indicates that the variable $films seem to be uninitialized
    }

    include 
    'films.html.php'
    When I load this into my browser I don't get any error messages but when I try to add a title via the form it isn't added to the database.
    Last edited by SpacePhoenix; Oct 18, 2013 at 00:07. Reason: placed php tags around php code

  2. #2
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    4,904
    Mentioned
    93 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $films=array();
    $films $s->fetchAll(); 
    You could use that instead of the while loop, it will grab the entire result set in one go
    Community Team Advisor
    Forum Guidelines: Posting FAQ Signatures FAQ Self Promotion FAQ
    Help the Mods: What's Fluff? Report Fluff/Spam to a Moderator

  3. #3
    SitePoint Enthusiast
    Join Date
    Feb 2013
    Posts
    35
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by SpacePhoenix View Post
    PHP Code:
    $films=array();
    $films $s->fetchAll(); 
    You could use that instead of the while loop, it will grab the entire result set in one go
    Thanks. Perhaps I entered the code incorrectly but now when I load the addfilm page I get this message:

    Notice: Undefined variable: s in /Applications/MAMP/htdocs/addfilm/index.php on line 78
    Fatal error: Call to a member function fetchAll() on a non-object in /Applications/MAMP/htdocs/addfilm/index.php on line 78

    Here's the end of the code after I pasted in your suggested code:

    PHP Code:
    try
    {
      
    $sql 'SELECT Title FROM films';
      
    $result $pdo->query($sql); // netbeans indicates that variable $result seems to be unused in its scope
    }
    catch (
    PDOException $e)
    {
      
    $error 'Error fetching films: ' $e->getMessage();
      include 
    'error.html.php';
      exit();
    }
    {
    $films=array();
    $films $s->fetchAll(); 
    }
    include 
    'films.html.php'

  4. #4
    SitePoint Enthusiast
    Join Date
    Feb 2013
    Posts
    35
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I'm pretty sure my error has to do with the $films variable. In the book it's $jokes and I assumed that jokes in that variable is the name of the database.

  5. #5
    SitePoint Evangelist
    Join Date
    Oct 2005
    Location
    Michigan, USA
    Posts
    434
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Should have been
    PHP Code:
    $result->fetchAll(); 
    not $s->fetchAll();
    - Robert

  6. #6
    SitePoint Enthusiast
    Join Date
    Feb 2013
    Posts
    35
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by QMonkey View Post
    Should have been
    PHP Code:
    $result->fetchAll(); 
    not $s->fetchAll();
    Thanks but now I'm getting this error message when I load the page: Warning: htmlspecialchars() expects parameter 1 to be string, array given in /Applications/MAMP/htdocs/addfilm/films.html.php on line 12

    Here is the code from line 12:

    Code:
    <?php foreach ($films as $title): ?>
    Here's the code for films.html.php:

    Code:
    <!DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="utf-8">
        <title>List of Films</title>
      </head>
      <body>
        <p><a href="?addfilm">Add Film Title</a></p>
        <p>Here are all the films in the database:</p>
        <?php foreach ($films as $title): ?>
          <blockquote>
            <p><?php echo htmlspecialchars($title, ENT_QUOTES, 'UTF-8'); ?></p>
          </blockquote>
        <?php endforeach; ?>
      </body>
    </html>

  7. #7
    SitePoint Enthusiast
    Join Date
    Feb 2013
    Posts
    35
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Well, I figured this out on my own. Here's an excerpt of the correct code for the index.php file:

    Code:
    if (isset($_POST['titletext'])) // I had 'Title' here before instead of 'titletext' which represents the text entered in the add film text box
    {
      try
      {
        $sql = 'INSERT INTO films SET
            Title = :titletext'; // formerly I had titletext instead of Title - didn't realize that the first field is the name of a column in my table
            titledate = CURDATE()'; // I deleted this line as I don't have a column titledate in my table
        $s = $pdo->prepare($sql);
        $s->bindValue(':titletext', $_POST['titletext']);
        $s->execute();
    Then at the bottom I changed this:

    Code:
    while ($row = $result->fetch())
    {
      $films[] = $row['Title'];  // $films should have been $titles. I thought that films in $films stood for the name of my database but I guess it's the name of the variable set in films.html.php (see below)
    }
    Excerpt from films.html.php:

    Code:
    <?php foreach ($titles as $film): ?>
          <blockquote>
            <p><?php echo htmlspecialchars($film, ENT_QUOTES, 'UTF-8'); ?></p>
          </blockquote>
        <?php endforeach; ?>
    Would appreciate any comments that would help clarify this further for me.


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
  •