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 
        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_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',
        'reg_id'            => '',
        'dato'              => $dato,
        'billedeURL'        => $billedeURL,
        'fiske_vaegt'       => $fiske_vaegt,
        'fiske_laengde'     => $fiske_laengde,
        'reg_user_id'       => $current_user->ID,
        'reg_redskabs_id'   => $redskabsID



<form method="post">
    <h3>Registering af din fangst</h3>
    <p><input type="number" value="" name="fiske_laengde" id="fiske_laengde" />cm</p>
    <p><input type="number" value="" name="fiske_vaegt" id="fiske_vaegt" />kg</p>
    <p><input type="string" value="" name="billedeURL" id="billedeURL" /></p>
    <p><input type="datetime-local" name="dato" value="<?php echo date("Y-m-d\TH:i:s",time($dato)); ?>"/></p>
    <p><label>Fangst udstyr</label></p>
    <p><input type="radio" value="1" name="redskaber[]" /></p>
    <p><input type="radio" value="2" name="redskaber[]" /></p>
    <p><input type="radio" value="3" name="redskaber[]" /></p>

my foreach code for my radio buttons


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


    foreach ( $_POST['results'] as $redskaber ) 
        echo $redskaber;
  • Are you trying to loop through $results? Why are you using $_POST['results']? You don't have any fields named results. Do you know the function of $_POST? – Jacob Peattie Oct 31 '17 at 11:38
  • I've been trying a lot and used many references but no luck. I've also just looped through $results but im very confused on how to connect the value to my foreign key reference to my table – Mg10 Oct 31 '17 at 11:45
  • It's hard for me to follow along with the code because it's not English, but what are you actually trying to do? I can see in the Insert that you're putting values into the reg_user_id and reg_redskabs_id column, but I don't know what you're trying to do after that? – Jacob Peattie Oct 31 '17 at 11:50
  • Okay. reg_redskabs_id is of course my reference the table with names i need to let the user choose between before submitting. So what im trying to get with $redskabsID is a ID value with the correct name from the foreach loop. my table is having an ID row and a name row. This is where I would love to loop through the names and then give the correct ID to my wpdb->insert – Mg10 Oct 31 '17 at 12:02
  • So to be clear: You want radio button fields for each row in wp_redskaber? And the selected radio button should become reg_redskabs_id of the insert into wp_registreringer? – Jacob Peattie Oct 31 '17 at 12:16

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 ), 

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.
  • Thank you i've really been struggling! This helped me so very much. But I have a question why are you using %s in the end of the label? and absint() I know its a reference to absolute Int but I have really heard about it before but it sounds great to double check for validation :) Again thank you for understanding and help me – Mg10 Oct 31 '17 at 12:52
  • absint() makes sure that the value is an integer greater than zero and attempt to extract an integer from any string. In the sprintf() call there’s two %s one for the ID to use as a value, the other for the name for the label. – Jacob Peattie Oct 31 '17 at 12:56
  • Of course, that makes sense now :) Have a great day! – Mg10 Oct 31 '17 at 12:58
  • If i need to have a dropdown list instead of radio buttons. This is what i've done but it's not giving me the ID for the selected option <p><label>Fiskeplads</label></p> <select> <?php $lakseplads = $wpdb->get_results( "SELECT * FROM wp_laksepladser" ); foreach ( $lakseplads as $valgplads ) { echo sprintf( '<option value="reg_laks_id" value="%s"> %s', esc_attr( $valgplads->laksepladser_id), $valgplads->laksepladser_navn ); } ?> </select><br> – Mg10 Oct 31 '17 at 13:49
  • You’re setting two values on each option and not closing the option tag. Your select element is also missing a name. – Jacob Peattie Oct 31 '17 at 13:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.