• I have a custom post type that inserts user details into a custom table from a form in the WordPress back-end. Here’s the code for the form:

    `function display_add_user_metabox(){
    echo
    ‘<form method=”post” enctype=”multipart/form-data” action=”‘.htmlspecialchars($_SERVER[‘PHP_SELF’] ).'”>’,
    ‘<p><label for”firstname” >First Name</label>’,
    ‘<input type=”text” name=”firstname”></p>’,
    ‘<p><label for”surname” >Last Name</label>’,
    ‘<input type=”text” name=”surname” > </p>’,
    ‘<label for”nickname”>Nickname</label>’,
    ‘<input type=nick”text” name=”nickname” ></p>’,
    ‘<label for”userpic”>user Image</label>’,
    ‘<input type=”file” name=”userpic” ></p>’,
    ‘<input type =”submit” name=”submit” value=”Add user” >’,
    ‘</form>’;
    }`

    and the function to insert the users:

    ` function add_user(){

    global $wpdb;
    $table_name = $wpdb->prefix . “users”;
    $name = $_POST[‘firstname’];
    $surname = $_POST[‘surname’];
    $nickname = $_POST[‘nickname’];
    $upload = wp_upload_bits($_FILES[“userpic”][“name”], null, file_get_contents($_FILES[“userpic”][“tmp_name”]));

    $wpdb->insert(
    $table_name,
    array(
    ‘user_name’ => $name,
    ‘user_surname’ => $surname,
    ‘user_nickname’ => $nickname,
    ‘user_image’ => $upload[‘url’]
    ),
    array(
    ‘%s’,
    ‘%s’,
    ‘%s’,
    ‘%s’
    )
    );

    }
    if( isset($_POST[‘submit’]) ) {
    add_user();
    }`

    This doesn’t insert anything in the table. However if I comment out the $upload or anything related to the image, everything works fine. What am I missing?

Viewing 7 replies - 1 through 7 (of 7 total)
  • I would add a ‘echo print_r($upload);’ just after the

    $upload = wp_upload_bits($_FILES[“userpic”][“name”], null, file_get_contents($_FILES[“userpic”][“tmp_name”]));

    to see what you are getting back.

    Thread Starter pamsillah

    (@pamsillah)

    That doesn’t return anything at all. ??

    Try
    echo 'debug01='.print_r($upload);
    then you can look at the source of the page and tr a find on ‘debug01’ to see if it is dumping anything

    Thread Starter pamsillah

    (@pamsillah)

    The search on the source of the page returns nothing as well. However if i add an else statement after the if isset like so:

    if( isset($_POST['submit']) ) {
       add_user(); 
    } else{
    	echo "Could not upload user details";
    } 

    the string is echoed back in the FRONTEND of my site.

    Well that explains why the code i told you to enter is not executing. It means $_POST[‘submit’] is not set so you never call add_user()

    Thread Starter pamsillah

    (@pamsillah)

    Ok. I changed it to if`(! isset($_POST[‘submit’]) ) {
    add_user(); `

    and im getting this:
    Warning: file_get_contents(): Filename cannot be empty in C:\xampp\htdocs\wordpress\wp-content\themes\themename\functions.php on line 31
    Array ( [error] => Empty filename ) debug01=1

    i don’t understand why its saying the filename is empty though?

    You put single quote marks in the IF statement?? Can you please provide 5 lines before and after your code .

    The php variable $_POST is an array. What is probably happing is that since you are using this in the WP backend, when you hit the submit button, it is not your form’s submit button but WP’s and it does not have the “name=…” in the input statement. You might want to echo $_post to see what it contains. I’ll bet it does not contain a key of ‘submit’

    Just after
    function add_user(){
    add
    echo 'debug2 ='.print_r($_POST).'<br><br>';
    and look at the results.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Upload images to custom database table in admin backend’ is closed to new replies.