gpt4 book ai didi

php 提交与 PDO SQL 问题

转载 作者:行者123 更新时间:2023-11-29 00:53:23 24 4
gpt4 key购买 nike

我想从普通的 mysql 查询更改为 PDO,但从那时起我什至无法回溯故障。

有没有人可以帮助我?任何提示将不胜感激!

Q1:怎么了? Q2:故障一般如何追踪?

<?php
// Check if add button active, start this
if (isset($_POST['create'])) {
$new=htmlspecialchars($_POST['newgroup']);
$sql = "INSERT INTO contactgroups (status, gr_name) VALUES(1, : new )";
$sql->bindvalue( ': new', $new);
$sql->execute();
$arr = $sql->errorInfo();
echo $arr;
echo "<meta http-equiv=\"refresh\" content=\"0;URL=groups.php\">";
}
?>

<form id="group-in" method="post" action="groups.php">
Add new Distribution group: <input type="text" name="newgroup" placeholder="name..."> <input type="submit" name="create" value="Create new">
Rename groupname: <input type="text" value="<?php echo $result['gr_name']; ?>"> <input type="submit" name="rename" value="Rename">
</form>

$_POST 来了,SQL 正在运行,但是在 bindPARAM/bindVALUE 之后什么都没有...

最佳答案

你永远不会在上面准备你的 sql 语句。

为了运行,您首先需要初始化数据库并创建一个新的 PDO 实例。假设您之前完成过此操作:

<?php
// Check if add button active, start this
if (isset($_POST['create'])) {
$new=htmlspecialchars($_POST['newgroup']);
$sql = "INSERT INTO contactgroups (status, gr_name) VALUES(1, : new )";
$stmt = $db->prepare($sql);
$stmt->bindvalue( ': new', $new);
$stmt->execute();
$arr = $stmt->errorInfo();
echo $arr;
echo "<meta http-equiv=\"refresh\" content=\"0;URL=groups.php\">";
}
?>

您可以使用 try/catch 语句包装函数/代码位,并使用 PDO 启用异常处理,以便更好地洞察 future 的错误。

关于php 提交与 PDO SQL 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7277328/

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