1

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

row


This is what happens when the id changes, it overwrites.. instead id like a new row created..

enter image description here



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();
}



 }
Michael Joseph Aubry
  • 12,282
  • 16
  • 70
  • 135
  • In wordpress wp_postmeta they do something similar to what I am trying to achieve, I see what you're saying but in this case its necessary. Could you show a simple example, I cant quite picture how I would check if ID has been passed... – Michael Joseph Aubry Jul 27 '13 at 23:54
  • Yes I see what you're trying to do now... Can you post sample markup of your form, to see which data are you sending and how your update function is being called? – omma2289 Jul 27 '13 at 23:56
  • Sorry someone was at the door.. I updated the OP to be more full.. there is the form and the function that posts the data.. there is obviously ajax involved.. but I am more focused on changing the insert query so that it adds new row if the `$post_id` data changes – Michael Joseph Aubry Jul 28 '13 at 00:22
  • 1
    I think this is normally called an 'upsert' – Joe Phillips Jul 28 '13 at 00:37
  • Check [this question](http://stackoverflow.com/q/1218905/2049063) for an example of 'upsert' – omma2289 Jul 28 '13 at 06:08

1 Answers1

0

It's so easy. You can use jquery.post or ajax. If you want to use submit: add hidden element and set it value to update or insert related your decision.

Hamed Khosravi
  • 535
  • 7
  • 21