因此,我尝试执行此文件上传,并将文件大小,名称和URL等信息存储在数据库中,同时将文件上传到计算机上的文件夹(用于测试目的)。它的上传没有问题,我唯一的问题是我想要的信息不会存储在数据库中。
if($_SERVER['REQUEST_METHOD']=='POST') {
$target_dir = "uploads/";
$file_name=basename($_FILES["fileToUpload"]["name"]);
$target_file = $target_dir . $file_name;
$fileSize=$_FILES["fileToUpload"]["size"];
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
$complete=$file_name.$imageFileType;
$myUrl=$target_dir.$complete;
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 10000000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "pdf" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
echo "Sorry, only PDF, JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
$mysqli=connect();
$stmt=$mysqli->prepare('INSERT INTO tbl_file (file_name,file_title,file_size,file_url) VALUES (?,?,?,?)') or die(mysqli_error());
$stmt->execute();
$stmt->bind_param('ssis',$complete,$file_name,$fileSize,$myUrl);
$stmt->close();
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.<br>";
} else {
echo "Sorry, there was an error uploading your file.";
}
}}
它必须与SQL语句有关,但我不知道它是什么,有什么想法吗?
答案 0 :(得分:0)
我看到你在调用execute()之后调用了bindParameters()方法。 它应该是相反的。
即
$stmt->bind_param('ssis',$complete,$file_name,$fileSize,$myUrl);
$stmt->execute();
...
答案 1 :(得分:0)
我想也许是因为您使用了错误的方法来打开与MySQL服务器的新连接。
您可以像这样创建一个新连接:
$mysqli=new mysqli("dbhost","username","passwd","dbname");
或
$mysqli=new mysqli();
$mysqli->connect("dbhost","username","passwd","dbname");
或
$connection = mysqli_connect("dbhost","username","passwd","dbname");
此外,您必须首先绑定参数,然后执行sql。