Foreach loop not working as expected - custom tables and references while submitting a form

I have two custom tables and i would like to have a form submission with the name of the redskabs tables to make an reference to my registreringer table. But im confused to how foreach is working in this example.

CREATE TABLE $table_registreringer 
    (
        reg_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
        dato TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
        billedeURL VARCHAR(80) NOT NULL,
        fiske_vaegt INT NOT NULL,
        fiske_laengde INT NOT NULL,         
        reg_user_id BIGINT UNSIGNED NOT NULL,
        reg_redskabs_id INT UNSIGNED NOT NULL,
        PRIMARY KEY  (reg_id),
        FOREIGN KEY  (reg_user_id) REFERENCES wp_users(id),
        FOREIGN KEY  (reg_redskabs_id) REFERENCES $table_redskaber(redskabs_id)
)

CREATE TABLE $table_redskaber 
    (
        redskabs_id INT UNSIGNED AUTO_INCREMENT NOT NULL,
        redskabs_navn CHAR (20),
        PRIMARY KEY  (redskabs_id)
)

Here is my registration.php page

global $wpdb;

if ( isset( $_POST['submit'] ) ){
$current_user = wp_get_current_user();
$dato = $_POST['dato'];
$billedeURL = $_POST['billedeURL'];
$fiske_vaegt = trim( $_POST['fiske_vaegt'] );
$fiske_laengde = trim( $_POST['fiske_laengde'] );
// I Don't know how to connect the chosen name to the correspondent ID so that I can use the foreign key and post my registration --  
$redskabsID = $wpdb-get_row( $wpdb-prepare("SELECT redskabs_id, redskabs_navn FROM wp_redskaber WHERE redskabs_id = %d", $redskabsID) );

echo $wpdb-show_errors();
$registrering = $wpdb-insert( 
$wpdb-prefix . 'registreringer',
    array(
        'reg_id'            = '',
        'dato'              = $dato,
        'billedeURL'        = $billedeURL,
        'fiske_vaegt'       = $fiske_vaegt,
        'fiske_laengde'     = $fiske_laengde,
        'reg_user_id'       = $current_user-ID,
        'reg_redskabs_id'   = $redskabsID
        ),
    array(
        '%d',
        '%d',
        '%s',
        '%d',
        '%d',
        '%d',
        '%d'
        )
    );

}

form

form method="post"
    h3Registering af din fangst/h3
    plabellængden/label/p
    pinput type="number" value="" name="fiske_laengde" id="fiske_laengde" /cm/p
    plabelvægten/label/p
    pinput type="number" value="" name="fiske_vaegt" id="fiske_vaegt" /kg/p
    plabelBillede/label/p
    pinput type="string" value="" name="billedeURL" id="billedeURL" //p
    plabeldato/label/p
    pinput type="datetime-local" name="dato" value="?php echo date("Y-m-d\TH:i:s",time($dato)); ?"//p
    plabelFangst udstyr/label/p
    pinput type="radio" value="1" name="redskaber[]" //p
    pinput type="radio" value="2" name="redskaber[]" //p
    pinput type="radio" value="3" name="redskaber[]" //p

my foreach code for my radio buttons

?php 

    $results = $wpdb-get_results( "SELECT redskabs_id, redskabs_navn FROM `wp_redskaber`" );

    $_POST['results'];

    foreach ( $_POST['results'] as $redskaber ) 
    {
        echo $redskaber;
    }

Topic radio wpdb Wordpress

Category Web


So the first part of your question is just how to get the contents of wp_redskaber into radio buttons. As you've attempted, use $wpdb->get_results(); to query the table. By default each row will be an Object, so you'd output it like this:

global $wpdb;

$results = $wpdb->get_results( "SELECT * FROM wp_redskaber" );

foreach ( $results as $redskaber ) {
    echo sprintf( 
        '<label><input type="radio" name="redskabs_id" value="%s"> %s</label>', 
        esc_attr( $redskaber->redskabs_id ), 
        $redskaber->redskabs_navn
    );
}

Because get_results() returns an array of objects, you get the id and name of each column with object notation: $redskaber->redskabs_id, and $redskaber->redskabs_nav.

So I've looped over each result and created a radio button input for each. Radio buttons should all have the same name, but different values. In this case I've given them the name redskabs_id. Note that I've used sprintf() to put the column values into the HTML. I find this neater than opening and closing strings or PHP tags, but you can do it whatever way you like.

So when the form is submitted you access the selected ID with $_POST['redskabs_id'].

So in your second block of code, you set $redskabsID to:

$redskabsID = $_POST['redskabs_id'];

$wpdb->insert() will properly escape it for SQL for you, so you're safe there, but for some extra validation you might want to confirm that it's a number with absint():

$redskabsID = absint( $_POST['redskabs_id'] );

That will get the ID inserted into the reg_redskabs_id column. You don't need to do anything special to link them. As long as the ID in that column matches an ID in the wp_redskaber table you can match them.

If you're doing some validation and want to be absolutely certain that $redskabsID is a valid ID in the wp_redskaber table, you can get an array of valid IDs using $wpdb->get_col(), and then compare that to the submitted value:

global $wpdb;

$valid_redskabs_ids = $wpdb->get_col("SELECT redskabs_id FROM wp_redskaber");

if ( ! in_array( $_POST['redskabs_id'], $valid_redskabs_ids ) ) {
    // Invalid redskabs_id submitted. Handle error here.
}

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.