• Martin

    (@astronaut123)


    I’m trying to write some data from the frontend into a custom database table.

    As long there is no value in the input field this script creates an empty line in the database table, but if the input field has some value nothing happens. Where is the mistake?!?!?

    function insert_directory() {
    
    //get the form elements and store them in variables
    $name=$_POST['name'];
    $submit=$_POST['submit'];
    
    if(isset($_POST['submit'])) {global $wpdb; $wpdb->insert( 'directory', array('name' => $name,));
    
    $inserted_entry = $wpdb->get_row($wpdb->prepare("SELECT * FROM directory ORDER BY id DESC LIMIT 0,1"));
    echo '<h3>The following entries were succsessfully added</h3>';
    echo'<table class="checkentries directory">';
    echo '<tr><td>Name:</td><td>'.$inserted_entry->name.'</td></tr>';
    echo '</table>';
    
    }  else {
    
     echo '<form method="post" action="">';
     echo '<table class="insert directory">';
     echo '<tr><td>Name:</td><td><input id="name" type="text" name="name" /></td></tr>';
     echo '</table>';
     echo '<tr class="submit"><td colspan="2"><input type="submit" name="submit" value="Insert Entry" />';
     echo '</form>';
    }
    }

The topic ‘Writing into MySQL Table’ is closed to new replies.