- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
所以我正在尝试进行条件更新,但我似乎在将数据与语句绑定(bind)时遇到了问题。
功能:
function updateEditor($email, $first, $last, $id){
global $DBH;
$response = false;
$upemail = "";
$upfirst = "";
$uplast = "";
$stmt = "SELECT memEmail, memFirst, memLast FROM MEMBER WHERE memID = :id";
try{
$STH = $DBH->prepare($stmt);
$STH->bindParam(':id', $id);
$STH->execute();
$STH->setFetchMode(PDO::FETCH_ASSOC);
$row = $STH->fetch();
if($row['memEmail'] != $email){ $upemail = $email;}
if($row['memFirst'] != $first){ $upfirst = $first;}
if($row['memLast'] != $last){ $uplast = $last;}
}catch(PDOException $e) {
echo $e->getMessage() . "first";
}
$stmt .= "UPDATE MEMBER SET ";
if(!empty($upemail)){
$stmt .= "memEmail = :memEmail";
if(!empty($upfirst) || !empty($uplast)){
$stmt .= ", ";
}
}
if(!empty($upfirst)){
$stmt .= "memFirst = :memFirst";
if(!empty($uplast)){
$stmt .= ", ";
}
}
if(!empty($uplast)){
$stmt .= "memLast = :memLast";
}
if(empty($upemail) && empty($upfirst) && empty($uplast)){
return false;
}else{
$stmt .= " WHERE memID = :id";
}
try{
$STH = $DBH->prepare($stmt);
if(!empty($upemail)){$STH->bindParam(':memEmail', $upemail);}else{$STH->bindParam(':memEmail', $row['memEmail']);}
if(!empty($upfirst)){$STH->bindParam(':memFirst', $upfirst);}else{$STH->bindParam(':memFirst', $row['memFirst']);}
if(!empty($uplast)){$STH->bindParam(':memLast', $uplast);}else{$STH->bindParam(':memLast', $row['memLast']);}
$STH->bindParam(':id', $id);
$STH->execute();
$STH->setFetchMode(PDO::FETCH_ASSOC);
$response = true;
}catch(PDOException $e) {
echo $e->getMessage() . "second";
$response = $e->getMessage() . "second";
}
return $response;
}
到目前为止,我已经尝试使用 ?
和上面的代码将变量放入语句中。我不断收到的错误是:
SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens
最佳答案
这里:
$stmt .= "UPDATE MEMBER SET ";
您将 UPDATE 附加到先前的 $stmt
字符串。你最终会得到:
$stmt = "SELECT memEmail, memFirst, memLast FROM MEMBER WHERE memID = :idUPDATE MEMBER SET "; // and the rest
导致多一个标识符 (:idUPDATE
)。
删除 .
以在此字符串中开始新查询。
$stmt = "UPDATE MEMBER SET ";
注意:
你让这种方式太复杂了。跳过对空值的检查,仅在更新数据集时更新所有列,通过检查已更改的内容和未更改的内容不会获得任何 yield 。
关于php - 条件 PDO bindParam 更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35995727/
我正在为我正在创建的 CMS 试用 Mollie 支付处理器,但我遇到了问题。当我尝试将数据输入 MySQL 数据库时,它并没有这样做。我收到此错误: Fatal error: Call to a m
当在表单中选择性别时,我想在我的查询中添加一个 WHERE。 我正在使用 bindParam 来使用一个下拉菜单来获取年份,但我想要第二个 bindParam,它向查询插入一个“AND Gender
我的代码基于@zzzeek 对this question 的回答.我稍微扩展了它,以便它考虑到 Postgresql 的 NULL 和 ARRAY。 class values(FromClause):
我找到了this脚本,因为可能容易受到 SQL 注入(inject)的攻击,我尝试绑定(bind)参数 starting_limit 变量,但不起作用。这是我的代码: include("include
我正在使用我在网上找到的登录脚本。 ( http://alias.io/2010/01/store-passwords-safely-with-php-and-mysql/ ) 当我试图让它工作时,我
我有以下 PHP 代码,用于将 session 数据插入到名为 ds_session 的表中: // session does not exists create insert stateme
我正在为一项任务创建一个论坛网站,代码工作正常,直到我改变了两件事:我将我的数据库约束更改为 CASCADE(网站的其余部分需要像这样)并且我更改了查询功能为安全而准备、绑定(bind)和执行。 此代
我是 PDO 的新手,但我对使用 bindParam 函数有点困惑。是否可以避免使用bindParam?如果不是 - 为什么? 我正在使用这样的存储过程 $e = 'example@g.com'; $
我很难理解如何正确使用bindParam(); 我一直在关注以下 URL 的详细信息:http://php.net/manual/en/pdo.prepared-statements.php其中的示例
PDO 不能使用单个 bindParam() 将值绑定(bind)到查询中多次出现的参数吗? 我很惊讶,我认为这是可能的,但我没有在 php 文档上找到任何关于此的信息,在网络上也没有。欢迎任何澄清/
在PDO中使用bindParam或bindValue时,PDOStatement对象中的“绑定(bind)”到底存储在哪里?是否可以显示此设置的属性/引用/等? 我试图了解这些和其他 PDOState
我觉得我错过了一些东西。 我尝试将 : 分隔符与 bindParam 一起使用,但未成功。 PHP: $te = 'test'; $tesql = "select charname from char
在 self 介绍 pgSQL 准备语句时,我已经成功地返回了几个查询的结果。但是,我有几个问题。 给定以下查询: $w_ft = "36"; $sth = $dbh->prepare("SELECT
我的代码有问题,我无法显示我的数据库中的数据我正在使用 bindParam() 和 bindValue() 它仍然无法正常工作。 这是我的代码: $id = $_GET['id']; try{
我有一个 pdo 语句来选择行并将它们返回到一个数组中。我使用分页每页显示 12 个结果。如果我直接输入 LIMIT 12, 12 而不是 LIMIT ?, ? 语句会正常工作。 我对这两个变量的 b
您好,这很可能是一个非常简单的答案,但我想知道如何仅在变量不为空时运行 BindParam 下面是一些示例代码: // Adds variable name from input POST $post
我是 PDO 的初学者,我正在制作一个 php 函数来返回航类的搜索结果,这是我的代码: $db = DB::getConnection(); $stmt = $db->prepare("SELECT
这已经被窃听了很长时间,我仍然无法弄清楚我做错了什么。在代码中,我想用逗号分隔的字符串选择一些用户。该字符串将始终合法有效。 在第一个示例中,我想使用的示例使用 bindParam 将 $postId
我刚开始使用 pdo 语句,当我尝试在 phpMyadmin sql 控制台中设置这样的查询时,它工作正常: SELECT name, number FROM client WHERE number
我想尝试使用 bindParam 从数据库中显示我的数据,但出现了一些错误。 Recoverable fatal error: Object of class PDOStatement could n
我是一名优秀的程序员,十分优秀!