douyuqing_12345 2015-01-27 09:12
浏览 48
已采纳

PDO更新查询绝对没有任何意义

So yesterday I ran into this error which I can't seem to get a hang of. The piece of code below is supposed to make two posts in a recursive MariaDB table switch places.

$cid is the ID of the post that is supposed to switch place, up or down. $pid is the parent ID. $ord is which place the post has under the parent. $dir is in which direction the post is supposed to be moved, up or down.

Then the post in the position that the first post is supposed to move to is taking the first posts initial place.

<?php

    $cid = isset($_GET['cid']) ? $_GET['cid'] : "";
    $pid = isset($_GET['pid']) ? $_GET['pid'] : "";
    $ord = isset($_GET['ord']) ? $_GET['ord'] : "";
    $dir = isset($_GET['dir']) ? $_GET['dir'] : "";

    if($cid == "" || $pid == "" || $ord == "" || $dir == "") die('nu gick det åt pipsvängen...');

    if($dir == "up") {
        $opos = $ord--;
    } elseif($dir == "down") {
        $opos = $ord++;
    } else {
        die('ingen riktning');
    };

    $ostmt = $dbh -> prepare("UPDATE csa_categories SET ordning = :onp WHERE foralder = :pid AND ordning = :oop");
    $ostmt -> bindValue(":onp", $ord);
    $ostmt -> bindValue(":pid", $pid);
    $ostmt -> bindValue(":oop", $opos);

    $ostmt -> execute();

    $cstmt = $dbh -> prepare("UPDATE csa_categories SET ordning = :cnp WHERE id = :cid");
    $cstmt -> bindValue(":cnp", $opos);
    $cstmt -> bindValue(":cid", $cid);

    $cstmt -> execute();

?>

This does absolutely nothing. I doesn't change anything in the table and there are no errors whatsoever.

  • 写回答

2条回答 默认 最新

  • ds122455 2015-01-28 09:46
    关注

    So today I finally made it! I was looking at the whole thing wrong. There where no flaws in the script, except for maybe the fact that I didn't comment it. Today I started commenting everything and gave my variables better names and it turns out, I've been thinking wrong. The update queries worked, but they updated the wrong lines, hence it seemed like nothing happened.

    Here is the final solution:

    <?php
    
        $absid = isset($_GET['cid']) ? $_GET['cid'] : "";   // ID of absolute row
        $parid = isset($_GET['pid']) ? $_GET['pid'] : "";   // Absolute row's parent ID
        $abspos = isset($_GET['ord']) ? $_GET['ord'] : "";  // Current position of absolute row
        $newrelpos = $abspos;                               // New position of relative row
        $dir = isset($_GET['dir']) ? $_GET['dir'] : "";     // Direction to move absolute row
    
        if($absid == "" || $parid == "" || $abspos == "" || $dir == "") die('missing variable(s)');
    
        // Generate absolute row's new position
        if($dir == "up") {
            $newabspos = $abspos - 1;
        } elseif($dir == "down") {
            $newabspos = $abspos + 1;
        } else {
            die('direction not set');
        };
    
        $relpos = $newabspos;   // Current position of relative row
    
        // Fetch relative row's ID
        $stmt = $dbh -> prepare("SELECT id FROM csa_categories WHERE foralder = :parid AND ordning = :pos");
        $stmt -> bindValue(":parid", $parid);
        $stmt -> bindValue(":pos", $relpos);
        $stmt -> execute();
        $row = $stmt -> fetch();
    
        $relid = $row['id'];    // Relative row's ID
    
        // Update absolute row
        $astmt = $dbh -> prepare("UPDATE csa_categories SET ordning = :newabspos WHERE id = :absid");
        $astmt -> bindValue(":newabspos", $newabspos);
        $astmt -> bindValue(":absid", $absid);
        $astmt -> execute();
    
        // Update relative row
        $rstmt = $dbh -> prepare("UPDATE csa_categories SET ordning = :newrelpos WHERE id = :relid");
        $rstmt -> bindValue(":newrelpos", $newrelpos);
        $rstmt -> bindValue(":relid", $relid);
        $rstmt -> execute();
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥150 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装