2012-03-03 53 views
2

希望我在这里的正确轨道上。我已经准备好客户的详细信息添加到我的数据库存储过程:存储过程和PHP表格

DROP PROCEDURE `sp_add_customer`// 
CREATE DEFINER=`test`@`%` PROCEDURE `sp_add_customer`(IN in_name VARCHAR(100), in_address_line_1 VARCHAR(100), in_address_line_2 VARCHAR(100), in_address_line_3 VARCHAR(100), in_city VARCHAR(50), in_county VARCHAR(50), in_phone VARCHAR(30), in_mobile VARCHAR(30), in_email VARCHAR(100)) 
BEGIN 

    INSERT INTO customer(name, address_line_1, address_line_2, address_line_3, city, county, phone, mobile, email) 
    VALUES(in_name, in_address_line_1, in_address_line_2, in_address_line_3, in_city, in_county, in_phone, in_mobile, in_email); 

END 

我现在想用这个存储过程使用HTML形式(类似于下面的一个)给客户添加到我的客户表。

<form id="htmlForm" action="add-customer.php" method="post" class="form-horizontal"> 
    <input type="text" class="input-large" placeholder="Customer Name"><br/> 
    <input type="text" class="input-large" placeholder="Phone"><br/> 
    <input type="text" class="input-large" placeholder="Mobile"><br/> 
    <input type="text" class="input-large" placeholder="Email"><br/> 
    <input type="text" class="input-large" placeholder="Address Line 1"><br/> 
    <input type="text" class="input-large" placeholder="Address Line 2"><br/> 
    <input type="text" class="input-large" placeholder="Address Line 3"><br/> 
    <input type="text" class="input-large" placeholder="City"><br/> 
    <input type="text" class="input-large" placeholder="County"><br/> 
    <button type="submit" class="btn">Add Stock</button> 
</form> 

可能有人请向我解释PHP代码,我需要使用存储过程从客户的形式向客户表中添加细节。

的附加customer.php文件包含:

<?php 

    //MySQL Database Connect 
    require once ("includes/config.php") 

    $name = $_POST['name']; 
    $phone = $_POST['phone']; 
    $mobile = $_POST['mobile']; 
    $email = $_POST['email']; 
    $address1 = $_POST['address1']; 
    $address2 = $_POST['address2']; 
    $address3 = $_POST['address3']; 
    $city = $_POST['city']; 
    $county = $_POST['county']; 

    try{ 
     $dbh=config.php(); 
     $stmt = $dbh->prepare('CALL sp_add_customer(:in_name, :in_address_line_1, :in_address_line_2, :in_address_line_3, :in_city, :in_county, :in_phone, :in_mobile, :in_email)'); 
     $stmt->bindParam(':in_name',$name,PDO::PARAM_STR,45); 
     $stmt->bindParam(':in_address_line_1',$address1,PDO::PARAM_STR,45); 
     $stmt->bindParam(':in_address_line_2',$address2,PDO::PARAM_STR,45); 
     $stmt->bindParam(':in_address_line_3',$address3,PDO::PARAM_STR,45); 
     $stmt->bindParam(':in_city',$city,PDO::PARAM_STR,45); 
     $stmt->bindParam(':in_county',$county,PDO::PARAM_STR,45); 
     $stmt->bindParam(':in_phone',$phone,PDO::PARAM_STR,45); 
     $stmt->bindParam(':in_mobile',$mobile,PDO::PARAM_STR,45); 
     $stmt->bindParam(':in_email',$email,PDO::PARAM_STR,45); 
     $stmt->execute(); 
    } 
    catch (PDOException $e) { 
    print "Error!: " . $e->getMessage() . "<br/>"; 
    die(); 
} 
?> 

目前我收到以下错误: 解析错误:语法错误,意想不到的T_VARIABLE

大加赞赏。

+1

'$胸径= config.php文件()'应该是'要求( 'config.php文件');'。我的猜测是你来自另一种语言,不知道你必须要求文件,你不能称它们为函数。 – Xeoncross 2012-03-03 23:26:03

+0

非常完美的谢谢你,那就完成了! – 2012-03-03 23:48:27

回答

0

我希望你知道如何将值从html页面发送到php代码。

php manual

<?php 
$stmt = $dbh->prepare("CALL sp_add_customer(?)"); 
$stmt->bindParam(1, $return_value, PDO::PARAM_STR, 4000); 

// call the stored procedure 
$stmt->execute(); 

print "procedure returned $return_value\n"; 
?> 

OK试试这个 http://www.joeyrivera.com/2009/using-mysql-stored-procedures-with-php-mysqlmysqlipdo/

+0

嗨@elrado,谢谢你的回应。我收到以下错误:致命错误:调用一个非对象的成员函数prepare()。有任何想法吗?谢谢 – 2012-03-03 21:16:23