从PHP表单保存到MySQL
我的数据库结构如下所示; 我的代码如下所示从PHP表单保存到MySQL,php,mysql,sql,mysqli,Php,Mysql,Sql,Mysqli,我的数据库结构如下所示; 我的代码如下所示 <?php $email_sql = mysqli_connect('localhost', 'root', 'root', 'myremovalsquote'); // Check connection if ($email_sql->connect_error) { die("Connection failed: " . $email_sql->connect_error); } else {
<?php
$email_sql = mysqli_connect('localhost', 'root', 'root', 'myremovalsquote');
// Check connection
if ($email_sql->connect_error) {
die("Connection failed: " . $email_sql->connect_error);
} else { echo 'connected'; }
?>
<?php get_header(); ?>
<section>
<div class="container">
<div class="row">
<div class="col-md-12">
<article>
<h1>Get Your FREE Quotes Now!</h1>
<div class="post">
<form method="post" action="<?php bloginfo('url') ?>/quote">
<ul id="quoteform">
<li class="row">
<div class="col-md-12">
<h2>Where are you moving from?</h2>
<select name="frompc">
<option value="ab">Aberdeen (AB)</option>
<option value="al">St Albans (AL)</option>
</select>
<p>What type of property are you moving from?</p>
<select name="fromtype">
<option value="storage">Storage</option>
<option value="bungalow">Bungalow</option>
<option selected value="house">House</option>
<option value="multistory">Multi-Story</option>
</select>
</div>
</li>
<li class="row">
<div class="col-md-12">
<h2>Move details</h2>
<input type="text" name="movedate" placeholder="DD/MM/YYYY">
<p>Is this date flexible?
<select name="flexible">
<option value="yes">Yes</option>
<option value="no">No</option>
</select></p>
</div>
</li>
<li class="row">
<div class="col-md-12">
<h2>Where are you moving to?</h2>
<select name="topc">
<option value="ab">Aberdeen (AB)</option>
<option value="al">St Albans (AL)</option>
</select>
<select name="totype">
<option value="storage">Storage</option>
<option value="bungalow">Bungalow</option>
<option selected value="house">House</option>
<option value="multistory">Multi-Story</option>
</select>
</div>
</li>
<li class="row">
<div class="col-md-12">
<h2>Extra Services</h2>
<input type="checkbox" name="packaging" value="Packaging">
<input type="checkbox" name="packing" value="Packing">
<input type="checkbox" name="furniture" value="Dismantle & Reassemble Furniture">
</div>
</li>
<li class="row">
<div class="col-md-12">
<h2>Notes / Additions</h2>
<textarea name="notes">Enter text here...</textarea>
</div>
</li>
<li class="row">
<div class="col-md-12">
<h2>Your Details</h2>
Full Name
<input type="text" name="fullname" value="Joe Bloggs">
Contact Number
<input type="text" name="contactnumber">
Email Address
<input type="email" name="email">
</div>
</li>
<li class="row">
<div class="col-md-12">
<input type="submit" name="submit" required="required" value="Get Quotes!" id="emailbutton">
<?php
//CREATE VARIBLES
if(isset($_POST["submit"])) {
$submitdate = date("d/m/Y", strtotime($oldDate));
$frompostcode = $_POST["frompc"];
$fromtype = $_POST["fromtype"];
$movedate = $_POST["movedate"];
$flexible = $_POST["flexible"];
$topostcode = $_POST["topc"];
$totype = $_POST["totype"];
$packing = $_POST["packing"];
$packaging = $_POST["packaging"];
$furniture = $_POST["furniture"];
$notes = $_POST["notes"];
$name = $_POST["fullname"];
$contactnumber = $_POST["contactnumber"];
$email = $_POST["email"];
}
?>
<?php
//IMPORT TO DATABASE
if (!empty($_POST['email'])) {
//CLEANSE VARIBLES
date_default_timezone_set("Europe/London");
$submitdate = date('d/m/y h:i a', time());
//IMPORT
$query = " INSERT INTO `quotes`
('id', 'date', 'fullname', 'email', 'contactnumber', 'movedate', 'flexible', 'fromtype', 'frompostcode', 'totype', 'topostcode', 'notes')
VALUES
(NULL, '".$submitdate."', '".$name."', '".$email."', '".$contactnumber."', '".$movedate."', '".$flexible."', '".$fromtype."', '".$frompostcode."', '".$totype."', '".$topostcode."', '".$notes."'); ";
mysqli_query($query);
echo 'Thankyou <br />';
echo mysqli_error();
echo $query;
mysqli_close($email_sql);
}
?>
</div>
</li>
</ul>
</form>
</div>
</article>
</div>
</div>
</div>
</section>
<?php get_footer(); ?>
我一辈子都无法将表单保存到数据库中,我正在学习PHP/SQL,我看不出哪里出了问题,我知道我需要使用mysqli来代替mysql。就我个人而言,我会使用PDO。如果选择使用PDO,则需要确保在php.ini文件中启用该选项。大多数默认安装的PHP都已经安装了PDO,但它可能没有启用。查找行:
;extension=php_pdo_mysql.dll
并取消其注释。所以你现在应该有:
extension=php_pdo_mysql.dll
然后,您可以开始使用PDO解决此问题:
//Place this section at the beginning of your `.php` file.
//Connect to your DB
$config = array(
'host' => 'localhost'
'username' => 'root'
'password' => 'root'
'dbname' => 'myremovalsquote'
);
$db = new PDO('mysql:host='.$config['host'].';dbname='.$config['dbname'],$config['username'],$config['password']);
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
//Place the following after your line:
//if (!empty($_POST['email'])) {
date_default_timezone_set("Europe/London");
$submitdate = date('d/m/y h:i a', time());
$frompostcode = htmlentities($frompostcode);
$fromtype = htmlentities($fromtype);
$movedate = htmlentities($movedate);
$flexible = htmlentities($flexible);
$topostcode = htmlentities($topostcode);
$totype = htmlentities($totype);
$packing = htmlentities($packing);
$packaging = htmlentities($packaging);
$furniture = htmlentities($furniture);
$notes = htmlentities($notes);
$name = htmlentities($name);
$contactnumber = htmlentities($contactnumber);
$email = htmlentities($email);
//Create your query
//You don't need to have the 'id' in your query because it's an auto increment field in your DB. You prepare the statement to avoid SQL injection
$query = $db->prepare("INSERT INTO quotes
(date, fullname, email, contactnumber, movedate, flexible, fromtype, frompostcode, totype, topostcode, notes)
VALUES
(:submitdate, :name, :email, :contactnumber, :movedate, :flexible, :fromtype, :frompostcode, :totype, :topostcode, :notes)");
//Bind the variables to the query and run
$query_params = array(
':submitdate' => $submitdate,
':name' => $name,
':email' => $email,
':contactnumber' => $contactnumber,
':movedate' => $movedate,
':flexible' => $flexible,
':fromtype' => $fromtype,
':frompostcode' => $frompostcode,
':totype' => $totype,
':topostcode' => $topostcode,
':notes' => $notes
);
$query->execute($query_params);
//Close connection
$db = null;
经过大量的尝试和研究,我发现代码是正确的,由于某种原因,对数据库执行查询的最后部分使用失败了
mysqli_query($query);
或
但当我使用以下方法时,效果很好
$connect->query($query);
你有什么错误吗?但是如果你正在学习,网上有很多教程你可以遵循。我没有任何错误,这就是问题所在。当我检查数据库时,没有输入任何内容。@请使用try catch block获取错误。mysql函数不适用于mysqli。它们是不同的驱动程序。@Bradhuston:您正在使用mysqli_*函数选择数据库,然后尝试使用mysql_*函数运行查询。它们是独立的驱动程序,不能一起使用。在我攻克PHP的基础知识之前,我真的不想弄乱PDO。我可以理解这一点,虽然PDO可能需要一些额外的时间来学习,但对我来说,语法似乎更干净。我从来没有学过mysql或mysqli的方法。我直接从PDO开始。所以,很明显我有偏见。你对SQL注入和更新的代码持开放态度。你真正的逃跑是怎么回事?应首先使用准备好的报表。如果你因为某种原因不能使用它们,请使用转义。不过,您不应该只将用户输入传递给查询。
$connect->query($query);