Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php MySql:如何在数据库中存储文件?_Php_Mysql_Mysqli - Fatal编程技术网

Php MySql:如何在数据库中存储文件?

Php MySql:如何在数据库中存储文件?,php,mysql,mysqli,Php,Mysql,Mysqli,它似乎无法上传到数据库这有什么问题 这是我的密码 这是连接到数据库的文件connectiontwo.php <?php $servername = "localhost"; $username = "root"; $password = ""; $dbname = "uphslletreviewer"; // Create connection $conn = new mysqli($servername, $username, $password, $dbname); /

它似乎无法上传到数据库这有什么问题

这是我的密码

这是连接到数据库的文件connectiontwo.php

<?php
 $servername = "localhost";
 $username = "root";
 $password = "";
 $dbname = "uphslletreviewer";

 // Create connection
 $conn = new mysqli($servername, $username, $password, $dbname);
 // Check connection
 if ($conn->connect_error) {
 die("Connection failed: " . $conn->connect_error);
 }

 ?>
<?php
 include_once 'connectiontwo.php';
 if(isset($_POST['btn-upload']))
 {    

 $file = rand(1000,100000)."-".$_FILES['file']['name'];
 $file_loc = $_FILES['file']['tmp_name'];
 $file_size = $_FILES['file']['size'];
 $file_type = $_FILES['file']['type'];
 $folder="uploads/";

 // new file size in KB
 $new_size = $file_size/1024;  
 // new file size in KB

 // make file name in lower case
 $new_file_name = strtolower($file);
 // make file name in lower case

 $final_file=str_replace(' ','-',$new_file_name);

 if(move_uploaded_file($file_loc,$folder.$final_file))
 {
 $sql="INSERT INTO module_let(module_desc,type,size)       VALUES('$final_file','$file_type','$new_size')";
 $conn->query($sql);
 ?>
 <script>
  alert('successfully uploaded');
    window.location.href='indexsample.php?success';
    </script>
<?php
 }
else
 {
?>
<script>
alert('error while uploading file');
    window.location.href='indexsample.php?fail';
    </script>
<?php
}
}
?>

这是可以上传到数据库的代码

<?php
 $servername = "localhost";
 $username = "root";
 $password = "";
 $dbname = "uphslletreviewer";

 // Create connection
 $conn = new mysqli($servername, $username, $password, $dbname);
 // Check connection
 if ($conn->connect_error) {
 die("Connection failed: " . $conn->connect_error);
 }

 ?>
<?php
 include_once 'connectiontwo.php';
 if(isset($_POST['btn-upload']))
 {    

 $file = rand(1000,100000)."-".$_FILES['file']['name'];
 $file_loc = $_FILES['file']['tmp_name'];
 $file_size = $_FILES['file']['size'];
 $file_type = $_FILES['file']['type'];
 $folder="uploads/";

 // new file size in KB
 $new_size = $file_size/1024;  
 // new file size in KB

 // make file name in lower case
 $new_file_name = strtolower($file);
 // make file name in lower case

 $final_file=str_replace(' ','-',$new_file_name);

 if(move_uploaded_file($file_loc,$folder.$final_file))
 {
 $sql="INSERT INTO module_let(module_desc,type,size)       VALUES('$final_file','$file_type','$new_size')";
 $conn->query($sql);
 ?>
 <script>
  alert('successfully uploaded');
    window.location.href='indexsample.php?success';
    </script>
<?php
 }
else
 {
?>
<script>
alert('error while uploading file');
    window.location.href='indexsample.php?fail';
    </script>
<?php
}
}
?>

您应该检查查询是否成功,如果不成功,请在某处记录/打印错误

if($conn->query($sql) === TRUE) {
     // successful
} else {
     // Errorhandling
     // for finding the error a simple
     echo $conn->errno.": ".$conn->error
}
正如Sami已经提到的,您应该真正使用准备好的语句:

什么不起作用?错误?该怎么办?请提供更多信息。也不要连接SQL字符串,始终使用参数。将文件存储在数据库中通常不是一个好主意,为什么不使用文件系统并仅存储数据库中的路径?最终的文件类型和新的文件大小不能上载到database@C他正在存储路径。出什么事了?不幸的是,Stack Overflow不是一个代码检查站点,您可以在这里询问代码出了什么问题,这会使您的代码偏离主题。你应该自己调试你的代码。