我创建了一个网络表单,允许用户请求查看我的简历,一旦用户点击提交,我想将其输入存储在phpMyAdmin中的数据库中。由于我是PHP的新手,并且在html文档中使用数据库,因此获得了一些代码来进行复制和更改,以匹配表单字段。当我点击提交时,它直接进入process_CV_request.PHP文件中的else语句。
我的数据库由自动递增的用户ID,名字,姓氏,电子邮件ID,公司名称,用户注释和cvtype(长或短)组成
我的表格
<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>
我用来连接数据库的db.php
<?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文件
<?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>
如果所有作品都可以,那么我想向用户显示他们输入的公司名称,评论和下载他们选择的cvtype的链接。谢谢
答案 0 :(得分:0)
因此,我使用PDO而不是mysqli重新创建了一个简单的页面版本。希望它有助于解决您的困境。如果您发现这种方法更容易理解,我鼓励您更多地了解PDO。
使用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)
);
html格式(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>
PHP-在index.php中,格式如下:
<?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
}
答案 1 :(得分:-1)
请勿使用query()
,请尝试mysqli_query()
。
答案 2 :(得分:-1)
您在第一个if条件中有错字。应该是
if($_SERVER["REQUEST_METHOD"] == "POST")
这将导致空值,从而无法执行查询