My current DB query overwrites the row, I am trying to add a new row if a column in that row changes, let me show an example.
This is the row. When post_id changes id like a new row for that id
This is what happens when the id changes, it overwrites.. instead id like a new row created..
Here is the function that INSERTS the data and UPDATES
//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");
It's a submit function, when the form is submitted the row is inserted or updated, I am clueless as to how I would write it to say, if NEW post_id INSERT NEW row... Hoping someone can help me write that query and give some advice...
UPDATE:
<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();
}
}