Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Image Problem

Status
Not open for further replies.

7724

Programmer
Feb 25, 2005
28
GB
Hi Guys,

I am trying to select some data taken from one table called 'uploads' in MySQL database that is held in a dropdown menu and place that data in another table called 'news' in the MySQL database when I hit submit.

Code:
<select name="picturemenuchoice">
<option value="" selected>select an image...</option>
<?php 
  $query = mysql_query("SELECT * FROM `uploads`"); 
  while($picture = mysql_fetch_assoc($query)){ 
?> 
  <option value="<?php echo $picture['url']; ?>"><?php
  echo $picture['name']; ?></option>
    <?php 
    } 
    ?> 
</select>

Now this works a treat, but when I submit the data the name of the image does NOT stay on the name of the image I have just chosen, it justs goes back to 'select an image', and also it does not not show the image name I have chosen when I go in and edit the selected dropdown item and again shows 'select an image'.

Now I have this code:

Code:
<?php

// login stuff

include("nameoffile.php");
include("ddwsff.php");
checklogin();

$picture = "";

if(isset($_POST['Submit']))
{


    $picture = $_POST['picturemenuchoice'];
	
	if(!isset($_GET['newsid']))
	{
		$result = mysql_query("Insert into news(picture) values('$picture')");
		$msg = "New record is saved";
	}
	else
	{
		$result = mysql_query("Update news set picture='$picturemenuchoice' where newsid=".$_GET['newsid']);
		$msg = "News Record is updated";
	}
}
if(isset($_GET['newsid']))
{
	$result = mysql_query("Select * From news where newsid=".$_GET['newsid'],$link);
	$row = mysql_fetch_array($result, MYSQL_BOTH);
    $picture = $row['picturemenuchoice'];
}

if(mysql_error())
{
echo mysql_error() ."<br>\n";
}

?>

[code]

Which states what should happen when I submit.  have I got it wrong anywhere when it saves so that instead of showing 'select an image' it will show the image name I have selected from the dropdown when I hit submit?

Many Many Thanks

CB
 
I believe for what you're trying to do, you need to use the optional 'selected' in your option tag ie:
Code:
<select name="nextpage.php" method="post">
  <option value="picture1.jpg">picture 1</option>
  <option selected value="picture2.jpg">picture 2</option>
  <option value="picture3.jpg">picture 3</option>
</select>
 
It is just as MattNeeley says.
You need to output the appropriate HTML code to indicate which of the <option> tags is the selected one.
There is no code in your post that shows any generation of HTNL for output. In that code you would conditionally add the 'selected' attribute to the appropriate option element.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top