图像未上载到数据库

pkmbmrz7  于 2021-06-21  发布在  Mysql
关注(0)|答案(4)|浏览(241)

我用一个表单创建了一个php脚本,它应该在数据库中插入一些数据,它实际上添加了文本和id,但没有添加文件。
数据库如下所示:
数据库名:highmob\u comenzi表名:players in table我们得到3行:id(auto\u increment)name(我们从表单中插入的名称)schite(应该上传文件的位置)type:blob colation:none,all none
这是我到目前为止试过的剧本

<?php
 include('connect-db.php');
 ?>
 <?php
 function renderForm($name, $schita, $error)
 {
 ?>

 <?php
 if ($error != '')
 {
 echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>';
 }
 ?>
  <form action="" method="post" enctype="multipart/form-data" >
  <input type="hidden" name="id" value="<?php echo $id; ?>"/>
  <input type="hidden" name="name" value="<?php echo $name; ?>"/>
  <input type="file" id="schita" name="schita" >
  <button type="submit" name="submit">Add Data</button>
  </form>
 <?php
 }
 include('connect-db.php');
 if (isset($_POST['submit']))
 {
 $name = mysql_real_escape_string(htmlspecialchars($_POST['name']));
 $schita = mysql_real_escape_string(htmlspecialchars($_POST['schita']));
 if ($name == '')
 {
 $error = 'Error !!';
 renderForm($name, $schita, $error);
 }
 else
 {
 mysql_query("INSERT players SET name='$name', schita='$schita'")
 or die(mysql_error());
 header("Location: mobila.php");
 }
 }
 else
 {
 renderForm('','','','','');
 }
 ?>

当我们以pagename.php?id=4的形式插入数据时,这个脚本为每个id创建一个页面
我想当我填写表格后,他创建了网页,当我打开网页看到上传的文件只在该网页上,
你知道为什么没用吗?

57hvy0tb

57hvy0tb1#

您需要将图像转换为base64,然后将其保存到数据库中。

// Select file type
  $target_file = basename($_FILES["file"]["name"]); 
  $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));

  // Convert to base64 
  $image_base64 = base64_encode(file_get_contents($_FILES['schita']['tmp_name']) );
  $image = 'data:image/'.$imageFileType.';base64,'.$image_base64;

  // Insert record
  $query = "INSERT into players(schita) values('".$image."')";
  mysqli_query($con,$query);
xqkwcwgp

xqkwcwgp2#

使用获取请求文件 $_FILES ,还需要确认mysql字段(schita)是 blob 类型

ttisahbt

ttisahbt3#

我用这个脚本上传了这个文件

<?php
 $dbh = new PDO("mysql:host=localhost;dbname=highmob_comenzi", "highmob", "PW");
 if(isset($_POST['btns'])){
     $name = $_FILES['myfile']['name'];
     $type = $_FILES['myfile']['type'];
     $data = file_get_contents($_FILES['myfile']['tmp_name']);
     $stmt = $dbh->prepare("UPDATE players SET data='$myfile', name='$name', mime='$type' WHERE id='$id'");
     $stmt->bindParam(1,$name);
     $stmt->bindParam(2,$type);
     $stmt->bindParam(3,$data);
     $stmt->execute();
 }
 ?>
 <!-- form -->
 <form method="post" enctype="multipart/form-data">
 <input type="file" name="myfile"/>
 <button name="btns"> Incarca Schita </button>
 </form>
 <!-- display data -->
 <?php
 $stat = $dbh->prepare("select * from players");
 $stat->execute();
 while($row = $stat->fetch()){
     echo "<a target='_blank' href='viewschita.php?id=".$row['id']."'>".$row['name']."</a>";
 }

 ?>

问题是我不知道怎么链接到文件,知道怎么做吗?

f3temu5u

f3temu5u4#

您需要更正insert查询。您缺少“into”关键字。将查询更改为:

mysql_query("INSERT into players SET name='$name', schita='$schita'");

相关问题