Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Insert time into SQL

I'm having trouble inserting time formats like 2:40 (minutes : seconds) into my SQL. I have a form which submits different data into it like title, artist, genre, etc. and it's displaying everything fine on the website but the time. It's always showing 00:00:00 and I don't know why.

Here's the form for the submission of the data:

<form class="upload" action="" method="GET">
        <div class="titles">
            <h4>Title</h4>
            <h4>Artist</h4>
            <h4>Genre</h4>
            <h4>Length</h4>
            <h4>Beats per minute</h4>
        </div>
        <div class="infos">
            <input type="text" name="title">
            <input type="text" name="artist">
            <input type="text" name="genre">
            <input type="text" name="length">
            <input type="text" name="bpm">
            <input type="hidden" name="action" value="INSERT">
            <br/>
            <button type="submit">Submit</button>
        </div>
    </form>

This is my current Code for Inserting the data:

if ($_GET['action'] == 'INSERT') {
    $sql = "INSERT INTO songs(title, artist, genre, runtime, bpm) VALUES ('" . $_GET['title'] . "' ,' " . $_GET['artist'] . "' ,' " . $_GET['genre'] . "' ,' " . $_GET['runtime'] . "' ,' " . $_GET['bpm'] . "')";
    echo $sql . "<hr />";
    $result = $conn->query($sql);
}

Since the echo $sql; displays what's going on behind the scenes, it displays this:

INSERT INTO songs(title, artist, genre, runtime, bpm) VALUES ('Test' ,' Test' ,' Test' ,' ' ,' 300')

But as soon as I edit the time afterwards (I built an edit button for that) it's changing the time, even if I have to write it like 00:02:40.

The edit button looks like this:

<form class="update" action="/" method="GET">
                <div class="titles">
                    <h4>Title</h4>
                    <h4>Artist</h4>
                    <h4>Genre</h4>
                    <h4>Length</h4>
                    <h4>Beats per minute</h4>
                    <button type="submit">Submit</button>
                </div>
            <input type="text" name="title" value="' . $row['title'] . '">
            <input type="text" name="artist" value="' . $row['artist'] . '">
            <input type="text" name="genre" value="' . $row['genre'] . '">
            <input type="text" name="runtime" value="' . $row['runtime'] . '">
            <input type="text" name="bpm" value="' . $row['bpm'] . '">
            <input type="hidden" name="ID" value="' . $row['id'] . '">
            <input type="hidden" name="action" value="UPDATE">
            </form>';

And the code for updating my data:

if ($_GET['action'] == 'UPDATE') {
    $sql = "UPDATE songs SET artist='" . $_GET['artist'] . "', title='" . $_GET['title'] . "', genre='" . $_GET['genre'] . "', runtime='" . $_GET['runtime'] . "', bpm='" . $_GET['bpm'] . "' WHERE id=" . $_GET['ID'];
    $result = $conn->query($sql);
}

Can someone please explain why the time is always empty? Also I'd be really thankful if one of you could explain how I can insert time formats like minutes : seconds into my database.

Dave

1 Answer

hi! in the form you have an input element with a name length but in the myqsl query you try to use the $_GET['runtime'] instead of $_GET['lenght'].

Oh boy what a stupid mistake. Thnaks a lot for enlighten me!