Page 1 of 2

rename values in dropdown box

Posted: Mon Apr 27, 2009 1:21 am
by angelic_devil
ok this is wat i m trying to do i have a drop down box with list populated from the database.

i have a text box and a rename button

when i select a particular value in the drop down box and enter a name in the textbox and then click on rename button ...it should rename the selected namefrom dropdown to the name entered in textbox

but its not doing so in my script
i m dead sure its something in the update command line which i m using... plz help

Code: Select all

 
 
 
<?php
 
$genre_name = $_POST['genre'];
$genrename = 'genrename';
$genrevalue = array();
$genre_select = $_POST['genrename']; 
 
$query_name = " SELECT image_category.genre FROM image_category ";
    $result = mysql_query($query_name);
    confirm_query($result);
    
       
?>
<?
 
 //-----------Rename genre in database ---------------------
 if (isset($_POST['rename_genre'])) {
 $query = "UPDATE image_category 
           SET image_category.genre = '$genre_name'
           WHERE image_category.genre = '". mysql_real_escape_string($genre_select) ."'";
$result1 = mysql_query($query, $connection);
echo $genre_select;
echo $genre_name;
if ($result1) {
             $message = "new genre has been renamed";
             
         }
         else {
          $message = "new genre could not be renamed";
          }
}
 
 
 
 ?>
 
<td id="page">
<?php if (!empty($message)) {echo "<p class=\"message\">" . $message . "</p>";} ?>
 
<form action="rename_genre.php" method="post">
<table>
 
<div style="position: absolute; width: 100px; height: 32px; z-index: 1; left: 225px; top: 5px" id="layer1">
<td><br>
<select name="genrename" maxlength="30">
<?php
        //-----------displays genre from database ---------------------
            while ($record = mysql_fetch_assoc($result)) {
        while (list($genrename, $genrevalue) = each ($record)) {
        echo '<option>'.htmlentities($genrevalue).'</option>';
       
        }
        echo "<BR>";
               
    }   
               
   
?> 
 </select> </td>
<tr>
                    <td>Genre</td>
                    <td><input type="text" name="genre" maxlength="20" value="<?php echo htmlentities($genre_name); ?>" /></td>
                </tr>
 
<tr>
                    <td colspan="2"><input type="submit" name="rename_genre" value="rename_genre" /></td>
                </tr>
                
            </table>
 
</form>
</td>
 
 
 
 
</body>
 

Re: rename values in dropdown box

Posted: Mon Apr 27, 2009 7:16 am
by Yossarian
do:

var_dump($_POST);

at the top of your script, I cannot see how you are passing along which option was selected because there is no value associated with each <option value="x">

Re: rename values in dropdown box

Posted: Mon Apr 27, 2009 8:23 pm
by angelic_devil
do mean like this...but its still not giving me the desired result...plz tell me which line r messed up and what i need to change in them

Code: Select all

 
 
 
<?php
 
$genre_name = $_POST['genre'];
$genrename = 'genrename';
$genrevalue = array();
$genre_value = $_POST['genrename'];
 
$query_name = " SELECT image_category.genre FROM image_category ";
    $result = mysql_query($query_name);
    confirm_query($result);
    $genrename_list[]=" ";
  
  while ($record = mysql_fetch_assoc($result)) {
          $genrename_list[] = $record['genre'] ;
              
        }
       
?>
<?
 
 //-----------Rename genre in database ---------------------
 if (isset($_POST['rename_genre'])) {
 $query = "UPDATE image_category 
           SET image_category.genre = '$genre_name'
           WHERE image_category.genre = '{$genrename_list[$genre_value]'";
$result1 = mysql_query($query, $connection);
 
if ($result1) {
            $message = "new genre has been renamed";
             
        }
         else {
         $message = "new genre could not be renamed";
          }
}
  
 ?>
 
<td id="page">
 
<form action="genre_rename.php" method="post">
<table>
 
<td><br>
<select name="genrename" maxlength="30">
 
        //-----------displays genre from database ---------------------
        
        <?php
       
        // Loops through the $status_list array
         foreach ($genrename_list as $value => $option)
        {
            // Sets an attribute to show the chosen status as selected
            $selected = ($genre_value == $value) ? ' selected="selected"' : '';
           //$status = (isset($_get['status'])) ? $_get['status'] : "";
           
            // Builds an option for each acceptable status
           echo '<option value="'.$value.'"'.$selected.'>'.$option.'</option>';
           // echo '<option value="'.$value.'"'.$status.'>'.$option.'</option>';
        }
        ?>
   
 
 </select> </td>
<tr>
                    <td>Genre</td>
                    <td><input type="text" name="genre" maxlength="20" value="<?php echo htmlentities($genre_name); ?>" /></td>
                </tr>
 
<tr>
                    <td colspan="2"><input type="submit" name="rename_genre" value="rename_genre" /></td>
                </tr>
                
            </table>
 
</form>
</td>
 
 
 
 
</body> 
 
 

Re: rename values in dropdown box

Posted: Tue Apr 28, 2009 4:47 am
by Yossarian
The questions you need to ask yourself, in order, are these.

1. Is my form correctly constructed? (did my PHP create the correct HTML?)
(A. validate the HTML, or at a minimum, look closely at the source code)

2. When I fill in the form, what does PHP understood I sent it?
(A. use var_dump($_POST) at the top of the script, and look at what PHP is passing round for you. Use var_dump() on PHP variables to see what is going on under the bonnet)

3. Exactly what is the sql statement made of, is it valid and does it work? Does my database actually contain some data which matches my requirement? (did my PHP variables all get stuck together correctly to make the sql statement?)
(A. Echo out the sql statement onto the screen, paste it into PhpMyAdmin and check it actually works)
echo $query; // in your case

Turn on error_reporting for the lifetime of your development work - NOT when your site is live.

Code: Select all

 
<?php
// first lines of your script
ini_set( 'display_errors', 1);
error_reporting( E_ALL );
?>
 
Do you see how hard it is to work out what is going on?

You have to first of all recognise that you are using one language (PHP) to create at least 2 other languages, HTML, SQL (ok, ones a markup).

This will only get more complex as you add JS, and maybe regex's and other stuff.

Logically eliminating possibilities is how I worked out how to approach debugging scripts, I hope it works for you too.

Divide and conquer.

Re: rename values in dropdown box

Posted: Tue Apr 28, 2009 5:02 am
by parimala
Yossarian way of giving solution is extraordinary.

while dealing wih php and javascript it's always a best p rocedure to come one by one step.

first clicking on the button then submitting the form
take the post value update the datbase and dynamically replace selectbox values finish
your task get completed.
Yossarian wrote:The questions you need to ask yourself, in order, are these.

1. Is my form correctly constructed? (did my PHP create the correct HTML?)
(A. validate the HTML, or at a minimum, look closely at the source code)

2. When I fill in the form, what does PHP understood I sent it?
(A. use var_dump($_POST) at the top of the script, and look at what PHP is passing round for you. Use var_dump() on PHP variables to see what is going on under the bonnet)

3. Exactly what is the sql statement made of, is it valid and does it work? Does my database actually contain some data which matches my requirement? (did my PHP variables all get stuck together correctly to make the sql statement?)
(A. Echo out the sql statement onto the screen, paste it into PhpMyAdmin and check it actually works)
echo $query; // in your case

Turn on error_reporting for the lifetime of your development work - NOT when your site is live.

<?php
// first lines of your script
ini_set( 'display_errors', 1);
error_reporting( E_ALL );
?>

Do you see how hard it is to work out what is going on?

You have to first of all recognise that you are using one language (PHP) to create at least 2 other languages, HTML, SQL (ok, ones a markup).

This will only get more complex as you add JS, and maybe regex's and other stuff.

Logically eliminating possibilities is how I worked out how to approach debugging scripts, I hope it works for you too.

Divide and conquer.

Re: rename values in dropdown box

Posted: Tue Apr 28, 2009 5:30 am
by user___
I agree with the latest posts, divide and conquer(In that case step by step) and you will find where your code breaks.

Re: rename values in dropdown box

Posted: Tue Apr 28, 2009 5:50 am
by angelic_devil
see i was able to get the data successfuly into the listbox but its not replaceing the seelcted value in the listbox with the value of text when i click on submit button

Re: rename values in dropdown box

Posted: Tue Apr 28, 2009 6:17 am
by user___
So your update statement does not work.

Code: Select all

 
 $query = "UPDATE image_category
               SET image_category.genre = '$genre_name'
              WHERE image_category.genre = '{$genrename_list[$genre_value]'";
echo $query;
$result1 = mysql_query($query, $connection) or die(mysql_error());
 
and tell us the result.

Re: rename values in dropdown box

Posted: Sun May 03, 2009 4:01 am
by angelic_devil
its not doing anything just showing the same page back again.

Re: rename values in dropdown box

Posted: Mon May 04, 2009 4:14 am
by angelic_devil
can anyone solve this issue?

Re: rename values in dropdown box

Posted: Mon May 04, 2009 12:33 pm
by McInfo
Change line 29

Code: Select all

$result1 = mysql_query($query, $connection);
to

Code: Select all

$result1 = mysql_query($query);
If you are able to populate the select box with data, that means you must have used mysql_connect() somewhere, but that is not shown in your code. Since there is a connection and line 29 fails, that means that $connection is not the variable that holds the link identifier. Removing $connection from the list of arguments allows mysql_query() to use whatever link resource is available.

If you had error reporting on, you would have seen an error:
Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource
Edit: This post was recovered from search engine cache.

Re: rename values in dropdown box

Posted: Mon May 04, 2009 8:02 pm
by angelic_devil
its not doing anything just reloading the same page again....also it displays no error message..here is the full code including the includes :

Code: Select all

 
 
<?php require_once("d:/wamp/www/php_stockphotos/includes/session.php"); ?>
<?php require_once("d:/wamp/www/php_stockphotos/includes/connection.php"); ?>
 <?php require_once("d:/wamp/www/php_stockphotos/includes/constants.php"); ?>
<?php
 
$genre_name = $_POST['genre'];
$genrename = 'genrename';
$genrevalue = array();
$genrename_list = array();
$genre_value = $_POST['genrename'];
 
$query_name = " SELECT image_category.genre FROM image_category ";
    $result = mysql_query($query_name);
    confirm_query($result);
    $genrename_list[]=" ";
 
  while ($record = mysql_fetch_assoc($result)) {
          $genrename_list[] = $record['genre'] ;
             
        }
       
?>
<?
 
 //-----------Rename genre in database ---------------------
 if (isset($_POST['rename_genre'])) {
 
 echo $genre_name;
 echo $genrename_list[$genre_value];
 if  ($genre_name!=""){
 $query = "UPDATE image_category
          SET image_category.genre = "$genre_name"
          WHERE image_category.genre = '{$genrename_list[$genre_value]'";
$result1 = mysql_query($query);
 
if ($result1) {
            $message = "new genre has been renamed";
             
        }
         else {
         $message = "new genre could not be renamed";
          }
}
 }
 
 else 
 { $message = " You have left the text box empty....enter valid name to rename the selected ";}
 
 ?> 
 
<td = "page"> 
<form action="rename.php" method="post">
<table>
 
<td><br>
<select name="genrename" maxlength="30">
 
        //-----------displays genre from database ---------------------
       
        <?php
       
      
         foreach ($genrename_list as $value => $option)
        {
            
            $selected = ($genre_value == $value) ? ' selected="selected"' : '';
                      
           
           echo '<option value="'.$value.'"'.$selected.'>'.$option.'</option>';
           
        }
        ?>
   
 
 </select> </td>
<tr>
                    <td>Genre</td>
                    <td><input type="text" name="genre" maxlength="20" value="<?php echo htmlentities($genre_name); ?>" /></td>
                </tr>
 
<tr>
                    <td colspan="2"><input type="submit" name="rename_genre" value="rename_genre" /></td>
                </tr>
               
            </table>
 
</form>
 
</td> 
 
 
 
</body> 
 
 
 

Re: rename values in dropdown box

Posted: Mon May 04, 2009 8:31 pm
by McInfo
Post the code for session.php and connection.php too.

Edit: This post was recovered from search engine cache.

Re: rename values in dropdown box

Posted: Mon May 04, 2009 8:53 pm
by angelic_devil
session.php

Code: Select all

 
<?php
    session_start();
    
    function logged_in() {
        return isset($_SESSION['username']);
    }
    
    function confirm_logged_in() {
        if (!logged_in()) {
            redirect_to("login.php");
        }
    }
?>
 
 

connection.php

Code: Select all

 
<?php
require("constants.php");
 
// 1. Create a database connection
$connection = mysql_connect(DB_SERVER,DB_USER,DB_PASS);
if (!$connection) {
    die("Database connection failed: " . mysql_error());
}
 
// 2. Select a database to use 
$db_select = mysql_select_db(DB_NAME,$connection);
if (!$db_select) {
    die("Database selection failed: " . mysql_error());
}
?>
 
 
constants.php

Code: Select all

 
define("DB_SERVER", "localhost");
define("DB_USER", "lucky");
define("DB_PASS", "lucky");
define("DB_NAME", "stockphotos");
 
 

Re: rename values in dropdown box

Posted: Mon May 04, 2009 9:02 pm
by McInfo
You need to either go through your code line-by-line and clean it up or start over.

Are you still using EditPad? Maybe you should look for a different editor. Eclipse PDT will show you where the errors are in your code.

The following apply to the main script: There are unescaped quotes on line 34 and a missing curly brace on line 35. There is a fragmented HTML attribute on line 53. There is an unmatched </body> tag on line 95. There are <td> tags that are not inside a table. These are things that Eclipse would catch. One problem that Eclipse would not catch is that neither the PHP nor the HTML is consistently indented.

Edit: This post was recovered from search engine cache.