3

I am trying design a form which involves a drop down list. I would like to edit the form by changing the value of the variables in the form. I have retrieved all the values from mysql DB, but when I click the submit button without changing the value in the dropdown list. The value will be set to null.

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'];
}}

This is my dropdown list:

<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>

What I want is when I click the submit button without changing the content of the post, the $post_type1 will not be set to null, but keep its original value from DB.

Thanks, hope I have stated my question clearly:)

Here's the code that builds the db query :

$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'";
Armel Larcier
  • 15,747
  • 7
  • 68
  • 89
Xiahao Wang
  • 45
  • 1
  • 3
  • 10

2 Answers2

1

Best way to do it would be have a default value of the drop down to be like -1 .e.g

HTML

<select name="category">

    <option id="-1">Please Select</option>
    <option id="1">Category 1</option>
    <option id="2">Category 2</option>

</select>

PHP

<?php

$post_type1 = ( 0 < ( int ) $_POST[ 'category' ] ) ? $_POST[ 'category' ] : null;

Edit : Sorry, misread the question

This is probably more what you want :

PHP

<?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>";

Hope that helps

James Lockhart
  • 1,030
  • 11
  • 17
0

In HTML you should set a default value for the dropdown.

You write a select element with options. The current database value's option element should have the boolean selected attribute set.

How can I set the default value for an HTML <select> element?

<select name="category" id="category">
    <option></option>
    <option></option>
    <option selected="selected">

    </option>
    <option></option>
</select>

This way $post_type1 will hold the selected value if you click submit without modifying the form.

Now for the image. you have to test values to build the right mySQL query:

$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'";

You get the gist of it!?

Community
  • 1
  • 1
Armel Larcier
  • 15,747
  • 7
  • 68
  • 89
  • That's exactly the element I was looking for, Thanks! – Xiahao Wang Dec 15 '13 at 14:01
  • sry , new to this. I have accepted your ans.By the way, do you know how to do the same for editing a post that has an image in it? – Xiahao Wang Dec 15 '13 at 14:50
  • What input type? type="file"? I fyour editing items from a database's table, I guess your form contains an input type="hidden" that represents the entity row's primary key value. In PHP, if $_FILES['image'] is not set (!isset($_FILES || !isset($_FILES['image']) then don't update the 'image' column. The problem might come from the sql request you're running. UPDATE? REPLACE INTO? Please post more code!? – Armel Larcier Dec 15 '13 at 14:53
  • this is the code for uploading images. However, when I submit the edited form without uploading a new image, the original image will be removed. – Xiahao Wang Dec 15 '13 at 15:06
  • That must be because of the way you construct you mySQL query. Edit your question with more code plz. – Armel Larcier Dec 15 '13 at 15:09
  • hi I have added in the sql query:) – Xiahao Wang Dec 15 '13 at 15:18
  • ya it's in the question, the code starts from $post_image1 = $_FILES['image']['name']; – Xiahao Wang Dec 15 '13 at 15:24
  • coz I just put it there. You had edited my answer. Anyhow, I edited my answer. You should be okay. – Armel Larcier Dec 15 '13 at 15:25
  • thank you for your ans. I wish I can give you more pts:) – Xiahao Wang Dec 15 '13 at 15:30
  • Did it help? You can extend the principle to build a cleaner query with less parameters. You can't +1? – Armel Larcier Dec 15 '13 at 15:34