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
You have a typo in the first if condition. It should be
This results in null values which fails to execute your query
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):
The html form (index.php):
The PHP – in index.php just below the form: