Jump to content

Dropdownbox updating database field


RichyVn

Recommended Posts

I need some help with updating the database table with the correct selection from that box.

I have a dropdown box. This is actually a list from the images in a curtain directory.

here the code I used:

<?php
$dhandle = opendir('../images/Vessels/');
$files = array();
if ($dhandle) {
   // loop through all of the files
   while (false !== ($fname = readdir($dhandle))) {
      if (($fname != '.') && ($fname != '..') &&
          ($fname != basename($_SERVER['PHP_SELF']))) {
          $files[] = (is_dir( "./$fname" )) ? "(Dir) {$fname}" : $fname; }
            }
   closedir($dhandle);
}

 

So when inserting a new record I can use the dropdownbox to populate the correct field.

 

Database:

Id, Vesselname, Image, more, andmore

 

There is a section where an authorized user can upload an image to the directory.

This image will then be available in dropdown when the need to add a new vessel (in this case)

The problem that I have is, when I later what to update the vessel, I want that same dropdownbox

to be showing the correct imagename as "selected".

How can I do that?

 

This is what I have been expirementing with sofar:

 

<?php require_once('../Connections/bgf_db.php'); ?>
<?php
$dhandle = opendir('../images/Vessels/');
$files = array();
$selectedFile = "mare.jpg";
if ($dhandle) {
   // loop through all of the files
   while (false !== ($fname = readdir($dhandle))) {
      if (($fname != '.') && ($fname != '..') &&
          ($fname != basename($_SERVER['PHP_SELF']))) {
          $files[] = (is_dir( "./$fname" )) ? "(Dir) {$fname}" : $fname; }
      }
   closedir($dhandle);
}
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }
  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}
$MyFile_Recordset1 = "no-picture.jpg";
if (isset($selectedFile)) {
  $MyFile_Recordset1 = $selectedFile;
}
mysql_select_db($database_bgf_db, $bgf_db);
$query_Recordset1 = sprintf("SELECT vessels.Name, vessels.Image FROM vessels WHERE vessels.Image = %s", GetSQLValueString($MyFile_Recordset1, "text"));
$Recordset1 = mysql_query($query_Recordset1, $bgf_db) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
$selectedFile = $row_Recordset1['Image'];
?>
<select name="NameHere">
<option value="<?php echo $selectedFile?>" 
<?php if (!(strcmp($selectedFile, $row_Recordset1['Image']))) {echo "selected=\"selected\"";} ?>><?php echo $selectedFile ?>
</option>
<?php
// Now loop through the files, echoing out a new select option for each one
foreach( $files as $fname )
{
   echo "<option value=\"$fname\">{$fname}</option>\n";
}
echo "</select>\n";
?>

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.