dongxing7318 2017-03-13 15:56
浏览 31
已采纳

复制行,丢失NULL值

I have this working where it will copy the row, and then link the new row to the previous row.

Where my issue is, is in copying over the NULL values. When I run this all null values go into the new row as blank.

How would I get it to change the value to NULL if it was originally NULL?

    $result = $mysqli->query("SELECT * FROM rdpricing WHERE psid = '$dupsid';"); 
if($result->num_rows >= "1"){
$count = $result->num_rows;
$cols = array();
$result = $mysqli->query("SHOW COLUMNS FROM rdpricing");

while ($r = $result->fetch_array(MYSQLI_ASSOC)) {
if (!in_array($r["Field"], array("rdpid", "psid", "rdold"))) { //Excluding these columns
    $cols[] = $r["Field"];
    }
}

// Build and do the insert
$result = $mysqli->query("SELECT * FROM rdpricing WHERE psid = '$dupsid';"); 

while ($r = $result->fetch_array(MYSQLI_ASSOC)) {

$insertSQL = "INSERT INTO rdpricing (" . implode(", ",$cols) . ", rdold) VALUES ("; 
$count = count($cols);

foreach($cols as $counter=>$col) {

**// This is where I Believe it needs to happen, and what I have attempted, and it is NOT working**

    if(empty($r[$col]) || is_null($r[$col]) || $r[$col] == ""){
        $r[$col] = NULL;
    }

    $insertSQL .= "'" . $mysqli->real_escape_string($r[$col]) . "'";
    if ($counter < ($count - 1)) {
        $insertSQL .= ", ";
    }
 } // END foreach

 $insertSQL .= ", '".$r["rdpid"]."');";

 $mysqli->query($insertSQL);
 if ($mysqli->affected_rows < 1) {
 printf("%s
", $mysqli->error);
} else {
}
$new_id = $mysqli->insert_id;

$statement = $mysqli->prepare("UPDATE rdpricing SET `psid`=? WHERE `rdpid`=?");
        $statement->bind_param('ss', $new_psid, $new_id);
        // Execute the prepared query.
        $statement->execute();
        $statement->close();
}

}
  • 写回答

1条回答 默认 最新

  • douchen5971 2017-03-13 16:12
    关注

    Generated from info in the comments:

    #reset/create  before the foreach, create an empty array
    $insertSQLValues=array();
    
    
    #in the foreach do some on given type 
    if(is_null($r[$col])){#real null
        $r[$col] = "null";
    } else if (empty($r[$col]) || $r[$col] == ""){#empty values
        $r[$col] = "''";
    } else {#standart data
        $r[$col] = "'".$mysqli->real_escape_string($r[$col])."'";
    }
    $insertSQLValues[]=$r[$col];
    
    
    #later
    $insertSQL .= implode(', ',$insertSQLValues).", '".$r["rdpid"]."');";
    

    Hopefully you can merge that into your code.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 请问如何在openpcdet上对KITTI数据集的测试集进行结果评估?
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗