Click here to Skip to main content
15,850,103 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have this code and I get a "Warning: Undefined array key "imgDate" " on line 8.

Line 8 is:
PHP
$imgDate = $_POST['imgDate'];


PHP
<?php

 require_once __DIR__ . '/db.php';

 if (count($_FILES) > 0) {
     if (is_uploaded_file($_FILES['userImage']['tmp_name'])) {
         $imgData = file_get_contents($_FILES['userImage']['tmp_name']);
         $imgDate = $_POST['imgDate'];
         $imgType = $_FILES['userImage']['type'];
         $sql = "INSERT INTO tbl_image(imageType ,imageData , imageDate) VALUES(?, ?, ?)";
         $statement = $conn->prepare($sql);
         $statement->bind_param("sss", $imgType, $imgData, $imgDate);
         $current_id = $statement->execute() or die("Error: Problem on Image Insert<br/>" . mysqli_connect_error());
     }
 }
 ?>

<HTML>
<HEAD>
<TITLE>Upload Spil</TITLE>
<link href="css/form.css" rel="stylesheet" type="text/css" />
<link href="css/style.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" href="//code.jquery.com/ui/1.13.2/themes/base/jquery-ui.css">
<link rel="stylesheet" href="/resources/demos/style.css">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://code.jquery.com/jquery-3.6.0.js"></script>
<script src="https://code.jquery.com/ui/1.13.2/jquery-ui.js"></script>
<script>
$( function() {
$("#datepicker").datepicker({changeYear: true, changeMonth: true, yearRange: "1900:+00", onSelect: function(dateText){
$('#imgDate').text(dateText)
    }
});

});
</script>

<style>
.image-gallery {
    text-align:center;
}
.image-gallery img {
    padding: 3px;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.5);
    border: 1px solid #FFFFFF;
    border-radius: 4px;
    margin: 20px;
}
</style>
</HEAD>
<BODY>
    <form name="frmImage" enctype="multipart/form-data" action=""
        method="post">
        <div class="phppot-container tile-container">
            <label>Upload Image File:</label>
            <div class="row">
                <input name="userImage" type="file" class="full-width" />
            </div>

            <div class="row">
                <p>Copyright Date: <input type="text" id="datepicker"></p>
            </div>

            <div class="row">
                <input type="submit" value="Submit" />
            </div>
        </div>
    </form>
    <?php
echo "<div class='evnt-p'>No Events on : <span id='imgDate'></span></div>";
    ?>

</BODY>
</HTML> 


What I have tried:

Probabaly, the two worst things: Google and Youtube

If I change line 8 to $imgDate = imgDate, it does store imgDate in the database rather than the selected date.

Where did I mess up?
Posted
Updated 20-Sep-23 4:47am
v2

1 solution

Look at the line, and at the error message:
PHP
$imgDate = $_POST['imgDate'];
Error
Undefined array key "imgDate" 
The error says that there is no element in the array with the index "imgDate" - so you need to look at your HTML to find out what is being POSTed to your code. Almost certainly, it's a spelling mistake somewhere, maybe you forgot to add name="imgDate" to the HTML?

You should expect to get syntax errors every day, probably many times a day while you are coding - we all do regardless of how much experience we have! Sometimes, we misspell a variable, or a keyword; sometimes we forget to close a string or a code block. Sometimes the cat walks over your keyboard and types something really weird. Sometimes we just forget how many parameters a method call needs.

We all make mistakes.

And because we all do it, we all have to fix syntax errors - and it's a lot quicker to learn how and fix them yourself than to wait for someone else to fix them for you! So invest a little time in learning how to read error messages, and how to interpret your code as written in the light of what the compiler is telling you is wrong - it really is trying to be helpful!

So read this: How to Write Code to Solve a Problem, A Beginner's Guide Part 2: Syntax Errors[^] - it should help you next time you get a compilation error!

And spending a little time learning to understand syntax error messages will save you a huge amount of time in future: you waited at least 1/4 hour for Chris to reply, then your email system probably added another 10 minutes or so, plus the time it took you to type up the question once you had found this site and created an account. Chances are that you could have saved a significant chunk of that time if you knew how to read them!

I'm not saying we don't want to help you fix them - sometimes I can't see my own errors because I read what I meant to write - but fixing syntax errors is part of the job, and if you can't do it for yourself people are going to look at you as a bit weird should you get a job in the industry!
 
Share this answer
 
Comments
Bo Herrmannsen 20-Sep-23 7:30am    
problem is also that i hack together blocks example codes

i know that the date i want to store is generated in line 33: $('#imgDate').text(dateText)

and that its printed in line 72: echo "No Events on : ";

and that i have a table above and comparing those i can see that indeed in do not have name in line 72

also the printing at line 72 is outside the form... maybe that is another mistake?
OriginalGriff 20-Sep-23 8:36am    
"problem is also that i hack together blocks example codes"
That's your problem! You can't just grab random code and throw it together, bash it around with a hammer to get it to fit a bit and expect it to "just work". That's never going to happen: each bit of code you sticky tape into you app expects the rest of the code around it to be working "this way" while the bit of code you glued in next to it demands "that way"! :laugh:

It's like needing a tire for your car and just grabbing the nearest wheel and shoving that on instead. You end up with a moped wheel and a Ferrari on the front and a Ford and a tractor on the back - and that just won't go in a straight line, much less round corners!

You need to learn the language, learn the framework it';s going to run under, and write code to fit your needs. Yes, there is a time and a place for "internet code" but it's never going to work if your whole app is thrown together from bits and pieces. I use code - but as "proven working" black box assemblies where I can use classes and call methods to do specific things without having to "know" how they work internally. But I have to write a lot of "glue code" to interface them with my code.
Dave Kreskowiak 20-Sep-23 10:19am    
The problem with "hacking together blocks of code" is that you MUST understand precisely what each block does and how they work to be able to successfully integrate them together and get it all to function.

Copy'n'Paste coding can work, but it's not for beginners. The requirements on you to know what you're doing go way up when you use someone's examples as your code base.

Copy'Paste'n'Pray is not a valid strategy for writing code.

OriginalGriff 20-Sep-23 10:50am    
But ... but ... they all come from ChatGPT so they *must* work together! :laugh:
Bo Herrmannsen 20-Sep-23 15:25pm    
hehe, i'm a 1977 year model, and life have thrown me a ball with autism written on it

not that i will use that as an excuse but it does make learning stuff hard for me

bed time but will look at the link tomorrow and then eyeball my code

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900