PDO未将表单数据输入数据库

时间:2018-07-17 19:33:25

标签: php mysql pdo

我正在尝试将表单数据输入数据库。我使用的代码几乎与注册脚本的代码相同,效果很好。在这一点上,我完全感到困惑。

我为PHP和PDO打开了错误报告,没有任何反应。发送表单后,它似乎可以工作(除非没有出现确认消息),但是没有任何内容输入数据库。

我有两个文件,request.php(窗体)和parseRequest.php(窗体的后端)。

request.php

<form action="" method="post">
            <div class="form-group">
                <input type="hidden" class="form-control" name="username" id="usernameField" value="<?php echo $_SESSION['username'];?>">
            </div>
            <div class="form-group">
                <label>Headlining Band/Artist</label>
                <input type="text" class="form-control" name="artist" id="artistField" placeholder="Artist">
            </div>
            <div class="form-group">
                <label>Date</label>
                <input type="text" class="form-control" name="day" id="dateField" placeholder="MM/DD/YYYY">
            </div>
            <div class="form-group">
                <label>Venue</label>
                <input type="text" class="form-control" name="venue" id="venueField" placeholder="Venue">
            </div>
            <div class="form-group">
                <label>City, State</label>
                <input type="text" class="form-control" name="city" id="cityField" placeholder="City, State">
            </div>
            <input type="hidden" name="token" value="<?php if(function_exists('_token')) echo _token(); ?>">
            <button type="submit" name="requestBtn" class="btn btn-primary pull-right">Submit</button>

parseRequest.php

<?php
include_once 'resource/Database.php';
include_once 'resource/utilities.php';
include_once 'resource/send-email.php';
// Processing the form

if(isset($_POST['requestBtn'], $_POST['token'])){
    if(validate_token($_POST['token'])) {
        //process form here
        $form_errors = "";

    // validation
    $required_fields = array('artist', 'day', 'venue', 'city');

    // check empty fieldset
    $form_errors = check_empty_fields($required_fields);

    // date check
    $fields_to_check_length = array('day' => 10);

    //call the function to check minimum required length and merge the return data into form_error array
    $form_errors = array_merge($form_errors, check_min_length($fields_to_check_length));

    // collect data
    $username = $_POST['username'];
    $artist = $_POST['artist'];
    $day = $_POST['day'];
    $venue = $_POST['venue'];
    $city = $_POST['city'];
}

else if(empty($form_errors))
{
    // preparing and inputting data
    try
    {
  $sqlInsert = "INSERT INTO requests(username, artist, day, venue, city)
VALUES (:username, :artist, :day, :venue, :city)";

  //use PDO prepared to sanitize data
  $statement = $db->prepare($sqlInsert);

  //add the data into the database
  $statement->execute(array(':username' => $username, ':artist' => $artist, ':day' => $day, ':venue' => $venue, ':city' => $city));

        // email confirmation

        $addresses = array($_SESSION['email'], 'codylkaczynski@gmail.com');

        //prepare email body
        $mail_body = '<html>
        <body style="font-family: Arial, Helvetica, sans-serif;
                            line-height:1.8em;">
        <h2>Amped Sound Staff Portal: Request Received</h2>
        <p>Dear '.$username.'<br><br>
        Your request for the '.$artist.' show in '.$city.' on '.$date.' has been received!</p><br/>
        <p>We will let you know if your request has been approved or denied ASAP.</p><br/>
        <p>Thank you!</p><br/>
        <p><strong>&copy;2018 Amped Sound</strong></p>
        </body>
        </html>';

        $namejeff = explode(',', $addresses);

        foreach ($addresses as $address)
        {
            $mail->AddAddress($address);
            $mail->Subject = "Request Received!";
            $mail->Body = $mail_body;
        }

        //Error Handling for PHPMailer
        if(!$mail->Send())
        {
            $result = "<script type=\"text/javascript\">swal(\"Error\",\" Email sending failed: $mail->ErrorInfo \",\"error\");</script>";
        }
        else
        {
        $result = "<script type=\"text/javascript\">
            swal({
            title: \"Request received!\",
            text: \"We have received your request! Please check your email for confirmation.\",
            type: 'success',
            confirmButtonText: \"Thank You!\" });
        </script>";
        }
    }

    catch (PDOException $ex)
    {
        $result = flashMessage("An error occurred: " .$ex->getMessage());
    }
 }
}

我很高兴能获得帮助。我已经尝试了很多在StackOverflow上发现的解决方案,但无济于事。

0 个答案:

没有答案