我有一个包含file_name和file_path的预订表,我需要根据noic将图片上传到用户行,在我选择图片并单击上传按钮后,它显示上传成功,但在数据库中没有& #39; t有图片和图片名称。
$target = "upload/";
$target = $target . basename( $_FILES['file']['name']);
//This gets all the other information from the form
$file=basename( $_FILES['file']['name']);
$filename=$_POST['file_name'];
//Writes the file to the server
if(move_uploaded_file($_FILES['file']['tmp_name'], $target)) {
//Tells you if its all ok
echo "The file ". basename( $_FILES['file']['name']). " has been uploaded, and your information has been added to the directory";
// Connects to your Database
//Writes the information to the database
mysql_query("UPDATE INTO booking (file_path,file_name)
VALUES ('$file', '$filename') WHERE noic = '$_SESSION[noic]'") ;
} else {
//Gives and error if its not
echo "Sorry, there was a problem uploading your file.";
}
<form enctype="multipart/form-data" action="userstatus.php" method="POST">
<p> File Name :
<input style="background:grey;" type="text" name="file_name" value="" >
</p>
<p>
<input type="file" name="file" >
</p>
<p>
<input type="submit" name="submit" value="Upload file" style="background:grey;">
</p>
</form>
答案 0 :(得分:0)
您是否尝试过运行查询
UPDATE INTO booking (file_path,file_name) VALUES ('$file', '$filename') WHERE noic = '(replace here the noic you want)'
在phpMyAdmin或MySql Workbench上?
为什么不尝试像这样运行查询:
UPDATE booking SET file_path = '$file', SET file_name= '$filename' WHERE noic = '$_SESSION[noic]'
同时检查您是否已将775权限设置为/ upload文件夹..
希望我帮助你!
答案 1 :(得分:0)
您的MySQL查询不正确,应该是UPDATE SET ... MySQL Insert语句不支持WHERE子句,因此如果直接用INSERT替换UPDATE,查询将失败,进行以下更改,
//Writes the information to the database
mysql_query("UPDATE booking SET file_path='$file',
file_name='$filename' WHERE noic = '$_SESSION[noic]'") ;
}
注意:请避免使用php mysql *类进行数据库查询,它已被弃用且不应使用,请切换到PDO或MySQLi。 PDO参考 - http://php.net/manual/en/book.pdo.php