我已获得此上传表单,并希望保留会话中下拉列表中的所选选项,以便在提交后显示最后一个选项,例如我选择颜色'提交颜色后仍然在下拉列表中选择。 我回复了$ _SESSION [' testname'](就在第一个单选按钮之前),它让我回到"颜色",但是在我喜欢的选项标签中回声'选择'如果"颜色"是最后一个选择,它什么都不返回!我错过了什么?
<?php
session_start();
if(isset($_POST['kategorie'])) {
$_SESSION['testname']=$_POST['kategorie']; }
?>
<?php
$con = mysqli_connect("localhost","Melvin","") or die ("could not connect to server: " . mysqli_connect_error($con));
mysqli_select_db($con, "galerie") or die ("Could not connect to database: " . mysqli_error($con));
if(isset($_POST['submit'])){
$name = $_FILES['file']['name'];
$sub_name = substr($name, 0, -4);
$img_ext = ".jpg";
$tmp_name = $_FILES['file']['tmp_name'];
$location = '_images/_galerie/';
$target = '_images/_galerie/' .$name;
if(move_uploaded_file($tmp_name,$location.$name)){
echo "file uploaded";
$nam = $_POST['nam'];
$kategorie = $_POST['kategorie'];
$size = $_POST['size'];
if ($size == 'thumb') {
// add "thumb" between filename and extension
$extension_pos = strrpos($target, '.'); // find position of the last dot, so where the extension starts
$thumb = substr($target, 0, $extension_pos) . '_thumb' . substr($target, $extension_pos);
$query = mysqli_query($con , "INSERT INTO images(img_name,img_title,img_cat,img_size)VALUES('".$thumb."','$nam','$kategorie','$size')");
} else {
$query = mysqli_query($con , "INSERT INTO images(img_name,img_title,img_cat,img_size)VALUES('".$target."','$nam','$kategorie','$size')");
}
function renameImg() {
$name = $_FILES['file']['name'];
$target = '_images/_galerie/' .$name;
$extension_pos = strrpos($target, '.');
$thumb = substr($target, 0, $extension_pos) . '_thumb' . substr($target, $extension_pos);
rename($target, $thumb);
//echo $name . " replaced with " . $thumb;
};
renameImg();
} else {
echo "file not uploaded";
}
}
?>
<div style="margin:20px 0 40px 0;">
<form action="upload.php" method="POST" enctype="multipart/form-data">
Upload: <input type="file" name="file">
Title: <input type="text" name="nam" value="Tattoo Gallery">
Category: <select name="kategorie" id="selectKat">
<option value="black" <?php if(isset($_SESSION['kategorie']) == "black") { echo ' selected';} ?>>Black and white</option>
<option value="colour" <?php if(isset($_SESSION['kategorie']) == "colour") { echo ' selected';} ?>>Colour</option>
</select>
<br>
<?php
echo $_SESSION['testname'];
?>
<input type="radio" name="size" value="full" id="regularRadio" checked="checked">
<label for="regularRadio">Full size</label>
<br>
<input type="radio" name="size" value="thumb" id="thumbRadio">
<label for="thumbRadio">Thumbnail</label>
<br>
<input type="submit" name="submit">
</form>
</div>
<?php
$result = mysqli_query($con, "SELECT * FROM images WHERE img_size='thumb'");
while($row = mysqli_fetch_array($result)){
echo "<img src=".$row['img_name'] . " class='thumbnails' style='display:inline;float:left;'>";
}
?>
&#13;
答案 0 :(得分:1)
您不能使用isset()函数。此函数返回True或False。
您只需要将$ _POST [&#39; kategorie&#39;]或$ _SESSION [&#39; testname&#39;](如我所见)的值与您的文字进行比较(&#34;颜色& #34;或&#34;黑&#34;),像这样:
if ($_SESSION['kategorie'] == "black") { echo ' selected'; }