【问题标题】:Php form insert to Mysql, mail & upload in same formphp表单插入到Mysql,以相同的形式发送邮件和上传
【发布时间】:2018-02-23 20:10:54
【问题描述】:

我有一个 php 表单,它在工作时会插入数据库、电子邮件通知,并允许可选地上传文档。 db & mail 部分有效,但文件上传无效。有一次,我确实在单独的页面上进行了文件上传。现在我有'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>

【问题讨论】:

  • 顶部添加:ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL);,报错。
  • 不应该说enctype='multipart/form-data'
  • 旁注:检查字段时使用isset!empty 是多余的。您只需要 !empty 检查,因为它会进行自己的 isset 检查。
  • 输出$_FILES的内容,有什么吗?
  • 如果您没有隐藏的输入,那么只需遍历 $_POST 并确保每个都不为空。有很多代码可以检查是否有空。此外,尝试简单地使用 HTML5 并将 required 添加到输入中,这样可以节省您的手指输入时间。

标签: php mysql


【解决方案1】:

我进行了更正,它有 50% 的工作。无论我选择 1 个文件还是 2 个文件,File2 都会上传,但 'file1' 永远不会起作用。

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

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


<html>
Select file1: <input type="file" multiple="multiple" name="file1"> 
Select file2e: <input type="file" multiple="multiple" name="file2">
</html> 

我有错误报告、启动错误等。所有设置,但仍然没有任何记录,好像没有错误一样。

【讨论】:

    【解决方案2】:

    以下是我将如何修改您的原始代码:

    <?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">
    

    很好读

    【讨论】:

      猜你喜欢
      • 2012-08-16
      • 1970-01-01
      • 2019-06-03
      • 1970-01-01
      • 1970-01-01
      • 2014-03-21
      • 1970-01-01
      • 2013-11-10
      • 2018-01-24
      相关资源
      最近更新 更多