So... this problem is causing me to lose hair at this very moment.
I have a form on my site that allows users to upload images to the gallery folder, and it works fine. It uploads the file, inserts it into the database, and I can then go to the gallery and it appears.
The problem, is that for some reason that escapes me, it will not insert the $_POST['caption'] variable into the database. It doesn't even capture it when you hit submit. So now I have several images with no caption listed, even though one was entered into the box. (Please note that I have a check in place to make sure that field is not empty, and no errors are thrown when running the checks).
Here is my code for the php and form sections:
if(isset($_POST['submit']))
{
$caption = trim($_POST['caption']);
$category = trim($_POST['gallery']);
if($caption = '')
{
$error .= '<p class="error">Please enter a caption for your image.</p>';
}
if($gallery = '')
{
$error .= '<p class="error">Please select a gallery for your image.</p>';
}
//Begin upload checks
$dir = "../gallery/";
$maxsize = 5000000; // 5MB
$valid_exts = array('jpeg','jpg','png','gif');
$ok = 1;
if(isset($_FILES['file']))
{
$target_file = $dir . basename($_FILES['file']['name']);
if($_FILES['file']['size'] < $maxsize)
{
// get file extension
$ext = strtolower(pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION));
if(in_array($ext, $valid_exts))
{
if(file_exists($target_file))
{
$error .= '<p class="error">File already exists.</p>';
$ok = 0;
}
else
{
$ok = 1;
}
}
else
{
$error .= '<p class="error">Image must be a png, gif, or jpg/jpeg file.</p>';
$ok = 0;
}
}
else
{
$error .= '<p class="error">Image must be no larger than 5MB.</p>';
$ok = 0;
}
}
else
{
$error .= '<p class="error">No image was selected for upload.</p>';
$ok = 0;
}
if(empty($error) && $ok == 1)
{
if(move_uploaded_file($_FILES['file']['tmp_name'], $target_file))
{
$date = date('m-d-Y');
$stmt = $db->prepare('INSERT INTO gallery_photos (photo_filename,photo_caption,photo_category,postdate) VALUES (?,?,?,STR_TO_DATE(?, "%m-%d-%Y"))');
if($stmt)
{
$stmt->bind_param('ssss',$_FILES['file']['name'],$caption,$category,$date);
if($stmt->execute())
{
$success .= '<p class="success">File successfully uploaded to the gallery.</p>';
}
else
{
$error .= '<p class="error">Error code 89. Please contact the site administrator.</p>';
}
}
else
{
$error .= '<p class="error">Error code 86. Please contact the site administrator.</p>';
}
}
else
{
$error .= '<p class="error">An error occured while uploading your file.</p>';
}
}
}
?>
<div id="form">
<form action="" method="post" enctype="multipart/form-data" name="upload_form">
<table cellspacing="2" cellpadding="2" width="500">
<tr><th colspan="2">Upload Image</th></tr>
<tr><td colspan="2">
<?php
if($error)
{
echo $error;
}
if($success)
{
echo $success;
}
if($caption)
{
echo $caption;
}
?>
<p>Only PNG files are allowed.</p>
</td></tr>
<tr>
<td align="right"><label for="gallery">Gallery</label></td>
<td>
<select name="gallery">
<option value="">Select One...</option>
<?php
$result = $db->query('SELECT * FROM gallery_category');
if(is_object($result) && $result->num_rows > 0)
{
while($row = $result->fetch_array())
{
echo '<option value="'.$row['category_id'].'">'.$row['category_name'].'</option>';
}
}
?>
</select>
</td>
</tr>
<tr>
<td align="right"><label for="file">Image</label></td>
<td><input type="file" name="file" /></td>
</tr>
<tr>
<td align="right"><label for="caption">Caption</label></td>
<td><input type="text" name="caption" /></td>
</tr>
<tr><td align="center" colspan="2"><input type="submit" name="submit" value="Upload Image"</td></tr>
</table>
</form>
</div>
Any help at identifying this problem would be greatly appreciated, as I cannot seem to locate it. No errors are thrown whatsoever in my logs, or on the page in question, and it is inserted into the database with no errors, and the image is uploaded with no problems.