SitePoint Sponsor

User Tag List

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

    Question Entering and Displaying Multiple Values

    Hello-
    I don't know if you're familiar with the "Build Your Own Database Driven Website" book from Sitepoint, but I'm using it to create an application to inventory library items and have people check items in and out. Per the example in Chapter 4, the index file allows a user to see all items and add one if needed. I have the HTML file setup to have forms to insert Title, Author/Singer/Publisher, Date Published (Month, Year), and Type. I have the appropriate tables set up, too. The problem is in the index file; it is only set up to insert a title and a date (which I don't need). How do I edit the index to both insert and display the information? Here's the link to the code: http://pastebin.com/ACSXaPhq.

    Thank you so much.
    Tyler

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,495
    Mentioned
    161 Post(s)
    Tagged
    4 Thread(s)
    To insert all data from the form in the database, you'll have to modify this part of the code:
    Code PHP:
    if (isset($_POST['Title']))
    {
            $Title = mysqli_real_escape_string($link, $_POST['Title']);
            $sql = 'INSERT INTO library SET
                    Title="' . $title . '",
                    jokedate=CURDATE()';
            {
                    $error = 'Error adding submitted item: ' . mysqli_error($link);
                    include 'error.html.php';
                    exit();
            }
     
            header('Location: .');
            exit();
    }
    You will want to validate the inserted values (for example: all fields need to have a value), and sanitize them using mysqli_real_escape_string (like you already do with the title). And then you add them to the INSERT query.

    To display all the values, you have to modify this part of the code:
    Code PHP:
    $result = mysqli_query($link, 'SELECT Title  FROM library');
    if (!$result)
    {
            $error = 'Error fetching library items: ' . mysqli_error($link);
            include 'error.html.php';
            exit();
    }
     
    while ($row = mysqli_fetch_array($result))
    {
            $title[] = $row['Title'];
    }
    Add all column you want to get from the library table in the SELECT query. Then, in the while loop, put those values in arrays like you already do with the title.
    In items.html.php, you'll have to use those arrays to actually display the values (like you already do with the title).

  3. #3
    SitePoint Member
    Join Date
    Mar 2012
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've gotten the display to work, but I'm still having trouble with the INSERT command. What is the format to insert and sanitize more than one item? There's something probably seemingly wrong with this, but I don't know how to go about it. http://pastebin.com/ifTrVUJX

  4. #4
    SitePoint Enthusiast
    Join Date
    Apr 2004
    Location
    Michigan
    Posts
    79
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    try...
    PHP Code:
    $sql "INSERT INTO library SET Title= '$title', Author= '$author', PublishDate= '$date', Type='$type'"

  5. #5
    SitePoint Member
    Join Date
    Mar 2012
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry, but that didn't work. Thanks, though. I don't understand why it wouldn't.

  6. #6
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,495
    Mentioned
    161 Post(s)
    Tagged
    4 Thread(s)
    http://dev.mysql.com/doc/refman/5.5/en/insert.html

    You need a comma between the columns in the set clause.

    Didn't you get an error?

    Another thing you can do when debugging a query is echo it (echo $sql to check if it's as you expect, and then copy and paste it in phpMyAdmin to see what result it gives.

  7. #7
    SitePoint Enthusiast
    Join Date
    Apr 2004
    Location
    Michigan
    Posts
    79
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    what error message(s) are you getting?

  8. #8
    SitePoint Member
    Join Date
    Mar 2012
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Like such?
    PHP Code:
      $sql "INSERT INTO library SET
                Title="' . $title . '",
                            Author="' . $author . '",
                            PublishDate="' . $date . '",
                            Type="' . $type . '"; 
    Now I'm getting the error "Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in F:\xampp\htdocs\additem\index.php on line 54"

  9. #9
    SitePoint Member
    Join Date
    Mar 2012
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nevermind that error. I fixed the quotes. No error shows up, but nothing is inserted.

  10. #10
    SitePoint Member
    Join Date
    Mar 2012
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why would it not give an error if it didn't successfully insert the variables?

  11. #11
    SitePoint Member
    Join Date
    Mar 2012
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I figured it out. The error wasn't in the INSERT command, but in the variable names both in the index and form. Thanks for helping me work it out. I really appreciate it.


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
  •