0

我正在使用 PHP 代码

$sql = "SELECT * FROM $table LIMIT 1"; 
$res = mysql_query($sql) or die(mysql_error());
for ($i = 1; $i < mysql_num_fields($res); $i++) {
    $col_names .= mysql_field_name($res, $i).", ";
}
$col_names = substr($col_names, 0, -2);

$sql = "INSERT INTO $table (" . $col_names . ") SELECT " . $col_names . " FROM $table WHERE id = $id";
$res = mysql_query($sql) or die(mysql_error());

$new_id = mysql_insert_id();

复制单个记录并在插入时生成新的 PK。这很好用,但我还需要用新创建的记录上的新信息更新 3 个字段,只是想检查最好的方法是在查询运行后使用更新语句还是有办法做到这一点最初的 INSERT 查询?表格列将来会发生变化,但是目前有 20 列左右,因此列出它们可能很麻烦。

4

0 回答 0