0
<!DOCTYPE html>
<html>
<head>
<title>Filmovi</title>
</head>
<body>

    <form action="unos.php" method="POST" enctype="multipart/form-data">
    <table border = "1">
    <tr>
    <td>Naslov:</td>
    <td><input type="text" name="naslov" value="" /></td>
    </tr>
    <tr>
    <td>Žanr:</td>
    <td>
    <?php

    $conn = new mysqli('localhost', 'root', '', 'kolekcija');
    $result = $conn->query("SELECT id, naziv FROM zanr");

    echo "<select name = 'id'>";
    while($row = $result->fetch_assoc())
    {
        unset($id, $name);
        $id = $row['id'];
        $name = $row['naziv'];
        echo '<option value = "' . $id . '">'.$name.'</option>';
    }

    echo "</select>";

    ?>
    </td>
    </tr>
    <tr>
    <td>Godina:</td>
    <td>
    <select name = "godina">
    <?php

    for($i = 1900; $i <= 2020; $i++)
    {
        echo "<option value = " .$i . ">" .$i . "</option>";
    }

    ?>
    <option name = "godina"></option>
    </select>
    </td>
    </tr>
    <tr>
    <td>Trajanje:</td>
    <td><input type="integer" name="trajanje" value="" /></td>
    </tr>
    <tr>
    <td>Slika:</td>
    <td><input type="file" name="image" />
    <input type = "hidden" name = "size" value = "1000000000" /></td>
    </tr>
    <tr>
    <td>Gumb:</td>
    <td><input type="submit" name="submit" value="Unos" /></td>
    </tr>

        </table>
    </form>

</body>
</html>

<?php

$link = mysqli_connect("localhost", "root", "") or die 
(mysqli_connect_errno());
mysqli_select_db($link, "kolekcija") or die ("Nemoguće se spojiti na 
bazu!".mysqli_connect_errno());

if(isset($_POST['submit']))
{
    $filename = $_FILES["image"]["name"];
    $filepath = "/unos.php:116".$filename;
    $naslov=$_POST['naslov'];
    $trajanje = $_POST['trajanje'];
    $zanr = $_POST['id'];
    $godina = $_POST['godina'];
    move_uploaded_file($filename, $filepath);

        $sql = "INSERT INTO filmovi (id, naziv_filma, trajanje, slika, 
id_zanr, godina)
                VALUES('null', '{$naslov}', '{$trajanje}' ,'{$filepath}', 
'{$zanr}', '{$godina}');";
     $result = mysqli_query($link, $sql) or die("Error in Query insert: " . 
mysqli_connect_errno());

    }

$msg = "";

$sql = "SELECT id, slika, naziv_filma, godina, trajanje FROM filmovi";
$myData = mysqli_query($link, $sql); 
echo "<table border = '1'>
<tr>
<th>Slika</th><th>Naziv filma</th><th>Godina</th><th>Trajanje</th>
<th>Akcija</th></tr>";
while($record = mysqli_fetch_array($myData)) 
    {
    echo "<form action = 'unos.php' method = 'post' enctype='multipart/form-
data'>";
    echo "<tr>";
    echo "<td>" . $msg.= '<img 
src="data:image/jpeg;base64,'.base64_encode($record['slika']) .'"/>' . "
</td>";
    echo "<td>" . $record['naziv_filma'] . "</td>";
    echo "<td>" . $record['godina'] . "</td>";
    echo "<td>" . $record['trajanje'] . "</td>";
    echo '<td>[<a href="unos.php?film='.$record[0].'">obriši</a>]</td>';

    if(isset($_GET['film']))
    {
        $id = ($_GET['film']);

        $sql_delete = "DELETE FROM filmovi WHERE id = {$id}";

        mysqli_query($link, $sql_delete);

        break;
    }


    echo "</tr>";
    echo "</form>";
    }




echo "</table>";

?>

Okay, this is my code, I have a programming course and this was the assigment. The idea was to make a form where you can put info about the movie, name, genre, year it was made and upload a picture. Everythign works and it's done but the only problem is that after uploading a picture (directly in the mysql database, I don't have a code where the picture is stored at the local disk), it won't load in the browser and after refreshing the browser the picture uploads itself again. It seems I have a loop somwhere but I can't figure out where 'cause I've been doing it and delaying the code for months as I'm stuck. Can someone help, please? Thank you very much in advance for all help!

  • You have an great example on how to store a picture in DB with PHP covered here: https://stackoverflow.com/questions/22236035/insert-picture-into-database-using-php – Denis Solakovic Aug 21 '17 at 13:31
  • You have to specify the folder in which you would like to save your images, so change your `$filepath = "/unos.php:116".$filename;` into `$filepath = "/directory/".$filename;`. Of course bare in mind that you have to have correct permissions on that folder. – Denis Solakovic Aug 21 '17 at 13:43

0 Answers0