2012-03-03 57 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