如何使用PDO将引导模式表单值POST到MYSQL数据库


How to POST Bootstrap modal form values to MYSQL database with PDO

需要您在哪些方面提供帮助。

我在Bootstrap中有一个收集客户信息的模态表单,我希望将这些信息保存在MYSQL数据库中。

我没有收到任何错误,但这些值没有插入数据库表中。PDO连接似乎是因为表中的自动增量值是在提交时添加的(但不是表单中的值)。

这是模态表单代码:

<!-- 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>

下面是php,它应该将表单值处理到mysql数据库中:

<?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'];