pdo 表单提交mysql,PDO中的表单以更新数据

I've been looking around and even here on the site, but I can not find the correct syntax in PDO to update data, such as the data of a user profile.

You could give me a practical example with html form?

I know that maybe I ask so much, but I can not make it work.

I enclose what until now have been able to do, but not work.

if(isset($_POST['submit'])) {

$email = $_POST['email'];

$location = $_POST['location'];

$id = $_SESSION['memberID'];

$stmt = $db->prepare("UPDATE `members` SET `email` = :email, `location` = :location WHERE `memberID` = :id");

$stmt->bindParam(":email", $email, PDO::PARAM_STR);

$stmt->bindParam(":location", $location, PDO::PARAM_STR);

$stmt->bindParam(":id", $_SESSION['memberID'], PDO::PARAM_STR);

$stmt->execute(array(':email' => $_POST['email'], ':location' => $_POST['location'], ':id' => $id));

}

And,

Email

Location

I was wondering if it was safe and correct to query the same page or should I create a class? Can you help with a practical example because I have tried everything.

解决方案

First I'll explain some of the changes I made to your code.

1) Back-ticks are not required unless you are using a reserved word, so I removed them

2) You are already defining $id as $id = $_SESSION['memberID']; so I changed $stmt->bindParam(":id", $_SESSION['memberID'], PDO::PARAM_STR);

3) If you are binding your parameters, you don't need to execute with an array, so I changed $stmt->execute(array(':email' => $_POST['email'], ':location' => $_POST['location'], ':id' => $id)); to $stmt->execute();

4) The action in your form must be echoed.

This is the resulting process

if(isset($_POST['submit'])) {

$email = $_POST['email'];

$location = $_POST['location'];

$id = $_SESSION['memberID'];

$sql = "UPDATE members SET email=:email, location=:location WHERE memberID=:id";

$stmt = $db->prepare($sql);

$stmt->bindValue(":email", $email, PDO::PARAM_STR);

$stmt->bindValue(":location", $location, PDO::PARAM_STR);

$stmt->bindValue(":id", $id, PDO::PARAM_STR);

$stmt->execute();

}

?>

This is the resulting form (easier to read with indentations)

Email

Location

Happy Coding !

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值