我当前的数据库查询覆盖了该行,如果该行中的列发生更改,我正在尝试添加一个新行,让我举个例子。
这是行。当 post_id 像该 id 的新行一样更改 id 时
这就是 id 更改时发生的情况,它会覆盖.. 而不是 id 就像创建的新行一样..
这是插入数据和更新的函数
//This is the INSERT
// Execute query
$wpdb->query(
$wpdb->prepare("INSERT INTO $table_name
(post_id, name, data, date_c, date_m)
VALUES (%d, %s, %s, %d, %d)",
'',
'',
time(),
time()
)
);
//This is the UPDATE
// DB data
$post_id = $wpdb->escape($alldata['postid']['thepostid']);
$name = $wpdb->escape($alldata['properties']['title']);
$data = $wpdb->escape(json_encode($alldata));
// Update
$wpdb->query("UPDATE $table_name SET
post_id = '$post_id',
name = '$name',
data = '$data',
date_m = '".time()."'
ORDER BY id DESC LIMIT 1");
这是一个提交功能,当提交表单时,插入或更新行,我不知道该怎么写,如果 NEW post_id INSERT NEW row ... 希望有人可以帮助我编写该查询并给出一些建议...
更新:
<form action="<?php echo $_SERVER['REQUEST_URI']?>" method="post" class="wrap" id="ls-slider-form">
<input type="hidden" name="submitted" value="1">
<!-- Global Settings -->
<div class="ls-page ls-post-id" style="display: block;">
<div id="post-body-content">
<div id="titlediv">
<div id="titlewrap">
<input type="text" name="thepostid" value="<?php echo the_ID() ?>">
</div>
</div>
</div>
</div>
<input type="submit" />
</form>
function brash_add_settings() {
// Add slider
if(isset($_POST['submitted'])) {
// Get WPDB Object
global $wpdb;
// Table name
$table_name = $wpdb->prefix . "brash";
// Execute query
$wpdb->query(
$wpdb->prepare("INSERT INTO $table_name
(post_id, name, data, date_c, date_m)
VALUES (%d, %s, %s, %d, %d)",
'',
'',
time(),
time()
)
);
// Empty slider
$alldata = array();
// ID
$id = mysqli_insert_id();
$alldata['properties'] = $_POST['alldata']['properties'];
$alldata['postid'] = $_POST['brash-post-id']['postid'];
// DB data
$post_id = $wpdb->escape($alldata['postid']['thepostid']);
$name = $wpdb->escape($alldata['properties']['title']);
$data = $wpdb->escape(json_encode($alldata));
// Update
$wpdb->query("UPDATE $table_name SET
post_id = '$post_id',
name = '$name',
data = '$data',
date_m = '".time()."'
ORDER BY id DESC LIMIT 1");
// Echo last ID for redirect
echo $id;
die();
}
}