如何在多个文件输入中将数据插入数据库?
How to insert data into database in a multiple file input?
我有一个代码可以让一个人通过表单上传多张图片。
问题是,图片可以很好地上传到服务器,但不确定如何将图片发送到数据库中。
PHP:
else{ // No error found! Move uploaded files
if(move_uploaded_file($_FILES["files"]["tmp_name"][$f], $targetscreenshots.$name))
$count++; // Number of successfully uploaded file
}
我应该把下面的代码放在哪里?
{
mysql_query("INSERT into Colleges (`files`) VALUES ('$files')"); // inserting data if file is moved
echo "Your screenshots have been uploaded successfully!"
}
您缺少负责修改数据库的代码,建议您阅读一些教程,例如this one。
我还没有测试过,但至少看起来它包含了所有需要的步骤。
$files = $_FILES["files"]["tmp_name"][$f]
只需在数据库中插入文件路径或名称
这是我在脚本中使用的自己的代码。
<?php
$upath="../images/";
//uploads is the name of file array that is being uploaded.
foreach ($_FILES['uploads']['name'] as $key=>$file) {
$target = $upath.$file;
$path=substr($target,3);
// echo $path; THIS CAN BE STORED DIRECTLY TO THE DATABASE
move_uploaded_file($_FILES['uploads']['tmp_name'][$key], $target)
or die();
mysql_query(**YOUR INSERT QUERY HERE. IT WONT BE EXECUTED IF IMAGE IS NOT UPLOADED PROPERLY.**)or die(mysql_error());
}
?>
我看了你的评论,所以我给出了这个答案...如果我误解了你的问题,请纠正我。
我有一个代码可以让一个人通过表单上传多张图片。
问题是,图片可以很好地上传到服务器,但不确定如何将图片发送到数据库中。
PHP:
else{ // No error found! Move uploaded files
if(move_uploaded_file($_FILES["files"]["tmp_name"][$f], $targetscreenshots.$name))
$count++; // Number of successfully uploaded file
}
我应该把下面的代码放在哪里?
{
mysql_query("INSERT into Colleges (`files`) VALUES ('$files')"); // inserting data if file is moved
echo "Your screenshots have been uploaded successfully!"
}
您缺少负责修改数据库的代码,建议您阅读一些教程,例如this one。 我还没有测试过,但至少看起来它包含了所有需要的步骤。
$files = $_FILES["files"]["tmp_name"][$f]
只需在数据库中插入文件路径或名称
这是我在脚本中使用的自己的代码。
<?php
$upath="../images/";
//uploads is the name of file array that is being uploaded.
foreach ($_FILES['uploads']['name'] as $key=>$file) {
$target = $upath.$file;
$path=substr($target,3);
// echo $path; THIS CAN BE STORED DIRECTLY TO THE DATABASE
move_uploaded_file($_FILES['uploads']['tmp_name'][$key], $target)
or die();
mysql_query(**YOUR INSERT QUERY HERE. IT WONT BE EXECUTED IF IMAGE IS NOT UPLOADED PROPERLY.**)or die(mysql_error());
}
?>
我看了你的评论,所以我给出了这个答案...如果我误解了你的问题,请纠正我。