Jump to content

How to get ID when showing name in Dropdown?


stirrah

Recommended Posts

Hi,

 

I got a dropdown and a textbox. The dropdown is getting data from the database and showing the names.

Now i want to save that names ID and send it to the database. How can i do this? 

Do i have to make another query something like this? Or can i make it some other (better) way, Maybe this is a question for the SQL-forum?

$categoryId = mysql_query("SELECT categoryId FROM `projectCategory` WHERE categoryName = '" . mysql_real_escape_string($_POST['categoryName']));

Here is my code:

<?php
if (isset($_GET['add_h_success']) && empty($_GET['add_h_success'])) {
	echo 'Projekt tillagt.';
} else {
if (empty($_POST) === false && empty($errors) === true) {
	add_hproject($_POST['huvudProjectName'], $categoryId);
	header('Location: add_hproject.php?add_h_success');
} else if (empty($errors) === false) {
	echo output_errors($errors);
}
?>

<?php
$options = '';
$category=mysql_query("SELECT categoryName, categoryId FROM `projectCategory`");
while($row = mysql_fetch_array($category)) {
    $options .="<option>" . $row['categoryName'] . "</option>";
}



?>


<form action="" method="POST">
	<ul>
		<li>
			<?php $menu="
    <select name='categoryId'>
      " . $options . "
    </select>
</form>";

echo $menu; ?>
		</li>
		<li>Namn: <br>
			<input type="text" name="huvudProjectName">
		</li>
		<li>
			<input type="submit" value="Lägg till"></li>
	</ul>
</form>

Get the category id and name from the database when you make the options list and set the id as the value for the options

$category=mysql_query("SELECT categoryName, categoryId FROM `projectCategory`");
while($row = mysql_fetch_array($category)) {
    $options .="<option value=\"".$row['categoryId']."\">" . $row['categoryName'] . "</option>";
}

When the form is submitted $_POST['categoryId''] will contain the category id for the selected category.

Ah! Smart! 


 


My problem now is that i cant submit the value in the dropdown...why? 


 


my code looks like this right now:



<?php
if (isset($_GET['add_h_success']) && empty($_GET['add_h_success'])) {
echo 'Projekt tillagt.';
echo $cat;
} else {
if (empty($_POST) === false && empty($errors) === true) {
$cat = $_POST['categoryName'];
$categoryId = mysql_query("SELECT categoryId FROM `projectCategory` WHERE categoryName = $cat");
add_hproject($_POST['huvudProjectName'], $categoryId);
header('Location: add_hproject.php?add_h_success');
} else if (empty($errors) === false) {
echo output_errors($errors);
}
?>

<?php
$options = '';
$category=mysql_query("SELECT categoryName, categoryId FROM `projectCategory`");
while($row = mysql_fetch_array($category)) {
$options .="<option>" . $row['categoryName'] . "</option>";
}



?>


<form action="" method="POST">
<ul>
<li>
<?php $menu="
<select name='categoryName'>
" . $options . "
</select>
";

echo $menu; ?>
</li>
<li>Namn: <br>
<input type="text" name="huvudProjectName">
</li>
<li>
<input type="submit" value="Lägg till"></li>
</ul>
</form>

And, what do you mean - exactly - that you cannot submit. Are you saying you are clicking the submit button and nothing happens. Or that you click it and it submits - but you don't see the results you expect. We are working in the blind and depend upon you to provide all the information necessary to help you.

Don't know why you have change your code from what you had. All you needed to do is replace the while loop with my code and then pass $_POST['categoryId'] variable to the add_hproject() function.

 

The problem with your code above is with the query. Values used in queries need to be wrapped in quotes

$categoryId = mysql_query("SELECT categoryId FROM `projectCategory` WHERE categoryName = '$cat'");

 

And mysql_query does not return the categoryId only the result resource. You need to use one of the mysql_fetch_* functions or mysql_result to get the categoryId value from the query.

And, what do you mean - exactly - that you cannot submit. Are you saying you are clicking the submit button and nothing happens. Or that you click it and it submits - but you don't see the results you expect. We are working in the blind and depend upon you to provide all the information necessary to help you.

 

Sorry! I mean that the $_POST never gets the value.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.