Upload images to custom database table in admin backend
-
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?
- The topic ‘Upload images to custom database table in admin backend’ is closed to new replies.