0

I have a table called picture(id, pic, date_posted). I want to insert the picture's url into the 'Pics' directory in my website from my index.php page. I used the following code:

if(isset($_POST['send'])){
    //sever is already connected, and database has already been selected
    $name = str_replace(" ","_",$_FILES['img']['name']);
    move_uploaded_file($_FILES['img']['tmp_name'], 'Pics/'.$name);

    //my sql statement that I think it doesn't work

    $sql = "INSERT INTO picture(pic, date_posted) VALUES(Pics/'".$name. "', ".time().")";
    $result = mysql_query($sql, $on) or die(mysql_error());

    //displaying the result
    $sql_dislay = "SELECT * FROM picture";
    $result_display = mysql_query($sql_display, $con) or die(mysql_error());

    echo "<table border=1>
            <tr>
               <th>ID</th>
               <th>Image</th>
               <th>Date Uploaded</th>
            </tr>";
    while($row = mysql_fetch_array($result_display)){
        $id = $row['id'];
        $pic = $row['pic'];
        $d = $row['date_posted'];

        echo "<tr>
                 <td>$id</td>
                 <td>$pic</td>
                 <td>$d</td>
              </tr>";
    }
    echo "</table>";
}

I got the error message from the sql statement 'Unknown column 'Pics' in 'field list'. But actually I want to insert the url of the image to the table, let say 'Pics/image001.jpg' then when I retrieved the result, it'll be what I want. Once again, the date posted is 0000-00-00 which is not the result I wanted.

Any help would be appreciated. Thanks

1 Answer 1

2

The query should be

$sql = "INSERT INTO picture(pic, date_posted) VALUES('Pics/".$name. "', ".time().")";

The quotes in the values should start before Pics.

And regarding the date, The best thing to do would be to use the ON UPDATE CURRENT_TIMESTAMP, if the date_posted field is of the type timestamp. About your problem, time()

Returns the current time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).

So, if you want to use time(), then you can use date() function to convert into time stamp

Sign up to request clarification or add additional context in comments.

3 Comments

thanks guy, it works. But the date_posted column display 0000-00-00 what should I do?
Balanicash is exactly right. The reason you want to do this is that the entire value Pics/your_file_name.jpg is a string, not just the name of the file.
For the time, use the MySQL NOW() function instead of PHP's time. That way, you don't have to deal with formatting the date. $sql = "INSERT INTO picture(pic, date_posted) VALUES('Pics/".$name. "', NOW())";

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.