gpt4 book ai didi

php - 存储过程和 PHP 表单

转载 作者:行者123 更新时间:2023-11-30 23:33:09 25 4
gpt4 key购买 nike

希望我在这里走在正确的轨道上。我有一个存储过程准备将客户详细信息添加到我的数据库中:

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 代码吗。

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

非常感谢。

最佳答案

我希望你知道如何将值从 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";
?>

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

关于php - 存储过程和 PHP 表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9549184/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com