图像未上载到数据库

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

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

  1. <?php
  2. include('connect-db.php');
  3. ?>
  4. <?php
  5. function renderForm($name, $schita, $error)
  6. {
  7. ?>
  8. <?php
  9. if ($error != '')
  10. {
  11. echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>';
  12. }
  13. ?>
  14. <form action="" method="post" enctype="multipart/form-data" >
  15. <input type="hidden" name="id" value="<?php echo $id; ?>"/>
  16. <input type="hidden" name="name" value="<?php echo $name; ?>"/>
  17. <input type="file" id="schita" name="schita" >
  18. <button type="submit" name="submit">Add Data</button>
  19. </form>
  20. <?php
  21. }
  22. include('connect-db.php');
  23. if (isset($_POST['submit']))
  24. {
  25. $name = mysql_real_escape_string(htmlspecialchars($_POST['name']));
  26. $schita = mysql_real_escape_string(htmlspecialchars($_POST['schita']));
  27. if ($name == '')
  28. {
  29. $error = 'Error !!';
  30. renderForm($name, $schita, $error);
  31. }
  32. else
  33. {
  34. mysql_query("INSERT players SET name='$name', schita='$schita'")
  35. or die(mysql_error());
  36. header("Location: mobila.php");
  37. }
  38. }
  39. else
  40. {
  41. renderForm('','','','','');
  42. }
  43. ?>

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

57hvy0tb

57hvy0tb1#

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

  1. // Select file type
  2. $target_file = basename($_FILES["file"]["name"]);
  3. $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
  4. // Convert to base64
  5. $image_base64 = base64_encode(file_get_contents($_FILES['schita']['tmp_name']) );
  6. $image = 'data:image/'.$imageFileType.';base64,'.$image_base64;
  7. // Insert record
  8. $query = "INSERT into players(schita) values('".$image."')";
  9. mysqli_query($con,$query);
xqkwcwgp

xqkwcwgp2#

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

ttisahbt

ttisahbt3#

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

  1. <?php
  2. $dbh = new PDO("mysql:host=localhost;dbname=highmob_comenzi", "highmob", "PW");
  3. if(isset($_POST['btns'])){
  4. $name = $_FILES['myfile']['name'];
  5. $type = $_FILES['myfile']['type'];
  6. $data = file_get_contents($_FILES['myfile']['tmp_name']);
  7. $stmt = $dbh->prepare("UPDATE players SET data='$myfile', name='$name', mime='$type' WHERE id='$id'");
  8. $stmt->bindParam(1,$name);
  9. $stmt->bindParam(2,$type);
  10. $stmt->bindParam(3,$data);
  11. $stmt->execute();
  12. }
  13. ?>
  14. <!-- form -->
  15. <form method="post" enctype="multipart/form-data">
  16. <input type="file" name="myfile"/>
  17. <button name="btns"> Incarca Schita </button>
  18. </form>
  19. <!-- display data -->
  20. <?php
  21. $stat = $dbh->prepare("select * from players");
  22. $stat->execute();
  23. while($row = $stat->fetch()){
  24. echo "<a target='_blank' href='viewschita.php?id=".$row['id']."'>".$row['name']."</a>";
  25. }
  26. ?>

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

展开查看全部
f3temu5u

f3temu5u4#

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

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

相关问题