[英]MySQL PHP not inserting all data into database
所以...這個問題正在此刻讓我掉頭發。
我的網站上有一個表單,允許用戶將圖像上傳到Gallery文件夾,並且工作正常。 它上傳文件,將其插入數據庫,然后我可以去畫廊並顯示它。
問題在於,出於某種原因,我無法將$ _POST ['caption']變量插入數據庫。 當您點擊“提交”時,它甚至無法捕獲。 所以現在我有幾張沒有標題的圖像,即使已在框中輸入了一張。 (請注意,我已經進行了檢查,以確保該字段不為空,並且在運行檢查時不會引發任何錯誤)。
這是我的php和form部分的代碼:
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>
識別此問題的任何幫助將不勝感激,因為我似乎找不到它。 我的日志或相關頁面中都不會引發任何錯誤,並且它沒有錯誤地插入到數據庫中,並且圖像上傳沒有問題。
問題來自您的檢查if($caption = '')
和if($gallery = '')
。 因為=
是賦值運算符,而不是比較。 它將把您的$caption
分配給''
並且期望空白標題的結果。 您應該更改為if($caption == '')
和if($gallery == '')
1)您分配了$ caption和$ gallery而不是進行檢查
if($caption = ''){ }
由於單個=,會將$ caption設置為'',並且不會對其進行檢查。 因此字幕將為空
你應該這樣檢查
if($caption == ''){ }
與==
也許你也應該嘗試
if($caption == NULL){ }
要么
if(empty($caption)){ }
2) $category = trim($_POST['gallery']);
我不確定您是否要這樣,也許您應該檢查一下
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.