0

Custom form is taking decimal but db inserts whole numbers but with a .0 behind. Why won't it give me the correct decimal number ?

Db field

    fiske_vaegt DECIMAL( 2,1 ) NOT NULL,

Form input field

    <p><input type="number" step="any" name="fiske_vaegt" id="fiske_vaegt" />kg</p>

CHECK the fiske_vaegt field if i Input 2.4 it changes it to 2.0 and 2.7 to 3.0 enter image description here

Anyone who have any experience with this goddamned problem?

post

    if ( isset( $_POST['submit'] ) ){
$fiske_vaegt = $_POST['fiske_vaegt'];

}

insert

$registrering = $wpdb->insert( 
$wpdb->prefix . 'registreringer',
    array(
        'fiske_vaegt'       => $fiske_vaegt
        ),
    array(
        '%d'
        )
    );
  • The obvious reason should be the code that handles the fiske_vaegt value and sends it to the query/db. I suppose you use Mysql and by default, it should honor the decimal place and not round them. – Drupalizeme Nov 2 '17 at 10:17
  • I've added the post method into the question. I dont really see the problem i'm getting – Mg10 Nov 2 '17 at 10:21
  • %d is the problem – Drupalizeme Nov 2 '17 at 10:24
4

Within $wpdb->insert, you are using %d which is used to store integer, use %f instead which will store float/decimal values.

$registrering = $wpdb->insert( 
    $wpdb->prefix . 'registreringer',
    array(
        'fiske_vaegt'       => $fiske_vaegt
    ),
    array(
        '%f'
    )
);
  • Gave you the answer points for best illustration of the answer – Mg10 Nov 2 '17 at 10:44
2

'%d' modifier is for integers, not decimals use the %f (floats or the %s strings) and it would save it correctly.

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.