PHP在编辑时更新表单下拉列表中选择的内容
我正在尝试设计一个包含下拉列表的表单。 我想通过更改表单中变量的值来编辑表单。 我已经从mysql数据库中检索到了所有值,但是当我单击submit按钮而不更改下拉列表中的值时。该值将设置为nullPHP在编辑时更新表单下拉列表中选择的内容,php,edit,html-select,Php,Edit,Html Select,我正在尝试设计一个包含下拉列表的表单。 我想通过更改表单中变量的值来编辑表单。 我已经从mysql数据库中检索到了所有值,但是当我单击submit按钮而不更改下拉列表中的值时。该值将设置为null if(isset($_POST['update'])){ $update_id = $_GET['edit_form']; $post_title1 = $_POST['title']; $post_date1 = date("Y-m-d"); $post_auth
if(isset($_POST['update'])){
$update_id = $_GET['edit_form'];
$post_title1 = $_POST['title'];
$post_date1 = date("Y-m-d");
$post_author1 = $_POST['author'];
$post_keywords1 = $_POST['keywords'];
$post_type1 = $_POST['category']; // This is the content I want to update but was set to null, the rest is fine.
$post_content1 = $_POST['content'];
$post_image1 = $_FILES['image']['name'];
$image_tmp = $_FILES['image']['tmp_name'];
}}
这是我的下拉列表:
<html>
<select name="category" class="err">`enter code here`
<option value=""><?php echo $post_type;?></option>
<option value="Class">Class</option>
<option value="Facilities">Facilities</option>
<option value="Services">Services</option>
</select>
在HTML中,应该为下拉列表设置默认值 您可以编写一个带有选项的select元素。 当前数据库值的option元素应设置布尔值
selected
属性
你明白要点了 最好的方法是将下拉列表的默认值设置为-1 HTML
请选择
第一类
第2类
PHP
非常感谢,我想我想要的远比这个简单:)如果你想让提问者接受答案,你应该+1这个问题。这正是我想要的元素,谢谢!sry,这是新来的。我已经接受了你的回复。顺便问一下,你知道如何编辑一篇有图片的文章吗?什么输入类型?type=“文件”?如果从数据库的表中编辑项目,我猜您的表单包含一个表示实体行主键值的输入type=“hidden”。在PHP中,如果未设置$_FILES['image'](!isset($_FILES | |!isset($_FILES['image']),则不更新“image”列。问题可能来自正在运行的sql请求。更新?替换为?请发布更多代码!?“width=”100“height=”100“>这是上传图像的代码。但是,当我提交编辑后的表单而不上传新图像时,原始图像将被删除。这一定是因为您构建mySQL查询的方式。请使用更多代码编辑您的问题。”。
$post_image1 = $_FILES['image']['name'];
move_uploaded_file($image_tmp,"../images/$post_image1");
$update_query = "UPDATE post SET post_title='$post_title1', post_date='$post_date1', post_author='$post_author1', post_image='$post_image1', post_keywords='$post_keywords1',post_type='$post_type1',
post_content='$post_content1' WHERE post_id ='$update_id'";
<select name="category" id="category">
<option></option>
<option></option>
<option selected="selected">
</option>
<option></option>
</select>
$update_query = "UPDATE post SET post_title='$post_title1', post_date='$post_date1', post_author='$post_author1'";
$post_image1 = isset($_FILES['image']) ? $_FILES['image']['name'] : false;
if($post_image1 !== false){
if(move_uploaded_file($image_tmp,"../images/$post_image1")){
//file successfully moved
$update_query .= " , post_image='$post_image1'";
}
}
$update_query .= ", post_keywords='$post_keywords1',post_type='$post_type1',
post_content='$post_content1' WHERE post_id ='$update_id'";
<select name="category">
<option id="-1">Please Select</option>
<option id="1">Category 1</option>
<option id="2">Category 2</option>
</select>
<?php
$post_type1 = ( 0 < ( int ) $_POST[ 'category' ] ) ? $_POST[ 'category' ] : null;
<?php
$mysqli = new mysqli( "localhost", "xxx", "xxx", "xxx" );
$query = "SELECT * FROM xxx";
$options = array();
$category = ( int ) $_POST[ 'category' ];
$query = $mysqli->query( $query );
if( FALSE !== $query )
{
while( $row = $mysliResults->fetch_assoc() )
{
$selected = "";
if( $category === ( int ) $row['id'] )
$selected = " selected=\"selected\"";
$options []= "<option id=\"{$row['id']}\"{$selected}>{$row['name']}</option>":
}
}
echo "<select name=\"category\">", implode( "\n\t", $options ), "</select>";