Entering and Displaying Multiple Values

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.

To insert all data from the form in the database, you’ll have to modify this part of the code:

if (isset($_POST['Title']))
        $Title = mysqli_real_escape_string($link, $_POST['Title']);
        $sql = 'INSERT INTO library SET
                Title="' . $title . '",
                $error = 'Error adding submitted item: ' . mysqli_error($link);
                include 'error.html.php';

        header('Location: .');

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:

$result = mysqli_query($link, 'SELECT Title  FROM library');
if (!$result)
        $error = 'Error fetching library items: ' . mysqli_error($link);
        include 'error.html.php';

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).

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


$sql = "INSERT INTO library SET Title= '$title', Author= '$author', PublishDate= '$date', Type='$type'";

Sorry, but that didn’t work. Thanks, though. I don’t understand why it wouldn’t.


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.

what error message(s) are you getting?

Like such?

  $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”

Nevermind that error. I fixed the quotes. No error shows up, but nothing is inserted.

Why would it not give an error if it didn’t successfully insert the variables?

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.