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

时间:2015-02-28 10:55:32

标签: php mysql twitter-bootstrap 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");           			   
}

?>

希望修复一件简单的事,谢谢

2 个答案:

答案 0 :(得分:4)

我认为你错过了引号并强调了变量定义

// [...]
$customerref = $_POST['customerref'];
//[...]

答案 1 :(得分:0)

当你省略引号时,PHP认为你在POST超全局中的键是一个常量定义,并且在你可能的应用程序的所有范围内都找到了阿姨:

$customerref = $POST[customerref];

当PHP无法找到此常量时,会发送错误。

如果您的代码和数据库中的一切顺利,那么错误就在POST [key]赋值中。

然后尝试在超全局POST周围加上引号:

$customerref = $POST['customerref'];