gpt4 book ai didi

PHP表单插入Mysql,以同一表单发送和上传

转载 作者:行者123 更新时间:2023-11-29 17:54:44 24 4
gpt4 key购买 nike

我有一个 php 表单,工作时会插入数据库、电子邮件通知,并允许可选的文档上传。数据库和邮件部分有效,但文件上传无效。有一次,我确实在单独的页面上进行了文件上传。现在我有'error_reporting(-1);'在 php.ini 和 Apache 日志文件中,没有显示任何内容来指示故障所在。我是否试图以同样的形式做太多事情?有没有办法获得更详细的错误报告以找出问题所在?

<?php
include('connect.php');
if((isset($_POST['username']) && !empty($_POST['username']))
&& (isset($_POST['email']) && !empty($_POST['email']))
&& (isset($_POST['city']) && !empty($_POST['city']))
&& (isset($_POST['state']) && !empty($_POST['state']))
&& (isset($_POST['file1']) && !empty($_POST['file1']))
&& (isset($_POST['file2']) && !empty($_POST['file2']))){
//if((isset($_POST['username']) && (!empty($_POST['username']))) && (isset($_POST['email']) && !empty($_POST['email'])) && (isset($_POST['subject']) && !empty($_POST['subject']))){
print_r($_POST);
$username = $_POST['username'];
$email = $_POST['email'];
$city = $_POST['city'];
$state = $_POST['state'];

$name= $_FILES['file']['name'];
$tmp_name= $_FILES['file']['tmp_name'];
$submitbutton= $_POST['submit'];
$position= strpos($name, ".");
$fileextension= substr($name, $position + 1);
$fileextension= strtolower($fileextension);
$file1= $_POST['file1'];

$to = "someone@somewhere.com";
$headers = "From : " . $email;

if( mail($to, $username, $city, $headers)){
echo "<strong>E-Mail Sent successfully, we will get back to you soon.</strong>";

$query = "INSERT INTO `contact` (username, email, city, state, file1, file2) VALUES ('$username', '$email', '$city', '$state', '$zip', '$file1', '$file2')";
$result = mysqli_query($connection, $query);

if (isset($name)) {
$path= 'uploads/';
if (!empty($name)){
if (move_uploaded_file($tmp_name, $path.$name)) {
echo 'Uploaded!';
}
}
}
}
}

?>
<html><head></head><body>
<div class="container">

<form class="form-contact" method="POST" multipart/form-data>

<input type="name" name="username" id="inputusername" class="auto-style7" placeholder="Your Name" required><span class="auto-style6">
<input type="email" name="email" id="inputEmail" class="auto-style7" placeholder="Your E-Mail Address">
<input type="name" name="city" id="inputcity" class="auto-style7" placeholder="Your City">
<input type="name" name="state" id="inputstate" class="auto-style7" placeholder="Your State">

<p></p>
Select file1: <input type="file" name="file1">
Select file2: <input type="file" name="file2">
<br>
<button class="btn btn-lg btn-primary btn-block" type="submit">Send</button></form>
</div>
</body>
</html>

最佳答案

以下是我修改您的原始代码的方法:

<?php
// turn on error reporting
error_reporting(E_ALL);
ini_set('display_errors', 1);

include('connect.php');

if ($_SERVER['REQUEST_METHOD'] === 'POST') {

// trim all POSTed values
$_POST = array_map('trim', $_POST);

// no need to use isset() just use !empty()
// note: empty('') is true, but empty(' ') is false hence why trimming is good to do
if (!empty($_POST['username'])
&& !empty($_POST['email'])
&& !empty($_POST['city'])
&& !empty($_POST['state'])) {

$username = $_POST['username'];
$email = $_POST['email'];
$city = $_POST['city'];
$state = $_POST['state'];

// now let's deal with the file inputs
// you have 2 file inputs with name attributes: file1 and file2

// we'll store only validated files in an array
$files = [];
foreach (['file1', 'file2'] as $name) {
// always check that it is a legit file
// todo: add more validation and sanitization
if (isset($_FILES[$name]['tmp_name'])
&& file_exists($_FILES[$name]['tmp_name'])
&& is_uploaded_file($_FILES[$name]['tmp_name'])
&& $_FILES[$name]['error'] === UPLOAD_ERR_OK) {
$files[$name] = $_FILES[$name];
}
}

$to = "someone@somewhere.com";
$headers = "From : " . $email;

if (mail($to, $username, $city, $headers)) {
echo "<strong>E-Mail Sent successfully, we will get back to you soon.</strong>";
// use prepared statements
// todo: learn about SQL injection
$query = "INSERT INTO contact(username, email, city, state, file1, file2) VALUES (?, ?, ?, ?, ?, ?)";
$stmt = mysqli_prepare($connection, $query);
$stmt->bind_param("ssssss",
$username,
$email,
$city,
$state,
// ensure files were provided before getting their names
isset($files['file1']['name']) ? $files['file1']['name'] : '',
isset($files['file2']['name']) ? $files['file2']['name'] : ''
);

$stmt->execute();

// upload the files
$path= 'uploads/';
foreach (['file1', 'file2'] as $name) {
if (isset($files[$name])
&& move_uploaded_file($files[$name]['tmp_name'], $path . $files[$name]['name'])) {
echo 'Uploaded!';
}
}
}
}
}
?>

您的表单还应该有一个 enctype 属性:

<form class="form-contact" method="POST" enctype="multipart/form-data">

值得一读

关于PHP表单插入Mysql,以同一表单发送和上传,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48955559/

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