PHP Prepared Statements,如何将数据从一列移到另一列

时间:2018-04-29 15:25:20

标签: php sql-update prepared-statement

我正在尝试将数据从我的表中的一列移动到另一列,然后将初始列设置回零。虽然初始列重置为零,但它也将零添加到目标列的末尾(我将为上下文提供一些屏幕截图)。

这是我的代码:

+--------------+
|stringToDouble|
+--------------+
|1.0 3.0 4.0   |
|2.0 1.0 3.0   |
+--------------+

这是函数运行之前的表: Before

函数运行后的表: After

运行时,我用来检查调试echos以查看'active_da'是否设置正确会产生这个结果(4是按钮ID,它们现在只是函数的触发器): Results from echo

我的问题是,我怎样才能简单地将'active_da'列转移到'completed_da'列的末尾而不添加额外的零?

编辑:我相信这是将其抛弃的查询

function complete_da($da_id) {
        include 'connect.php';
        include 'globals.php';

        retrieve_user_data($_SESSION['logged_in']);
        nation_data_grab($_SESSION['logged_in']);
        echo "u_n_i: ".$user_nation_information['active_da']." <br>";

        $ret_da_info_sql = "SELECT `active_da`, `completed_da` FROM `nations` WHERE `nations`.`user_id` = ?;";
        $ret_da_info_stmt = mysqli_stmt_init($connect);
        if(!mysqli_stmt_prepare($ret_da_info_stmt, $ret_da_info_sql)) {
            echo "Statement 1 Failed";
        } else {
            mysqli_stmt_bind_param($ret_da_info_stmt, "i", $user_data['id']);
            mysqli_stmt_execute($ret_da_info_stmt);

            // Use get_result for *, bind_result for specific columns

            mysqli_stmt_bind_result($ret_da_info_stmt, $active_da, $completed_da); 
            mysqli_stmt_fetch($ret_da_info_stmt);
            echo "u_n_i: ".$user_nation_information['active_da']." <br>";
            $updated_cda_str = ($completed_da .= ",".$active_da);
        }
        mysqli_stmt_close($ret_da_info_stmt);

        $update_compl_with_active_sql = "UPDATE `nations` SET `completed_da` = ? WHERE `nations`.`user_id` = ?;";
        $update_compl_with_active_stmt = mysqli_stmt_init($connect);
        if(!mysqli_stmt_prepare($update_compl_with_active_stmt, $update_compl_with_active_sql)) {
            echo "Statement 2 Failed";
        } else {
            mysqli_stmt_bind_param($update_compl_with_active_stmt, "si", $updated_cda_str, $user_data['id']);
            mysqli_stmt_execute($update_compl_with_active_stmt);

            $set_active_zero_sql = "UPDATE `nations` SET `active_da` = ? WHERE `nations`.`user_id` = ?;";
            $set_active_zero_stmt = mysqli_stmt_init($connect);

            $zero = 0;

            if(!mysqli_stmt_prepare($set_active_zero_stmt, $set_active_zero_sql)) {
                echo "Statement 2 Failed";
            } else {
                mysqli_stmt_bind_param($set_active_zero_stmt, "ii", $zero, $user_data['id']);
                mysqli_stmt_execute($set_active_zero_stmt);
            }
            mysqli_stmt_close($set_active_zero_stmt);
        }
        mysqli_stmt_close($update_compl_with_active_stmt);


        mysqli_close($connect);
    }

0 个答案:

没有答案