I'm trying to create a PHP script that will upload picture to a folder /img, and save picture directory to database as $img_location. When I try to upload a picture I get these errors.
Undefined index: select_picture in /Applications/XAMPP/xamppfiles/htdocs/ia2/post.php on line 65
Undefined index: select_picture in /Applications/XAMPP/xamppfiles/htdocs/ia2/post.php on line 67
Notice: Undefined index: select_picture in /Applications/XAMPP/xamppfiles/htdocs/ia2/post.php on line 68
Notice: Undefined index: select_picture in /Applications/XAMPP/xamppfiles/htdocs/ia2/post.php on line 69
Notice: Undefined variable: img_location in /Applications/XAMPP/xamppfiles/htdocs/ia2/post.php on line 115
Here is the PHP handling picture input
elseif (!$tid && !empty($_POST['select_picture'])) {
$name = $_FILES['select_picture']['name']; //line 65
var_dump($_FILES);
$size = $_FILES['select_picture']['size']; //line 67
$type = $_FILES['select_picture']['type']; //line 68
$tmp_name = $_FILES['select_picture']['tmp_name']; //line 69
$max_size = 20000000;
$extension = substr($name, strpos($name, '.') + 1);
if (isset($name) && !empty($name)) {
if (($extension == "jpg" || $extension == "jpeg") && $type == "image/jpeg" && $extension == $size <= $max_size) {
$location = "img/";
if (move_uploaded_file($tmp_name, $location . $name)) {
$img_location = "img/$name";
$smsg = "Uploaded Successfully";
} else {
$fmsg = "Failed to Upload File";
}
} else {
$fmsg = "File size should be 20 MegaBytes & Only JPEG File";
}
} else {
$fmsg = "Please Select a File";
}
Here is the part where image location is inserted into the database along with other information
if ($tid) { // Add this to the replies table:
$q = "INSERT INTO posts (thread_id, user_id, img_location, description_title, description, posted_on) VALUES ($tid, {$_SESSION['user_id']}, '" . mysqli_real_escape_string($dbc, $img_location) . "', '" . mysqli_real_escape_string($dbc, $description_title) . "', '" . mysqli_real_escape_string($dbc, $description) . "',UTC_TIMESTAMP())"; //this is line 115
$r = mysqli_query($dbc, $q);
if (mysqli_affected_rows($dbc) == 1) {
echo '<p>Your post has been entered.</p>';
} else {
echo '<p>Your post could not be handled due to a system error.</p>';
}
} // Valid $tid.
Here is the frontend
// Only display this form if the user is logged in:
if (isset($_SESSION['user_id'])) {
// Display the form:
echo '<form action="post.php" method="post" accept-charset="utf-8" enctype="multipart/form-data>';
//lines between removed
// Create picture input:
echo '<div class="form-group">';
if (isset($smsg)) {echo '<div class="alert alert-success" role="alert"';
echo $smsg;
echo '</div> }';}
if (isset($fmsg)) {echo '<div class="alert alert-danger" role="alert">';
echo $fmsg;
echo '</div> }';}
echo '<label for="select_picture">' . $words['select_picture'] . '</label>
<input type="file" class="form-control" name="select_picture" id="select_picture" ';
// Check for existing value:
if (isset($select_picture)) {
echo "value=\"$select_picture\" ";
}
echo '/><p class="help-block">Upload JPEG Files that are below 20 MB</p></div>';
} // End of $tid IF.