skip to Main Content

I have created a webform to allow a user to request to see my CV, once the user hits submit I want to store their input inside a database in phpMyAdmin. Since I am new to PHP and using databases within a html document, I was given some code to copy and change to match my form fields. When I hit submit it goes straight to the else statement within the process_CV_request.PHP file.

My database consists of userid which auto increments, firstname, surname, emailid, companyname, usercomment and cvtype(Long or Short)

My Form

    <body>
        <div class="contact-title">
            <h1 >CV Request</h1>
        </div>
        <div>
            <form id="contact-form" action="process_CV_requests.php" method="post" action="">

                <input type="text" name="FirstName" class="form-control" placeholder="Your First Name"><br>

                <input type="text" name="Surname" class="form-control" placeholder="Your Surname"><br>

                <input type="text" name="CompanyName" class="form-control" placeholder="Your Company Name"><br>

                <input type="text" name="EmailAddress" class="form-control" placeholder="Your Email Address"><br>

                <textarea name="comment" class="form-control" placeholder="Leave a Comment" rows="5"></textarea><br>

                <p class="cvType">CV: Short <input type="radio" name="cvType" value="Short" checked> Long <input type="radio" name="cvType" value="Long"><br></p>

                <input type="submit" class="form-control submit" value="Submit">
            </form>
        </div>
    </body>
    </html>

My db.php which I use to connect to my database

<?php

    error_reporting( error_reporting() & ~E_NOTICE);

    $db_location = "localhost";
    $db_username = "Username";
    $db_password = "password";
    $db_database = "nameofmydatabase";
    $db_connection = new mysqli("$db_location", "$db_username", "$db_password");

    if ($db_connection->connect_error){
        die("Connection failed: " . $db_connection->connect_error);
    }
    $db = mysqli_select_db($db_connection, $db_database)
        or die ("Error - could not open database");


?>

process_CV_request.PHP file

<?php

require_once "db.php";

    if($SERVER["REQUEST_METHOD"] == "POST")
    {
        $erremail = $errfirstname = $errsurname = $errCVtype = $errCompanyname = "";
        $email = $firstname = $surname = $usercomment = $cvtype = $companyname = "";

        $firstname = mysqli_real_escape_string($db_connection, $_POST["firstname"]);
        $surname = mysqli_real_escape_string($db_connection, $_POST["surname"]);
        $companyname = mysqli_real_escape_string($db_connection, $_POST["company"]);
        $email = mysqli_real_escape_string($db_connection, $_POST["emailid"]);
        $cvtype = mysqli_real_escape_string($db_connection, $_POST["cvchoice"]);

        $usercomment = mysqli_real_escape_string($db_connection, $_POST["usercomment"]);
    }
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
    <title>PHP AND MySQLi Thank you message.</title>
</head>
<body>

<?php
    if($_SERVER["REQUEST_METHOD"] == "POST")
    {
        $qry ="insert into cv_requests(firstname, surname, companyname, emailid, usercomment, cvrequested)
            values('$firstname','$surname','$companyname','$usercomment','$email',$cvtype');";
        $res = $db_connection->query($qry);
        if($res)
        {
            echo "<p>Thank you for requesting to see my CV</p>";
            echo "<p>Your company name: <strong>".$companyname."</strong></p>";
            echo "<p>Your comment: <strong>".$usercomment."</strong></p>";
            echo "<p><a href='files/";
            if($cvtype === 'short')
                echo "Short_CV";
            else
                echo "Long_CV";
            echo ".pdf' target='_blank'>view my ".$cvtype." CV</a></p>";
            exit();
        }
        else
        {
            echo "<p>Error occured, please try again.</p>";
            exit();
        }

    }
$db_connection->close();    
?>
</body>
</html>

If all works then I want to display to the user their company name that they entered, their comment and a link to download the cvtype that they selected. Thanks

2

Answers


  1. You have a typo in the first if condition. It should be

    if($_SERVER["REQUEST_METHOD"] == "POST")
    

    This results in null values which fails to execute your query

    Login or Signup to reply.
  2. So I recreated a simple one page version of this using PDO instead of mysqli. Hopefully it helps towards your predicament. I would encourage learning more about PDO if you find this method easier to understand.

    Create a basic table in your db with SQL (phpmyadmin):

    create table cv_requests (
        userid int not null auto_increment primary key,
        firstname varchar(255),
        surname varchar(255),
        companyname varchar(255),
        emailid varchar(255),
        usercomment text,
        cvrequested tinyint(1)
    );
    

    The html form (index.php):

    <div>
    
        <form id="contact-form" action="index.php" method="post">
    
            <input type="text" name="first_name" class="form-control" placeholder="Your First Name"><br>
    
            <input type="text" name="surname" class="form-control" placeholder="Your Surname"><br>
    
            <input type="text" name="company_name" class="form-control" placeholder="Your Company Name"><br>
    
            <input type="text" name="email" class="form-control" placeholder="Your Email Address"><br>
    
            <textarea name="comment" class="form-control" placeholder="Leave a Comment" rows="5"></textarea><br>
    
            <p class="cvType">CV: Short <input type="radio" name="cv_type" value="Short" checked> Long <input type="radio" name="cv_type" value="Long"><br></p>
    
            <input type="submit" class="form-control submit" value="Submit">
    
        </form>
    
    </div>
    

    The PHP – in index.php just below the form:

    <?php
    
    // function to connect to the database
    function connect($dbhost, $dbname, $dbuser, $dbpassword) {
    
        // try to connect, if not end the script
        try {
    
            return new PDO('mysql:host=' . $dbhost . ';dbname=' . $dbname, $dbuser, $dbpassword);
    
        } catch (PDOException $e) {
    
            die($e->getMessage());
    
        }
    
    }
    
    // a new PDO instance - enter db credentials
    $pdo = connect('localhost', 'test_db', 'root', '');
    
    // if somethings been posted to the page
    if ($_POST) {
    
        // set variables to post values - for use binding paramaters
        $first = $_POST['first_name'];
        $last = $_POST['surname'];
        $company = $_POST['company_name'];
        $email = $_POST['email'];
        $comment = $_POST['comment'];
    
        if (isset($_POST['cv_type'])) {
    
            if ($_POST['cv_type'] == 'Short') : $cv_type = 0; endif;
    
            if ($_POST['cv_type'] == 'Long') : $cv_type = 1; endif;
    
        }
    
        // prepare a new sql query
        $insert = $pdo->prepare('insert into cv_requests (firstname, surname, companyname, emailid, usercomment, cvrequested) values (:first, :last, :company, :email, :comment, :type)');
    
        // binds all of the parameters to be inserted into the db to the vars we set earlier
        $insert->bindParam(':first', $first);
        $insert->bindParam(':last', $last);
        $insert->bindParam(':company', $company);
        $insert->bindParam(':email', $email);
        $insert->bindParam(':comment', $comment);
        $insert->bindParam(':type', $cv_type);
    
        // insert into the database
        $insert->execute(); ?>
    
        <p>Thank you for requesting to see my CV</p>
        <p>Your company name: <strong><?= $company; ?></strong></p>
        <p>Your comment: <strong><?= $comment; ?></strong></p>
    
        <?php $cv = $cv_type ? 'Long' : 'Short'; ?>
    
        <a href='files/<?= $cv; ?>.pdf' target='_blank'>View my <?= $cv; ?> CV</a>
    
        <?php
    
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search