使用php将图像添加到MySQL blob数据

时间:2015-04-24 13:59:53

标签: php mysql database

在过去的几个小时里,我尝试了很多代码,但都没有。 我尝试将图像上传到我的数据库。但是Picture不会显示在localhost数据库中:(

其他变量如Titel等都可以。

我的代码是这样的:

<!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>

在我的MySQL数据库中是一个mediablob数据。

你们看到我的问题吗?

对不好的代码抱歉。这是我的第一个PHP代码,我需要这样做作为我的作业。

1 个答案:

答案 0 :(得分:0)

只需将正文复制并粘贴到您的代码中即可:

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

也可以使用这个PHP代码:

<?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 (" ");
    }
?>