-1

In the last hours I tried many codes but none of them works. I try to upload an image to my database. But the Picture doesnt show on the localhost database :(

The other variables like Titel etc. are okay.

My Code is like this:

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css" />

<meta charset="ISO-8859-1">
<title>Film Hinzufügen</title>
<script type=“text/javascript”>

function isNumberKey(evt)
      {
         var charCode = (evt.which) ? evt.which : event.keyCode
         if (charCode > 31 && (charCode < 48 || charCode > 57))
            return false;

         return true;
      }

</script>
<?php

    if (isset($_POST['addButton'])) {

         //SQL Injection defence!

        //Variablen hinzufügen
        $Titel = $_POST['Titel'];
        $Genre = $_POST['Genre'];
        $Picture = $_POST['Datei'];
        $Erscheinungsjahr = $_POST['Erscheinungsjahr'];
        $FSK = $_POST['FSK'];
        $Filmdauer = $_POST['Filmdauer'];
        $Beschreibung = $_POST['Beschreibung'];
        $imgData =$_POST['Beschreibung'];
        //Film hinzufügen
        $con = mysql_connect("127.0.0.1","root","");
        if (!$con) {
            die('Could not connect: ' . mysqli_error($con));
        }
        mysql_select_db("db_movie_usr");
        $sqlString = "INSERT INTO t_movie (FilmTitel, Genre, Erscheinungsjahr, FSK, Filmdauer, Beschreibung, Picture) VALUES ('".$Titel."','".$Genre."','".$Erscheinungsjahr."','".$FSK."','".$Filmdauer."','".$Beschreibung."','file_get_contents($imgData)')";



        if (mysql_query($sqlString)) {

            header("Location:MainPage.php");
        }
    }   
        else {
            print (" ");
        }


?>
</head>

<body class="body2" >
<form METHOD ="POST" ACTION = "AddMovie.php">

 <div style="margin:0 auto;text-align:center">
 <div class="centre" >

<table class="tg">
//Add the Title, Genre etc.


</table>
<input name="Datei" type="file" size="50" accept="text/*"> 
<input type="Submit" name="addButton" value="Film adden"  >

 </div>
 </div>
 </form>
</body>
</html>

In my MySQL Database is a mediablob data.

Do you guys see my problem?

Sorry for the bad code. It is my first php code and I need to do this as my homework.

semaxx
  • 43
  • 9
  • 1
    you're missing a valid enctype; it's required when dealing with files. plus this will kill your JS ` – Funk Forty Niner Apr 24 '15 at 14:01
  • Add error reporting to the top of your file(s) right after your opening PHP tag for example ` – Funk Forty Niner Apr 24 '15 at 14:02
  • 1
    [You need to prevent SQL Injection!](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) Please, [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Apr 24 '15 at 14:02
  • refer this links http://stackoverflow.com/questions/17717506/how-to-upload-images-into-mysql-database-using-php-code and http://forgetcode.com/PHP/539-Photo-Upload-and-Retrive-with-MySql-using-BLOB – Jayson Apr 24 '15 at 14:02
  • 1
    the form tag should be `enctype="multipart/form-data"` – Jayson Apr 24 '15 at 14:03
  • @JayBlanchard but would that solve my problem? – semaxx Apr 24 '15 at 14:07
  • @Fred-ii- Like:
    ?
    – semaxx Apr 24 '15 at 14:09
  • You should not store an image in a mysql database. – M K Apr 24 '15 at 14:38

1 Answers1

0

Just copy and paste this body to your code:

<body class="body2" >
 <form method="POST" action="AddMovie.php" enctype="multipart/form-data">

    <div style="margin:0 auto;text-align:center">
    <div class="centre" >

       <table class="tg">
          //Add the Title, Genre etc.

       </table>
    <input name="Datei" type="file" size="50" accept="text/*"> 
    <input type="Submit" name="addButton" value="Film adden"  >

  </div>
  </div>
 </form>
</body>

Also use this php code:

<?php

if (isset($_POST['addButton'])) {

     //SQL Injection defence!

    //Variablen hinzufügen
    $Titel = $_POST['Titel'];
    $Genre = $_POST['Genre'];
    $Picture = $_FILES['Datei']['tmp_name'];
    $Erscheinungsjahr = $_POST['Erscheinungsjahr'];
    $FSK = $_POST['FSK'];
    $Filmdauer = $_POST['Filmdauer'];
    $Beschreibung = $_POST['Beschreibung'];
    $imgData =$_POST['Beschreibung'];
    //Film hinzufügen
    $con = mysql_connect("127.0.0.1","root","");
    if (!$con) {
        die('Could not connect: ' . mysqli_error($con));
    }
    mysql_select_db("db_movie_usr");
    $sqlString = "INSERT INTO t_movie (FilmTitel, Genre, Erscheinungsjahr, FSK, Filmdauer, Beschreibung, Picture) VALUES ('".$Titel."','".$Genre."','".$Erscheinungsjahr."','".$FSK."','".$Filmdauer."','".$Beschreibung."','file_get_contents($imgData)')";



    if (mysql_query($sqlString)) {

        header("Location:MainPage.php");
    }
}   
    else {
        print (" ");
    }
?>
Shahzad Barkati
  • 2,532
  • 6
  • 25
  • 33
  • Still doesnt work :/ Now there is a data at the database. But it is just 21B. And I cant see it with my other programm. I am sure the problem is the upload and not the watching part.. – semaxx Apr 24 '15 at 14:21
  • I will try it on monday :) – semaxx Apr 24 '15 at 14:57