Jump to content

How to get ID when showing name in Dropdown?


stirrah
Go to solution Solved by Ch0cu3r,

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>
Link to comment
Share on other sites

  • Solution

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.

Edited by Ch0cu3r
Link to comment
Share on other sites

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>
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Edited by Ch0cu3r
Link to comment
Share on other sites

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.

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.