gpt4 book ai didi

php - 如何设置它以便不必上传表单中的文件即可将表单的其余数据插入 mysql 数据库?

转载 作者:行者123 更新时间:2023-11-30 01:11:31 26 4
gpt4 key购买 nike

我已经到处搜索了几天,但似乎无法找到解决方案。

我正在用 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com