我已经到处搜索了几天,但似乎找不到解决方案。
我正在用PHP / mySQL建立用户博客。
我想允许用户上传新的博客文章(又名标题,博客和照片)。
一切工作正常,除非他们没有选择要上传的照片,脚本将不会解析,并且什么也不会插入数据库。
我已经发布了解析脚本文件。
即使未选择照片,如何将信息插入数据库?
//Get photo details
?><?php
if (isset($_FILES["blog_photo"]["name"]) && $_FILES["blog_photo"]["tmp_name"] != ""){
$fileName = $_FILES["blog_photo"]["name"];
$fileTmpLoc = $_FILES["blog_photo"]["tmp_name"];
$fileType = $_FILES["blog_photo"]["type"];
$fileSize = $_FILES["blog_photo"]["size"];
$fileErrorMsg = $_FILES["blog_photo"]["error"];
$kaboom = explode(".", $fileName);
$fileExt = end($kaboom);
list($width, $height) = getimagesize($fileTmpLoc);
if($width < 10 || $height < 10){
header("location: ../message.php?msg=ERROR: That image has no dimensions");
exit();
}
//Restrict photo uploads
$db_file_name = rand(100000000000,999999999999).".".$fileExt;
if($fileSize > 3048576) {
header("location: ../message.php?msg=ERROR: Your image file was larger than 3mb");
exit();}
if (!preg_match("/\.(gif|jpg|png)$/i", $fileName) ) {
header("location: ../message.php?msg=ERROR: Your image file was not jpg, gif or png type");
exit();
} else if ($fileErrorMsg == 1) {
header("location: ../message.php?msg=ERROR: An unknown error occurred");
exit();
}
$moveResult = move_uploaded_file($fileTmpLoc, "../user/$log_id/$db_file_name");
if ($moveResult != true) {
header("location: ../message.php?msg=ERROR: File upload failed");
exit();
}
include_once("../php_includes/image_resize.php");
$target_file = "../user/$log_id/$db_file_name";
$resized_file = "../user/$log_id/$db_file_name";
$wmax = 600;
$hmax = 400;
img_resize($target_file, $resized_file, $wmax, $hmax, $fileExt);
//define variables from form to insert into database
$bh =$_POST['blog_heading_a'];
$bt =$_POST['blog_text_a'];
$u =$_POST['user_id'];
$a =$_POST['blog_photo'];
//insert into database
$insertSQL ="INSERT INTO blog (blog_heading, blog_photo, blog_day, blog_text, user_id) VALUES ('$bh','$db_file_name',NOW(),'$bt','$u')";
mysql_select_db($database_database_conn, $database_conn);
$Result1 = mysql_query($insertSQL, $database_conn) or die(mysql_error());
header("location: ../user.php?u=$log_id");
exit();
}
?>
最佳答案
您需要在if()
之外进行数据库插入,因为该代码仅在上传照片后才会执行。
<?php
if (isset($_FILES["blog_photo"]["name"]) && $_FILES["blog_photo"]["tmp_name"] != ""){
$fileName = $_FILES["blog_photo"]["name"];
$fileTmpLoc = $_FILES["blog_photo"]["tmp_name"];
$fileType = $_FILES["blog_photo"]["type"];
$fileSize = $_FILES["blog_photo"]["size"];
$fileErrorMsg = $_FILES["blog_photo"]["error"];
$kaboom = explode(".", $fileName);
$fileExt = end($kaboom);
list($width, $height) = getimagesize($fileTmpLoc);
if($width < 10 || $height < 10){
header("location: ../message.php?msg=ERROR: That image has no dimensions");
exit();
}
//Restrict photo uploads
$db_file_name = rand(100000000000,999999999999).".".$fileExt;
if($fileSize > 3048576) {
header("location: ../message.php?msg=ERROR: Your image file was larger than 3mb");
exit();}
if (!preg_match("/\.(gif|jpg|png)$/i", $fileName) ) {
header("location: ../message.php?msg=ERROR: Your image file was not jpg, gif or png type");
exit();
} else if ($fileErrorMsg == 1) {
header("location: ../message.php?msg=ERROR: An unknown error occurred");
exit();
}
$moveResult = move_uploaded_file($fileTmpLoc, "../user/$log_id/$db_file_name");
if ($moveResult != true) {
header("location: ../message.php?msg=ERROR: File upload failed");
exit();
}
include_once("../php_includes/image_resize.php");
$target_file = "../user/$log_id/$db_file_name";
$resized_file = "../user/$log_id/$db_file_name";
$wmax = 600;
$hmax = 400;
img_resize($target_file, $resized_file, $wmax, $hmax, $fileExt);
} else {
$db_file_name = '';
}
//define variables from form to insert into database
$bh =$_POST['blog_heading_a'];
$bt =$_POST['blog_text_a'];
$u =$_POST['user_id'];
$a =$_POST['blog_photo'];
//insert into database
$insertSQL ="INSERT INTO blog (blog_heading, blog_photo, blog_day, blog_text, user_id) VALUES ('$bh','$db_file_name',NOW(),'$bt','$u')";
mysql_select_db($database_database_conn, $database_conn);
$Result1 = mysql_query($insertSQL, $database_conn) or die(mysql_error());
header("location: ../user.php?u=$log_id");
exit();
?>
关于php - 我如何设置它,这样就不必上传表单中的文件即可将其余的表单数据插入mysql数据库中?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19439598/