Support » Developing with WordPress » wpdb Cannot Update column in Database

  • Resolved TheXrion

    (@negarehgfx)


    In custom table I trying to update specific column with using $wpdb->update class and using HTML form for updating one column that have same id (in here dlname Is ID of data ).

    The problem is this When I trying to update column in my table using html form The page only will Refresh

    BUT When i manually set ID in codes and THEN typing and submitting new value by html form it will work like charm.

    It seems to be machine cannot understand value that inputted from the form This is my code :

    if(isset($_POST['btnUpdate'])){
        global $wpdb;
        $table_name='wp_AR_tb_name';
    
        $data_array = array(
    
        'dstatus' => $_POST['dstatuschange'],
        'downloadname' => $_POST['dlname'],
    
        );
    
        $data_where = array('downloadname' =>'dlname');
    
                $wpdb->update($table_name,$data_array,$data_where);

    And this is my Form :

    <form method="POST">
    
    <label> Input One : </label>
    <input type="text" name="dlname"/>
    
    <label> Input Two : </label>
    <input type="text" name="dstatuschange" />
    
    <button type="submit" name="btnUpdate"> Submit </button>
    </form>
Viewing 6 replies - 1 through 6 (of 6 total)
  • I dont see anything wrong with the code.

    Have you confirmed that you are making it inside the if statement and that the $_POST does contain the expected fields?

    
    // verify there is post data and see what values are set
    if (count($_POST) > 0) {
     var_dump($_POST);
     die();
    }
    
    
    // verify that we made it past the if statement
    if(isset($_POST['btnUpdate'])){
    var_dump('made it here');
    die();
    
    Thread Starter TheXrion

    (@negarehgfx)

    Yeah, I worte this code , When i looking at these lines , I telling to myself : What the hell is going on ? And whats wrong with it ?

    I Pasted the full code up there and there is nothing to Paste , Inserting $wpdb->insert working without any problem but the updating is the matter .

    I cant get your point about ( I think there is no need to using if statement )

    Can you Please provide me ” Changed Code ” and replying it.
    Best Regards

    ok, you might check and see if the generated update query is valid.

    You will need to set the SAVEQUERIES var in your wp-config.php

    
    defined( 'SAVEQUERIES', true );
    

    Then try something like this

    
    $wpdb->update($table_name,$data_array,$data_where);
    
    // Print last SQL query string
    var_dump($wpdb->last_query);
    
    // Print last SQL query result
    var_dump($wpdb->last_result);
    
    // Print last SQL query Error
    var_dump($wpdb->last_error);
    
    
    Thread Starter TheXrion

    (@negarehgfx)

    It Print queries :
    string(102) “UPDATE wp_AR_tb_name SET dstatus = ‘6’, downloadname = ‘1265847’ WHERE downloadname = ‘dlname'” array(0) { } string(0) “”

    As i said wordpress cant understand the value of dlname that was inputted from the form
    as you seen in the prints up there..

    BUT when i change $wpdb->update to $wpdb->insert the insert query will work and understand the value of dlname !

    Thats Weird !!!

    • This reply was modified 3 years, 5 months ago by TheXrion.

    I didn’t notice this before, but this code is messing up the where statement.

    
    $data_where = array('downloadname' =>'dlname');
    

    I think it should be

    
    $data_where = array('downloadname' => $_POST['dlname']);
    

    Since i am assuming you are wanting the results where downloadname is equal to the form value from the dlname form element.

    Thread Starter TheXrion

    (@negarehgfx)

    Thank you so much @vrandom this change worked fine .
    OMG ! Last night i try the same code but i used $_GET instead of $_POST !

    Solved : https://wordpress.org/support/topic/wpdb-cannot-update-column-in-database/#post-11240040

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘wpdb Cannot Update column in Database’ is closed to new replies.