如何使用 PDO POST Bootstrap 模态形式值到 MYSQL 数据库
How to POST Bootstrap modal form values to MYSQL database with PDO
需要您的帮助来解决本应非常简单的问题。
我在 Bootstrap 中有一个收集客户信息的模态表单,我希望将此信息保存在 MYSQL 数据库中。
我没有收到任何错误,但值没有插入到数据库中 table。 PDO 连接似乎是因为 table 中的自动增量值在提交时被添加(但不是表单中的值)。
模态表单代码如下:
<!-- Modal Insert Form -->
<div class="modal fade" id="insert" tabindex="-1" role="dialog" aria-labelledby="insert" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">
<span class="glyphicon glyphicon-remove" aria-hidden="true"></span>
</button>
<h4 class="modal-title custom_align" id="Heading">Add New Customer Detail</h4>
</div>
<form class="form-horizontal" method="post" action="customers_add.php">
<div class="modal-body">
<div class="row">
<div class="col-xs-6">
<input name="firstname" id="firstname" type="text" class="form-control" placeholder="First Name">
</div>
<div class="col-xs-6">
<input type="text" class="form-control" placeholder="Last Name">
</div>
</div>
<br>
<div class="row">
<div class="col-xs-12">
<input type="text" class="form-control" name="company" placeholder="Company">
</div>
</div>
<br>
<div class="row">
<div class="col-xs-12">
<input type="text" class="form-control" name="address1" placeholder="Address Line 1">
</div>
</div>
<br>
<div class="row">
<div class="col-xs-12">
<input type="text" class="form-control" name="address2" placeholder="Address Line 2">
</div>
</div>
<br>
<div class="row">
<div class="col-xs-4">
<input type="text" class="form-control" name="town" placeholder="Town">
</div>
<div class="col-xs-4">
<input type="text" class="form-control" name="county" placeholder="County">
</div>
<div class="col-xs-4">
<input type="text" class="form-control" name ="postcode" placeholder="Post Code">
</div>
</div>
<br>
<div class="row">
<div class="col-xs-6">
<input type="text" class="form-control" name="telephone1" placeholder="Telephone 1">
</div>
<div class="col-xs-6">
<input type="text" class="form-control" name="telephone2" placeholder="Telephone 2">
</div>
</div>
<br>
<div class="row">
<div class="col-xs-6">
<input type="text" class="form-control" name="website" placeholder="Website">
</div>
<div class="col-xs-6">
<input type="text" class="form-control" name="referredby" placeholder="Referred By">
</div>
</div>
<br>
<div class="form-group">
<div class="col-xs-12">
<textarea rows="2" class="form-control" name="dietaryreq" placeholder="Dietary Requirements"></textarea>
</div>
</div>
<div class="form-group">
<div class="col-xs-12">
<textarea rows="2" class="form-control" name="notes" placeholder="Notes"></textarea>
</div>
</div>
</div>
<div class="modal-footer ">
<button type="submit" name="submit" id="submit" class="btn btn-success btn-lg" style="width: 100%;" value="Add Customer">
<span class="glyphicon glyphicon-ok-sign"></span>
Add New Customer
</button>
</div>
</form>
</div> <!-- /.modal-content -->
</div> <!-- /.modal-dialog -->
</div>
... 这是应该将表单值处理到 mysql 数据库中的 php:
<?php
include('config.php');
// check if variable is set and Add Customer Button pressed.
if(isset($_POST["submit"])=="Add Customer")
{
// Define Variables
$userref = $_SESSION['user']['User_Ref'];
$customerref = $POST[customerref];
$firstname = $POST[firstname];
$lastname = $POST[lastname];
$company = $POST[company];
$addressline1 = $POST[addressline1];
$addressline2 = $POST[addressline2];
$town = $POST[town];
$county = $POST[county];
$postcode = $POST[postcode];
$telephone1 = $POST[telephone1];
$telephone2 = $POST[telephone2];
$emailaddress = $POST[emailaddress];
$website = $POST[website];
$referredby = $POST[referredby];
$dietaryreq = $POST[dietaryreq];
$notes = $POST[notes];
// Prepare SQL Query
$STM = $db->prepare("INSERT INTO Customers(User_Ref, Customer_Ref, First_Name, Last_Name, Company, Address_Line_1, Address_Line_2, Town, County, Post_Code, Telephone_1, Telephone_2, Email_Address, Website, Referred_By, Dietary_Req, Notes) VALUES (:userref,:customerref,:firstname,:lastname,:company,:addressline1,:addressline2,:town,:county,:postcode,:telephone1,:telephone2,:emailaddress,:website,:referredby,:dietaryreq,:notes)");
// Bind parameters, Named parameters always start with colon(:)
$STM->bindParam(':userref',$userref);
$STM->bindParam(':customerref',$customerref);
$STM->bindParam(':firstname',$firstname);
$STM->bindParam(':lastname',$lastname);
$STM->bindParam(':company',$company);
$STM->bindParam(':addressline1',$addressline1);
$STM->bindParam(':addressline2',$addressline2);
$STM->bindParam(':town',$town);
$STM->bindParam(':county',$county);
$STM->bindParam(':postcode',$postcode);
$STM->bindParam(':telephone1',$telephone1);
$STM->bindParam(':telephone2',$telephone2);
$STM->bindParam(':emailaddress',$emailaddress);
$STM->bindParam(':website',$website);
$STM->bindParam(':referredby',$referredby);
$STM->bindParam(':dietaryreq',$dietaryreq);
$STM->bindParam(':notes',$notes);
// Execute prepared statement
$STM->execute();
// Redirecting it to other page where we will show success message.
header("location:customers.php");
}
?>
希望修复起来很简单,谢谢
我想你错过了变量定义中的引号和下划线
// [...]
$customerref = $_POST['customerref'];
//[...]
当您省略引号时,PHP 认为您在 POST 超全局中的键是一个定义的常量,并且在您可能的应用程序的所有范围内都可以找到:
$customerref = $POST[customerref];
当PHP找不到这个常量时,它会发送一个错误。
如果您的代码和数据库中一切顺利,那么错误就在 POST[key] 赋值中。
然后尝试在超全局POST两边加上引号:
$customerref = $POST['customerref'];
需要您的帮助来解决本应非常简单的问题。
我在 Bootstrap 中有一个收集客户信息的模态表单,我希望将此信息保存在 MYSQL 数据库中。
我没有收到任何错误,但值没有插入到数据库中 table。 PDO 连接似乎是因为 table 中的自动增量值在提交时被添加(但不是表单中的值)。
模态表单代码如下:
<!-- Modal Insert Form -->
<div class="modal fade" id="insert" tabindex="-1" role="dialog" aria-labelledby="insert" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">
<span class="glyphicon glyphicon-remove" aria-hidden="true"></span>
</button>
<h4 class="modal-title custom_align" id="Heading">Add New Customer Detail</h4>
</div>
<form class="form-horizontal" method="post" action="customers_add.php">
<div class="modal-body">
<div class="row">
<div class="col-xs-6">
<input name="firstname" id="firstname" type="text" class="form-control" placeholder="First Name">
</div>
<div class="col-xs-6">
<input type="text" class="form-control" placeholder="Last Name">
</div>
</div>
<br>
<div class="row">
<div class="col-xs-12">
<input type="text" class="form-control" name="company" placeholder="Company">
</div>
</div>
<br>
<div class="row">
<div class="col-xs-12">
<input type="text" class="form-control" name="address1" placeholder="Address Line 1">
</div>
</div>
<br>
<div class="row">
<div class="col-xs-12">
<input type="text" class="form-control" name="address2" placeholder="Address Line 2">
</div>
</div>
<br>
<div class="row">
<div class="col-xs-4">
<input type="text" class="form-control" name="town" placeholder="Town">
</div>
<div class="col-xs-4">
<input type="text" class="form-control" name="county" placeholder="County">
</div>
<div class="col-xs-4">
<input type="text" class="form-control" name ="postcode" placeholder="Post Code">
</div>
</div>
<br>
<div class="row">
<div class="col-xs-6">
<input type="text" class="form-control" name="telephone1" placeholder="Telephone 1">
</div>
<div class="col-xs-6">
<input type="text" class="form-control" name="telephone2" placeholder="Telephone 2">
</div>
</div>
<br>
<div class="row">
<div class="col-xs-6">
<input type="text" class="form-control" name="website" placeholder="Website">
</div>
<div class="col-xs-6">
<input type="text" class="form-control" name="referredby" placeholder="Referred By">
</div>
</div>
<br>
<div class="form-group">
<div class="col-xs-12">
<textarea rows="2" class="form-control" name="dietaryreq" placeholder="Dietary Requirements"></textarea>
</div>
</div>
<div class="form-group">
<div class="col-xs-12">
<textarea rows="2" class="form-control" name="notes" placeholder="Notes"></textarea>
</div>
</div>
</div>
<div class="modal-footer ">
<button type="submit" name="submit" id="submit" class="btn btn-success btn-lg" style="width: 100%;" value="Add Customer">
<span class="glyphicon glyphicon-ok-sign"></span>
Add New Customer
</button>
</div>
</form>
</div> <!-- /.modal-content -->
</div> <!-- /.modal-dialog -->
</div>
... 这是应该将表单值处理到 mysql 数据库中的 php:
<?php
include('config.php');
// check if variable is set and Add Customer Button pressed.
if(isset($_POST["submit"])=="Add Customer")
{
// Define Variables
$userref = $_SESSION['user']['User_Ref'];
$customerref = $POST[customerref];
$firstname = $POST[firstname];
$lastname = $POST[lastname];
$company = $POST[company];
$addressline1 = $POST[addressline1];
$addressline2 = $POST[addressline2];
$town = $POST[town];
$county = $POST[county];
$postcode = $POST[postcode];
$telephone1 = $POST[telephone1];
$telephone2 = $POST[telephone2];
$emailaddress = $POST[emailaddress];
$website = $POST[website];
$referredby = $POST[referredby];
$dietaryreq = $POST[dietaryreq];
$notes = $POST[notes];
// Prepare SQL Query
$STM = $db->prepare("INSERT INTO Customers(User_Ref, Customer_Ref, First_Name, Last_Name, Company, Address_Line_1, Address_Line_2, Town, County, Post_Code, Telephone_1, Telephone_2, Email_Address, Website, Referred_By, Dietary_Req, Notes) VALUES (:userref,:customerref,:firstname,:lastname,:company,:addressline1,:addressline2,:town,:county,:postcode,:telephone1,:telephone2,:emailaddress,:website,:referredby,:dietaryreq,:notes)");
// Bind parameters, Named parameters always start with colon(:)
$STM->bindParam(':userref',$userref);
$STM->bindParam(':customerref',$customerref);
$STM->bindParam(':firstname',$firstname);
$STM->bindParam(':lastname',$lastname);
$STM->bindParam(':company',$company);
$STM->bindParam(':addressline1',$addressline1);
$STM->bindParam(':addressline2',$addressline2);
$STM->bindParam(':town',$town);
$STM->bindParam(':county',$county);
$STM->bindParam(':postcode',$postcode);
$STM->bindParam(':telephone1',$telephone1);
$STM->bindParam(':telephone2',$telephone2);
$STM->bindParam(':emailaddress',$emailaddress);
$STM->bindParam(':website',$website);
$STM->bindParam(':referredby',$referredby);
$STM->bindParam(':dietaryreq',$dietaryreq);
$STM->bindParam(':notes',$notes);
// Execute prepared statement
$STM->execute();
// Redirecting it to other page where we will show success message.
header("location:customers.php");
}
?>
希望修复起来很简单,谢谢
我想你错过了变量定义中的引号和下划线
// [...]
$customerref = $_POST['customerref'];
//[...]
当您省略引号时,PHP 认为您在 POST 超全局中的键是一个定义的常量,并且在您可能的应用程序的所有范围内都可以找到:
$customerref = $POST[customerref];
当PHP找不到这个常量时,它会发送一个错误。
如果您的代码和数据库中一切顺利,那么错误就在 POST[key] 赋值中。
然后尝试在超全局POST两边加上引号:
$customerref = $POST['customerref'];