如何使用 PDO 和 PHP 表单变量执行插入存储过程?
大家晚上好,
我对 PDO 准备好的陈述非常陌生,需要一些指导/温和的推动。我创建了以下 MySQL 存储过程:
-- --------------------------------------------------------------------------------
-- Routine DDL
-- --------------------------------------------------------------------------------
DELIMITER $$
CREATE DEFINER=`root`@`%` PROCEDURE `RegInsert`(
IN p_regid int,
IN p_username VARCHAR(45),
IN p_password VARCHAR(45),
IN p_confpassword VARCHAR(45),
IN p_status INT(1),
IN p_salutation VARCHAR(45),
IN p_firstname VARCHAR(45),
IN p_lastname VARCHAR(45),
IN p_jobtitle VARCHAR(45),
IN p_telephone VARCHAR(45),
IN p_companyname VARCHAR(45),
IN p_industry VARCHAR(45),
IN p_address VARCHAR(45),
IN p_city VARCHAR(45),
IN p_state VARCHAR(45),
IN p_country VARCHAR(45),
IN p_postalcode VARCHAR(45),
IN p_regtype VARCHAR(45),
IN p_interest VARCHAR(45),
IN p_hdsprovider VARCHAR(45)
)
BEGIN
Insert into regdata(
RegID,
UserName,
Password,
Confpassword,
Status,
Salutation,
FirstName,
LastName,
JobTitle,
Telephone,
Companyname,
Industry,
Address,
City,
Country,
State,
PostalCode,
RegType,
Interests,
HDSprovider
)
values(
p_regid,
p_username,
p_password,
p_confpassword,
p_status,
p_salutation,
p_firstname,
p_lastname,
p_jobtitle,
p_telephone,
p_companyname,
p_industry,
p_address,
p_city,
p_country,
p_state,
p_postalcode,
p_regtype,
p_interest,
p_hdsprovider
);
END
在阅读有关 PDO 的文档时,我了解到以下语句用于打开连接,并且 try catch 块设置参数。这就是我感到困惑的地方。到目前为止,这是我的代码块:
<?php
require once ("/home/somedir/pdo_connect.php")
try{
$dbh=pdo_connect.php();
$stmt = $dbh->prepare("CALL RegInsert(?)");
$stmt->bindParam
}
catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
?>
问题:
1. 将过程输入参数与 PHP 表单变量相关联的正确语法是什么?是不是类似:
$_Post['salutation'] = p_salutation
Reginsert.php 包含上述 PDO 语句。如何从表单上的提交按钮调用它?
您是否建议为确认电子邮件创建单独的文件和容器函数?如果是这样,在 reginsert.php 运行没有错误后如何触发该文件运行?
非常感谢您帮助这个睡眠不足的新手。非常感谢!
编辑:
这是 reginsert.php 的修订代码。当我提供有效的表单数据并按提交时,记录不会插入到数据库中。你能引导我走向正确的方向吗?
<?php
require once ("/home/mydir/pdo_connect.php")
try{
$dbh=pdo_connect.php();
$stmt = $dbh->prepare('CALL RegInsert(?)');
$stmt->bindParam(':p_username',$email_address,PDO::PARAM_STR,45);
$stmt->bindParam(':p_password',$create_password,PDO::PARAM_STR,45);
$stmt->bindParam(':p_confpassword',$confirm_password,PDO::PARAM_STR,45);
$stmt->bindParam(':p_salutation',$salutation2,PDO::PARAM_STR,45);
$stmt->bindParam(':p_firstname',$first_name,PDO::PARAM_STR,45);
$stmt->bindParam(':p_lastname',$last_name,PDO::PARAM_STR,45);
$stmt->bindParam(':p_jobtitle',$job_title,PDO::PARAM_STR,45);
$stmt->bindParam(':p_telephone',$telephone,PDO::PARAM_STR,45);
$stmt->bindParam(':p_companyname',$company_name,PDO::PARAM_STR,45);
$stmt->bindParam(':p_industry',$industry,PDO::PARAM_STR,45);
$stmt->bindParam(':p_address',$address,PDO::PARAM_STR,45);
$stmt->bindParam(':p_city',$city,PDO::PARAM_STR,45);
$stmt->bindParam(':p_state',$state,PDO::PARAM_STR,45);
$stmt->bindParam(':p_country',$country,PDO::PARAM_STR,45);
$stmt->bindParam(':p_postalcode',$postal_code,PDO::PARAM_STR,45);
$stmt->bindParam(':p_regtype',$partner_customer_other,PDO::PARAM_STR,45);
$stmt->bindParam(':p_interest',$interests,PDO::PARAM_STR,45);
$stmt->bindParam(':p_hdsprovider',$provider_partner,PDO::PARAM_STR,45);
$stmt->bindParam(':p_passwordremindquestion',$password_reminder_question,PDO::PARAM_STR,45);
$stmt->bindParam(':p_passwordremindanswer',$password_reminder_answer,PDO::PARAM_STR,45);
$stmt->execute();
}
catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
?>
Good Evening All,
I'm very new to PDO prepared statements and need some guidance/gentle nudges. I've created the following MySQL stored procedure:
-- --------------------------------------------------------------------------------
-- Routine DDL
-- --------------------------------------------------------------------------------
DELIMITER $
CREATE DEFINER=`root`@`%` PROCEDURE `RegInsert`(
IN p_regid int,
IN p_username VARCHAR(45),
IN p_password VARCHAR(45),
IN p_confpassword VARCHAR(45),
IN p_status INT(1),
IN p_salutation VARCHAR(45),
IN p_firstname VARCHAR(45),
IN p_lastname VARCHAR(45),
IN p_jobtitle VARCHAR(45),
IN p_telephone VARCHAR(45),
IN p_companyname VARCHAR(45),
IN p_industry VARCHAR(45),
IN p_address VARCHAR(45),
IN p_city VARCHAR(45),
IN p_state VARCHAR(45),
IN p_country VARCHAR(45),
IN p_postalcode VARCHAR(45),
IN p_regtype VARCHAR(45),
IN p_interest VARCHAR(45),
IN p_hdsprovider VARCHAR(45)
)
BEGIN
Insert into regdata(
RegID,
UserName,
Password,
Confpassword,
Status,
Salutation,
FirstName,
LastName,
JobTitle,
Telephone,
Companyname,
Industry,
Address,
City,
Country,
State,
PostalCode,
RegType,
Interests,
HDSprovider
)
values(
p_regid,
p_username,
p_password,
p_confpassword,
p_status,
p_salutation,
p_firstname,
p_lastname,
p_jobtitle,
p_telephone,
p_companyname,
p_industry,
p_address,
p_city,
p_country,
p_state,
p_postalcode,
p_regtype,
p_interest,
p_hdsprovider
);
END
In reading the documentation on PDO, I understand that the following statement is used to open the connection and the try catch block sets the parameters. That's where I'm getting confused. Here's my code block thus far:
<?php
require once ("/home/somedir/pdo_connect.php")
try{
$dbh=pdo_connect.php();
$stmt = $dbh->prepare("CALL RegInsert(?)");
$stmt->bindParam
}
catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
?>
Questions:
1. What is the correct syntax for associating my procedure input parameters with my PHP form variables? Is it something like:
$_Post['salutation'] = p_salutation
Reginsert.php contains the above PDO statements. How do I call it from my submit button on the form?
Do you suggest creating a separate file and container function for the confirmation email? If so, how do I trigger that file to run after reginsert.php runs without errors?
Thanks so much for helping this sleep-deprived newbie. It's much appreciated!!
EDIT:
Here's the revised code for reginsert.php. When I provide valid form data and press submit, a record is not inserted into the database. Can you steer me in the right direction?
<?php
require once ("/home/mydir/pdo_connect.php")
try{
$dbh=pdo_connect.php();
$stmt = $dbh->prepare('CALL RegInsert(?)');
$stmt->bindParam(':p_username',$email_address,PDO::PARAM_STR,45);
$stmt->bindParam(':p_password',$create_password,PDO::PARAM_STR,45);
$stmt->bindParam(':p_confpassword',$confirm_password,PDO::PARAM_STR,45);
$stmt->bindParam(':p_salutation',$salutation2,PDO::PARAM_STR,45);
$stmt->bindParam(':p_firstname',$first_name,PDO::PARAM_STR,45);
$stmt->bindParam(':p_lastname',$last_name,PDO::PARAM_STR,45);
$stmt->bindParam(':p_jobtitle',$job_title,PDO::PARAM_STR,45);
$stmt->bindParam(':p_telephone',$telephone,PDO::PARAM_STR,45);
$stmt->bindParam(':p_companyname',$company_name,PDO::PARAM_STR,45);
$stmt->bindParam(':p_industry',$industry,PDO::PARAM_STR,45);
$stmt->bindParam(':p_address',$address,PDO::PARAM_STR,45);
$stmt->bindParam(':p_city',$city,PDO::PARAM_STR,45);
$stmt->bindParam(':p_state',$state,PDO::PARAM_STR,45);
$stmt->bindParam(':p_country',$country,PDO::PARAM_STR,45);
$stmt->bindParam(':p_postalcode',$postal_code,PDO::PARAM_STR,45);
$stmt->bindParam(':p_regtype',$partner_customer_other,PDO::PARAM_STR,45);
$stmt->bindParam(':p_interest',$interests,PDO::PARAM_STR,45);
$stmt->bindParam(':p_hdsprovider',$provider_partner,PDO::PARAM_STR,45);
$stmt->bindParam(':p_passwordremindquestion',$password_reminder_question,PDO::PARAM_STR,45);
$stmt->bindParam(':p_passwordremindanswer',$password_reminder_answer,PDO::PARAM_STR,45);
$stmt->execute();
}
catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我来这里是为了寻求建议(在谷歌上找到的。)然而,在我测试我的建议之前,我可以看到您不仅在查询(例程调用)中使用了一个占位符(?标记),而且您还尝试向其发送命名参数。
你的代码可能看起来更像这样,
我的例程只有一个参数,但它使用命名变体工作(对我来说可能有点过头了。)
I came here looking for advice (found on google.) Before I even tested mine, however, I could see that you are not only using one place-holder (the ? mark) for your query (the routine call) but you are also trying to send named parameters to that.
Your code should probably look more like this
I only had one parameter for my routine, but it worked using the named variation (possibly a little over-kill for me.)