Forum Moderators: coopster

Message Too Old, No Replies

post ID from dropdown selection

how to post the ID

         

itsme85

1:59 pm on Aug 17, 2008 (gmt 0)

10+ Year Member



Hello,

I want to use the ID from the selected item and insert it into my join table.

This are my tables:

product
product_id
product_name

category
category_id
category_name

product_category
product_category_id
product_id
category_id

I use this to insert the data and make a selection.


<html>
<head>
<title></title>

</head>

<?php
// Connect database.
include("connectdb.php");

$categoryquery = "SELECT DISTINCT category_name, category_id FROM category";

$categoryresult = mysql_query($categoryquery) or die(mysql_error());
?>
<form name="input" action="post.php" method="post">

<p>Product:</p>
<input size="30" type="text" name="product_name">

<p>Category:
<select name="category_name">
<!-- Drop down -->
<?php
while($row = mysql_fetch_array($categoryresult)){
echo '<option value="' .$row['category_id']. '">'. $row['category_name']. '</option>' ;
}
?>
<option value="category_name">(category_name)</option>
</select>

<p><input type="submit" value="Submit" name="submit"><p>

</form>

</body>
</html>

and I use this to post the data into the database:


<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>

<?php
// Connect database.
include("connectdb.php");

// sample new data to insert into our db
// -------------------------------------
$product_name=$_POST['product_name'];
$category_name=$_POST['category_name'];
$category_id=$_POST['category_id'];
// -------------------------------------

// Inserting the data into product
// -----------------------------
mysql_query("insert into product(product_name) values('$product_name')");

# get the product id
$product_id = mysql_insert_id();

# and insert the product_category
mysql_query("insert into product_category(product_id, category_id) values ('$product_id','$category_id')");

?>
</body>
</html>

The problem is, it will post only the product_id into table product_category.

Could anyone help me to post the category_id as well?

Thank you in advance.

System

2:02 pm on Aug 17, 2008 (gmt 0)

redhat



The following message was cut out to new thread by eelixduppy. New thread at: [webmasterworld.com...]
11:01 am on Aug. 17, 2008 <small>(est -4)</small>

[edited by: eelixduppy at 3:03 pm (utc) on Aug. 17, 2008]

eelixduppy

1:32 pm on Aug 18, 2008 (gmt 0)



Try the following code in place of what you have there already:

mysql_query("insert into product_category(product_id, category_id) values ('$product_id','$category_id')") or die([url=http://www.php.net/mysql-error]mysql_error[/url]());

Tell me if you are getting any errors from this line in the browser. Also, you might want to consider echoing out the query to make sure that everything is in place where you want it; sometimes there is something really small that you'll miss otherwise.