如何使用同一个表的多行数据在Mysqli表中插入多行

时间:2015-11-18 13:01:45

标签: php mysql mysqli

我正在尝试使用同一个表中多行的数据将新行插入tblsubscriptions。我可以创建多个条目,但它们都具有相同的user_id。这是我的代码:

    if ($stmt = $mysqli->prepare("UPDATE `tblsubscriptions` SET `inactive_by`='transfered'  WHERE `order_id`=? ")) {
        $stmt->bind_param('i', $order_id);
        stmt->execute() or trigger_error($stmt->error); //Execute the statement
        $row_count= $stmt->affected_rows;
        $result = "success";
    } else {
        die("Error: " . $mysqli->error);
        $result = "failure";
    }

    if ($result === success) {
        for ($i = 0; $i <= $row_count - 1; $i++) {
            if ($stmt = $mysqli->prepare("SELECT `user_id`, `subscribed_for`, `plan_start_date`, `plan_end_date`, `actual_start_date`, `actual_end_date`, `paid_by`, `current_status` FROM tblsubscriptions WHERE `order_id`=? && `inactive_by`='transfered'")) {
                $stmt->bind_param('i', $order_id);
                $stmt->execute() or trigger_error($stmt->error); //Execute the statement
                $stmt_result = get_result($stmt);
                $user_id = $stmt_result[0]['user_id'];
                $subscribed_for = $stmt_result[0]['subscribed_for'];
                $plan_start_date = $stmt_result[0]['plan_start_date'];
                $plan_end_date = $stmt_result[0]['plan_end_date'];
                $actual_start_date = $stmt_result[0]['actual_start_date'];
                $actual_end_date = $stmt_result[0]['actual_end_date'];
                $paid_by = $stmt_result[0]['paid_by'];
                $current_status = $stmt_result[0]['current_status'];
                if ($stmt = $mysqli->prepare("INSERT INTO `tblsubscriptions`(`user_id`, `subscribed_for`, `plan_start_date`, `plan_end_date`, `actual_start_date`, `actual_end_date`, `paid_by`, `order_id`, `current_status`, `admin_id`)VALUES(?,?,?,?,?,?,?,?,?,?)")) {
                    $stmt->bind_param('isssssiisi', $user_id, $subscribed_for, $plan_start_date, $plan_end_date, $actual_start_date, $actual_end_date, $paid_by, $order_id, $current_status, $admin_id);
                    $stmt->execute() or trigger_error($stmt->error); //Execute the statement    
                    $result = "success";
                } else {
                    die("Error: " . $mysqli->error);
                    $result = "failure";
                }
            } else {
                die("Error: " . $mysqli->error);
                $result = "failure";
            }
        }
    } else {
        $result = "NO Order";
    }

0 个答案:

没有答案