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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Php Option Dropdown

Status
Not open for further replies.

7724

Programmer
Feb 25, 2005
28
GB
Hi Guys,

I’m a bit stuck working on a PHP content management system that assigns a photograph with each profile I create.

Now I have my php page set-up so you can enter a variable called $firstname, and this data entered into the textbox is saved in MySQL database in a table called ‘profiles’ when you hit submit - (and it can be retrieved and modified) as shown below:

Code:
<?php

// login stuff

include("../ch.php");
include("../cm.php");
checklogin();

$firstname = "";
$image = "";

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

	$firstname = $_POST['firstname'];
	$image = $_POST['image'];
	
	if(!isset($_GET['profilesid']))
	{
		$result = mysql_query("Insert into profiles(firstname,image) values('$firstname','$image')");
		$msg = "New record is saved";
	}
	else
	{
		$result = mysql_query("Update profiles set firstname='$firstname', image='$image' where profilesid=".$_GET['profilesid']);
		$msg = "profiles Record is updated";
	}
}
if(isset($_GET['profilesid']))
{
	$result = mysql_query("Select * From profiles where profilesid=".$_GET['profilesid'],$link);
	$row = mysql_fetch_array($result, MYSQL_BOTH);
	$firstname = $row['firstname'];
	$image = $row['image'];
}
?>
<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"></div>
<form name="form1" method="post" action="">
  <input name="firstname" type="text" id="firstname" value="<?php echo $firstname?>" size="36">
</form>
</body>
</html>

Now as you can see I also have an $image variable on this page. What I want to do is create an <option></option> drop down menu, which looks at a different table in MySQL database called ‘uploads’ which holds information on .jpg images uploaded to a folder.

Basically each .jpg uploaded has $name and $url data assigned to it in the ‘uploads’ table..

So I want the <option></option> drop down menu to do is:

- Return all $name of uploaded images in the dropdown
- Hit Submit
- The $URL to that image I have selected is held in the ‘profiles’ table
held in the $image variable.

But I have not got a clue how to do it – anybody help?

Thanks

Chris
 
Write a function that checks if there are any images in the table for the specified ID.
Steps:
1. Write SQL to get image meta data from table (e.g. "SELECT * FROM images WHERE profilesid ='".$_POST['profilesid'].'"")
2. Extecute query and inspect how many matches are returned (mysql_num_rows());
3. If matches > 0 assemble the HTML into a variable. Start with $dropdown = '<select name="whatever">';
4. loop through the result set in a while loop (while $row=mysql_fetch_assoc($result)).
5. Concatenate the option tags to the variable $dropdown
$dropdown .= '<option>'.$row['name'].'</option>';
6. when the loop ends, close the <select> tag with </select>
7. If $dropdown is not empty, print it into the HTML at the place where you want it.

That's it.
 
Thanks - not being a php expert I'm not sure how to implement the babove. I have this code, which I think I am NEARLY there with:

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

It shows the image names, but when I hit submit (forgot to include the button in my earlier code!) it just jumps back to 'select an image...' - not storing the URL value in my 'profiles' table.

Thanks
 
Hi 7724

Might I recommend some error-checking? While developing, I find it mightily handy to echo out sql statements that don't seem to be working properly. It is also handy for debugging to echo out any MySQL errors.

For example:
Code:
if(!isset($_GET['profilesid'])){
  $sql = "Insert into profiles(firstname,image) values('$firstname','$image')";
  $result = mysql_query($sql);
  if (mysql_error()){
    echo "Error on query: " . $sql . "<br>" . mysql_error();
  }
  else
    $msg = "New record is saved";
}
else{
  $sql = "Update profiles set firstname='$firstname', image='$image' where profilesid=".$_GET['profilesid'];
  $result = mysql_query($sql);
  if (mysql_error()){
    echo "Error on query: " . $sql . "<br>" . mysql_error();
  }
  else
    $msg = "profiles Record is updated";
}

The above error checking will notify you if you have an error, echo out the SQL statement that was problematic, and tell you what went wrong. I do this for all my queries, and it's saved hours of debugging trouble!
Just for fun, echo out the SQL statements before you execute them, to see if you can spot the problem.

Best of luck!

[cheers]
Cheers!
Laura
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top