我当前的数据库查询会覆盖该行,如果该行中的列发生更改,我会尝试添加新行,让我们展示一个示例。
这是行。当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();
}
}
答案 0 :(得分:0)
这很容易。您可以使用jquery.post或ajax。如果你想使用submit:添加隐藏元素并将其设置为更新或插入相关的决定。