0

Im using custom tables to make a user register page of something they've done. I'm having problems understanding wpdb and how to use it to insert the data from my html form and how to connect those files and function.

When posting my data im getting errors with

wpdb->prepare is missing 2 arguments with is line:

$redskabsID = $wpdb->get_row( $wpdb->prepare("SELECT redskabs_id FROM wp_redskaber WHERE redskabs_id = %d") );

and a error with

mysql_real_escape_string() expects parameter 1 to be string 

with my insert

$registrering = $wpdb->insert( 
$wpdb->prefix . 'registreringer',
array(
    'dato'              => new DateTime( 'now' ),
    'fiske_vægt'        => apply_filters('pre_register_fiske_vægt', $fiske_vægt ),
    'fiske_længde'      => apply_filters('pre_register_fiske_længde', $fiske_længde ),
    'reg_user_id'       => $current_user->ID,
    'reg_redskabs_id'   => apply_filters('pre_register_reg_redskabs_id', $redskabsID)
    )
); 

Here is the relevant info:

Table 1

CREATE TABLE $table_registreringer 
    (
        reg_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
        dato TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
        fiske_vægt INT NOT NULL,
        fiske_længde 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)

table 2

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

Function or the logic behind my form

<?php 
if ( ! is_user_logged_in() ) {
    return;
}
?>
<div class="wrapper">

<?php

global $wpdb;

if ( isset( $_POST['submit'] ) ){

$current_user = wp_get_current_user();

$fiske_vægt = trim( $_POST['fiske_vægt'] );
$fiske_længde = trim( $_POST['fiske_længde'] );

$redskabsID = $wpdb->get_result ("SELECT redskabs_id FROM wp_redskaber");

$registrering = $wpdb->insert( 
$wpdb->prefix . 'registreringer',
    array(
        'dato'              => new DateTime( 'now' ),
        'fiske_vægt'        => apply_filters('pre_register_fiske_vægt', $fiske_vægt ),
        'fiske_længde'      => apply_filters('pre_register_fiske_længde', $fiske_længde ),
        'reg_user_id'       => $current_user->ID,
        'reg_redskabs_id'   => apply_filters('pre_register_reg_redskabs_id', $redskabsID)
        )
    );
}
?>

and my Html form with a foreach to get everything from wp_redskaber table into my form

<form method="post">
    <h3>Registering af din fangst</h3>
    <p><label>længden</label></p>
    <p><input type="number" value="" name="fiske_længde" id="fiske_længde" />cm</p>
    <p><label>vægten</label></p>
    <p><input type="number" value="" name="fiske_vægt" id="fiske_vægt" />kg</p>
    <p><label>Fangst udstyr</label></p>
    <?php 

    $results = $wpdb->get_row( "SELECT redskabs_navn FROM `wp_redskaber`" );

    echo "\t<tr>\n";

    foreach ( $results as $redskaber ) 
    {
        echo "\t\t<td>$redskaber->redskabs_navn</td>\n";
    }

    echo "<td>"
        ."<label>Spin <input type='radio' name='picks[$redskaber]' value='spin'></label><br>"
        ."<label>Flue <input type='radio' name='picks[$redskaber]' value='flue'></label><br>"
        ."<label>Orm <input type='radio' name='picks[$redskaber]' value='orm'></label><br>"
        ."</td>";

    echo "\t</tr>\n";


    $wpdb->show_errors(); 
    ?> 
    <button type="submit" name="btnregister" class="button" >Submit</button>
    <input type="hidden" name="submit" value="submit" />
</form>

1 Answer 1

1

You're using a placeholder ("%s") in your SQL for prepare, but you don't pass it a value to put in place of that placeholder, hence the complaint.

use $wpdb->prepare("SELECT ... %s", $myvalue)

And I'm pretty sure your other error stems from you passing in a DateTime object. $wpdb will not convert that automatically, it expects strings, which it then passes to mysql_real_escape_string() (or mysqli_real_escape_string() depending on what the connection is set up with).

Side note: you might want to overthink using special chars in variable names. If somebody who doesn't use your language's keyboard layout ever needs to edit that script, he'll have a fun time copy pasting æ in variables :)

6
  • I see your point with the language :) And by giving the prepare a value and changed the date by having the user input a date and it stopped complaining. But thanks mate :) Right now i'm not getting any errors, but it still doesn't post into the db ::/
    – Mg10
    Commented Oct 30, 2017 at 14:46
  • Did you enable show_errors? Also look at $wpdb->last_query for the actual SQL and run that manually against your DB for easier debugging.
    – janh
    Commented Oct 30, 2017 at 14:52
  • $wpdb->last_query - When i use this I can see that my html form isn't giving any value to the wpdb insert. and then I get Null value for my Auto incrementet Id for my primary key - But thanks again now I have something to look into
    – Mg10
    Commented Oct 30, 2017 at 15:00
  • Make sure to look at what's actually in $_POST. Having special chars in field names might lead to wacky results depending on what encoding your PHP-file is in when you access $_POST.
    – janh
    Commented Oct 30, 2017 at 15:54
  • INSERT INTO wp_registreringer ( dato, fiske_vaegt, fiske_laengde, reg_user_id, reg_redskabs_id) VALUES (0, 0, 0, 1, NULL) Is what wpdb->last_query; gives me. And it tells me my insert is wrong.
    – Mg10
    Commented Oct 31, 2017 at 8:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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