TechnoDiver Posted July 29, 2021 Share Posted July 29, 2021 I'm getting an undefined array key warning from an array key that has been defined like the other keys in the array. The method call is as follows: $post_obj->addNews( $_POST['title'], $_POST['content'], $_POST['category'], $_POST['status'], $_POST['post_type'], $_POST['tags'], $_POST['post_image'] ); The method and class are: class Post { private $conn; private $user_obj; public function __construct($conn, $user) { $this->conn = $conn; $this->user_obj = new User($conn, $user); } public function addNews($title, $content, $category, $status, $type, $tags, $image) { if(!empty($title) && !empty($content)) { $title = strtoupper($title); $title = mysqli_real_escape_string($this->conn, $title); $content = nl2br($content); $content = mysqli_real_escape_string($this->conn, $content); $added_by = $this->user_obj->getUsername(); $query = mysqli_query($this->conn, "SELECT top_cat_id FROM top_categories WHERE top_cat_title='$category'"); $row = mysqli_fetch_array($query); $cat_id = $row['top_cat_id']; $insert_sql = mysqli_query($this->conn, "INSERT INTO news VALUES ('', '$title', '$content', '$added_by', '$category', '$cat_id', '$image', '$tags', '$status', '$type', '0', '0', '0', '0');"); } } } the relevant HTML is: <div class="form-group"> <label>Image</label> <input type="file" name="post_image" accept="image/*" onchange="preview_image(event)" value="Choose Image"> <br> <label>Image Preview</label><br> <img class="img-rounded" id="output_image" width="150" height="150" /> </div> The warning is an undefined array key on 'post_image'. But it's been defined like all the others. There's nothing I could find relevant to this and I'm lost. Is the problem because it's an image? I see no reason why 'post_image' remains undefined. If anyone feels like looking it over, I always appreciate the extra direction, I'm lost on this Quote Link to comment https://forums.phpfreaks.com/topic/313451-undefined-image-array-key-that-has-been-defined/ Share on other sites More sharing options...
requinix Posted July 29, 2021 Share Posted July 29, 2021 File uploads do not go in $_POST. They go in $_FILES. https://www.php.net/manual/en/features.file-upload.post-method.php Quote Link to comment https://forums.phpfreaks.com/topic/313451-undefined-image-array-key-that-has-been-defined/#findComment-1588687 Share on other sites More sharing options...
kicken Posted July 29, 2021 Share Posted July 29, 2021 Uploaded files are in $_FILES, not $_POST. 1 Quote Link to comment https://forums.phpfreaks.com/topic/313451-undefined-image-array-key-that-has-been-defined/#findComment-1588688 Share on other sites More sharing options...
requinix Posted July 29, 2021 Share Posted July 29, 2021 Seconds apartĀ and similarly phrased! Quote Link to comment https://forums.phpfreaks.com/topic/313451-undefined-image-array-key-that-has-been-defined/#findComment-1588690 Share on other sites More sharing options...
TechnoDiver Posted July 29, 2021 Author Share Posted July 29, 2021 Alright, thanks. I knew about $_FILES but I didn't realize that the values didn't pass into $_POST too Quote Link to comment https://forums.phpfreaks.com/topic/313451-undefined-image-array-key-that-has-been-defined/#findComment-1588697 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.