【问题标题】:Insert picture into database using php使用php将图片插入数据库
【发布时间】:2014-04-09 18:29:46
【问题描述】:

您好,我有一个包含许多输入(姓名、姓氏、图片、年龄..)的表单,我想将所有这些信息提交到我的数据库中,我知道如何进行简单的输入,但图片我有问题 有人能帮助我吗 我的 html 文件是

<form action="insertion.php" method="post" >
<table >
 <tr>
 <td><strong>Titre</strong></td>
 <td><input name="titre" type="text" value=""  /></td>
 </tr>
 <tr>
 <td><strong>Annee</strong></td>
 <td><input name="annee" type="number" value=""  /></td>
 </tr>
 <tr>
 <td><strong>Genre musical</strong></td>
 <td><input name="Gmusical" type="texte" value="" /></td>
 </tr>
 <tr>
 <td>
 <strong>Picture</strong>
 </td>
 <td>
 <input type="file" name="img"/>
 </td>
 </tr>
</table>
 <input type="submit" value="Submit "  />
</form>

我要提交到数据库的文件insertion.php是

<?php
include("connexion.php");
$titre=$_POST['titre'];
$annee=$_POST['annee'];
$Gmusical=$_POST['Gmusical'];
$picture=$_POST['img'];

$req="INSERT INTO `cd`
(`titre`, `Annee`, `genremusical`, `Image`) 
VALUES 
 ('$titre','$annee','$Gmusical','$picture');";

            if (mysql_query($req))
            {
            echo "ok";
            }
            else 
            echo 'ko';
            }

【问题讨论】:

  • 你可能需要看到这个stackoverflow.com/questions/17153624/…
  • 旁注:为了处理与图像相关的表单,您的表单首先需要包含enctype='multipart/form-data'。加上这个$picture=$_POST['img'];很可能是$picture=$_FILES['img'];
  • 您不应该将二进制图像数据存储到数据库中。只需将它们保存为图像文件并将路径/文件名保存到数据库即可。
  • 如果你愿意,你可以存储二进制数据——但你应该知道你为什么要这样做!

标签: php mysql


【解决方案1】:

通常您不想在数据库中存储实际的 BLOB(二进制大对象)数据类型。您将图像的路径存储在 Web 服务器上的某个位置。

因此,在“图像”列中,您将存储路径“images/photo1103.jpg”。

要显示照片: echo "&lt;img src=". $image_query_fetch['Image'] .'" alt=\"\" /&gt;";

【讨论】:

    【解决方案2】:

    您必须将数据库中“图像”列的列类型设置为BLOB或LONGBLOB(或others),例如:

    CREATE TABLE cd (
     ... 
     Image LONGBLOB, 
    ); 
    

    然后像你一样简单地插入数据。

    解决此问题的通常更好的方法是将文件存储在文件系统中,并且仅将文件的路径保存在数据库中。如果您想这样做,您可能需要查看this SO question。 (另见this one。)

    (正如用户 Fred -ii- 在评论中指出的那样,您还必须设置 form-Tag 的 enctype到“多部分/表单数据”。)

    【讨论】:

      【解决方案3】:

      列类型可以是 varchar 我还建议使用 mysqli 而不是 mysql

      下面的代码应该可以帮助你: 编辑了代码试试这个作为一个文件,如果你还没有尝试过,可以尝试使用echo $picture_name;调试它。

      (可选: 还有一件事可以提供帮助是ob_start();将它放在文件的最顶部,紧随&lt;?php标签之后,而不是在页面底部用ob_flush();?&gt;标签之前关闭它,这样你就会得到类似的东西:)

      <?php
      include('connexion.php');
      
      function outout_errors($error) {
          echo '<ul><li>',$error.'</li></ul>';
      }
      
      if($_POST) {
          $titre = mysql_real_escape_string(strip_tags($_POST['titre']));
          $annee = mysql_real_escape_string(strip_tags($_POST['annee']));
          $Gmusical = mysql_real_escape_string(strip_tags($_POST['Gmusical']));
          $picture_tmp = $_FILES['img']['tmp_name'];
          $picture_name = $_FILES['img']['name'];
          $picture_type = $_FILES['img']['type'];
      
          $allowed_type = array('image/png', 'image/gif', 'image/jpg', 'image/jpeg');
      
          if(in_array($picture_type, $allowed_type)) {
              $path = 'images/'.$picture_name; //change this to your liking
          } else {
              $error[] = 'File type not allowed';
          }
      
          if(!is_numeric($annee)) {
              $error[] = $annee.' is not a number';
          }
      
          if(!empty($error)) {
              echo '<font color="red">'.output_errors($error).'</font>';
      
          } else if(empty($error)) {
              $req="INSERT INTO `cd` (`titre`, `Annee`, `genremusical`, `Image`)  VALUES ('$titre', '$annee', '$Gmusical', '$path')";
              move_uploaded_file($picture_tmp, $path);
      
              if (mysql_query($req)) {
                  echo 'ok';
              } else {
                  echo 'ko';
              }
          }
      }
      
      
      ?>
      
      <form action="" method="post" enctype="multipart/form-data">
          <table>
           <tr>
              <td><strong>Titre</strong></td>
              <td><input name="titre" type="text"></td>
           </tr> <tr>
              <td><strong>Annee</strong></td>
              <td><input name="annee" type="text"></td>
           </tr><tr>
              <td><strong>Genre musical</strong></td>
              <td><input name="Gmusical" type="text"></td>
           </tr><tr>
              <td><strong>Picture</strong></td>
           <td><input type="file" name="img"></td>
           </tr>
           <tr>
              <input type="submit">
           </tr>
          </table>
      </form>
      

      【讨论】:

        【解决方案4】:
        <?php include("config.php");?>
        <?php
        error_reporting("0");
        if(isset($_POST['submit'])) {   
            // Get image name   
            $image = $_FILES['image']['name'];
            // Get text
            $image_name= $_POST['image_name'];
            // Image file directory 
            $target = "images/".basename($image);
            // Now insert query
            $sql = "INSERT INTO addimage(image, image_name) values ('$image','$image_name')";
            // Execute query
            mysqli_query($dbcon,$sql);  
            if(move_uploaded_file($_FILES['image']['tmp_name'],$target))    
            {
              $img = "image uploading successfully";
            } else {
              $img = "faild image uploading";   
            }   
        }   
        ?>  
        
        <!DOCTYPE html>
        <html>    
        <head>
          <title>Upload image with text Field</title>
          <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
          <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
          <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
          <script type="text/javascript" src="script.js"></script>
        </head>
        
        <body>
          <div class="container">
            <div class="col-sm-10">
              <h1>Image uploading with text field</h1>
              <div class="card-box">
                <div class="row">
                  <div class="col-md-6">
                    <div class="p-20">
                      <form method="post" enctype="multipart/form-data">
                        <h5 class="alert-success">
                          <?php if(isset($img)){ echo $msg;}?>
                        </h5>
                        <div class="form-group">
                          <label class="control-label">Image Name</label>
                          <input type="text" class="form-control" data-size="sm" name="image_name">
                        </div>
                        <div class="form-group">
                          <label class="control-label">Small file style</label>
                          <input type="file" class="filestyle" data-size="sm" name="image">
                        </div>
                        <div class="form-group">
                          <input type="submit" class="btn btn-success" value="Save" name="submit">
                        </div>
                      </form>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </body>    
        </html>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-10-03
          • 2023-03-05
          • 2016-03-02
          相关资源
          最近更新 更多