我有这个工作,它将复制行,然后将新行链接到上一行。
我的问题在于复制NULL值。当我运行这个时,所有空值都以空白的形式进入新行。
如果它原来是NULL,我如何才能将值更改为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\n", $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();
}
}
答案 0 :(得分:1)
根据评论中的信息生成:
#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"]."');";
希望您可以将其合并到您的代码中。