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!

PHP dropdown issue?

Status
Not open for further replies.

7724

Programmer
Feb 25, 2005
28
GB
Hi Guys,

Have a problem with a dropdown, which takes data from one table in MySQL database called ‘uploads’ and saves it in another called ‘news’ when I select it from the dropdown and hit submit:

Here’s all the code:


Code:
<?php

// login stuff

include("../something.php");
include("../somethingelse.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 picturemenuchoice='$picture' 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";
}

?>
<html>
<head>
<title>Admin</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="stylesheet" href="../ssheet.css">
</head>

<body>
<div align="center">
  <form name="form1" method="post" action="">
    <table width="100%" align="center" cellpadding="1" cellspacing="1">
      <tr bgcolor="#CC0000"> 
        <td colspan="3"><span class="yellow_bold">ADD NEWS:</span> <span class="white_bold">Please 
          Add Full Details</span></td>
      </tr>
      <tr> 
        <td colspan="3"> 
          <p>&nbsp;</p>
        </td>
      </tr>
      <tr> 
        <td colspan="3">&nbsp;</td>
      </tr>
      <tr> 
        <td colspan="3" bgcolor="#FFFF00"><?php echo $msg?></td>
      </tr>
      <tr> 
        <td colspan="3">&nbsp;</td>
      </tr>
      <tr> 
        <td colspan="3"></td>
      </tr>
      <tr> 
        <td colspan="3" valign="top"> 
          <select name="picturemenuchoice">
            <option value="" selected>select an image...</option>
            <?php 
              $query = mysql_query("SELECT * FROM `uploads`"); 
              while($picture = mysql_fetch_assoc($query)){ 
            ?> 
            <?php echo $_POST['picturemenuchoice']; ?> <? } ?></option>
            <option value="<?php echo $picture['url']; ?>"><?php 
            echo $picture['name']; ?></option>
            <?php 
            } 
            ?> 
          </select>
          <p align="right"><br>
            <input type="submit" name="Submit" value="Submit" class="button">
            <input type="reset" name="Reset" value="Reset" class="button">
          </p>
        </td>
      </tr>
    </table>
  </form>
</div>
</body>
</html>

Now even though the dropdown does save the data selected from the dropdown table ‘uploads’ into the news table I still have two problems:

1) With the dropdown it has a blank space in between each value (see screenshot below)

dropdown.jpg


2) As you will see the same page also GET’s the data from the MySQL database so I can edit it, so how do I get the dropdown to see that if I want to GET and edit for a particular record it shows the value I saved in my ‘news’ table for that record in the dropdown? As when I currently GET the dropdown, just displays blank.

Many Thanks

Cbo
 
For your first problem, the offending part looks to be outputting a closing option tag while no option tag was open. Your syntax of context switching all the time (moving in and out of php mode) is very confusing to follow and I was unsure what ended where.

As for the second question, you need to output [tt]selected="selected"[/tt] in the correct <option> tag. While you're looping through the options, you need to check if the value matches the one in GET. If it does, put the selected part in that option tag.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top