好的,我有一个脚本,可以将表单中的数据插入数据库并上传图片,将所有信息插入数据库但不上传图片。
代码如下,我是PHP的新手,所以非常感谢您的建议。
谢谢: - )
//This is the directory where images will be saved
$target = "../restaurants/images/";
$target = $target . basename( $_FILES['photo']);
//This gets all the other information from the form
$rest_name=$_POST['rest_name'];
$address1=$_POST['address1'];
$address2=$_POST['address2'];
$town=$_POST['town'];
$postcode=$_POST['postcode'];
$open_time=$_POST['open_time'];
$halal=$_POST['halal'];
$deliver_areas=$_POST['deliver_areas'];
$deliver_time=$_POST['deliver_time'];
$type=$_POST['type'];
$photo=($_FILES['photo']);
mysql_select_db(restaurants);
//Writes the information to the database
mysql_query("INSERT INTO `restaurants` VALUES ('', '$rest_name', '$address1', '$address2', '$town', '$postcode', '$open_time', '$halal', '$deliver_areas', '$deliver_time', '$type', '$photo')") ;
if( mysql_errno() != 0){
// mysql error
// note: message like this should never appear to user, should be only stored in log
echo "Mysql error: " . htmlspecialchars( mysql_error());
die();
}
//Writes the photo to the server
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))
{
//Tells you if its all ok
header('Location: add-restaurant.php?added');
}
else {
//Gives and error if its not
header('Location: add-restaurant.php?fail');
}
答案 0 :(得分:0)
使用以下代码。希望它会对你有所帮助。
if ($_FILES["photo"]["size"] < 30000) // specify file size here file size
{
if ($_FILES["photo"]["error"] > 0)
{
echo "Return Code: " . $_FILES["photo"]["error"] . "<br />";
}
else
{
if (file_exists("../restaurants/images/" . $_FILES["photo"]["name"]))
{
echo $_FILES["photo"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["photo"]["tmp_name"],"../restaurants/images/" . $_FILES["photo"]["name"]);
$rest_name=$_POST['rest_name'];
$address1=$_POST['address1'];
$address2=$_POST['address2'];
$town=$_POST['town'];
$postcode=$_POST['postcode'];
$open_time=$_POST['open_time'];
$halal=$_POST['halal'];
$deliver_areas=$_POST['deliver_areas'];
$deliver_time=$_POST['deliver_time'];
$type=$_POST['type'];
$photo=$_FILES["photo"]["name"] ;
mysql_select_db(restaurants);
//Writes the information to the database
mysql_query("INSERT INTO `restaurants` VALUES ('', '$rest_name', '$address1', '$address2', '$town', '$postcode', '$open_time', '$halal', '$deliver_areas', '$deliver_time', '$type', '$photo')") ;
if( mysql_errno() != 0)
{
// mysql error
// note: message like this should never appear to user, should be only stored in log
echo "Mysql error: " . htmlspecialchars( mysql_error());
die();
}
}
}
}