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条)

报告相同问题?

悬赏问题

  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。
  • ¥20 CST怎么把天线放在座椅环境中并仿真
  • ¥15 任务A:大数据平台搭建(容器环境)怎么做呢?
  • ¥15 YOLOv8obb获取边框坐标时报错AttributeError: 'NoneType' object has no attribute 'xywhr'