Connecting Tech Pros Worldwide Forums | Help | Site Map

Dynamic dropdown menu to database

Newbie
 
Join Date: Nov 2006
Posts: 4
#1: Nov 20 '06
Hey all,

Heres the situation:
I have a form page that needs to have multiple drop down menus that is pulling their options from a database. Right now I have one working out of 3. So that is one issue, now the issue that I am having with the one that is working, is that it is not writing anything to the database. No matter what you select, the database is empty. Any help would be sweet.
So here is the form page, with just a few text fields, and the one dynamic drop down that is working:

[PHP]<?PHP
//this code is bringing in the values for the dropdown.
$sql="SELECT * FROM category";
$result=mysql_query($sql);
$options="";

while ($row=mysql_fetch_array($result)) {
$id=$row["cat"];
$cat=$row["category"];
$options.="<OPTION VALUE=\"$id\">".$cat.'</option>';

}
?>
<html>
<head>
<title>Manage Spots</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body><form action="process.php" method="post" enctype="multipart/form-data">
<p>Title
<input type="text" name="Title">
</p>
<p>Spot #
<input type="text" name="Spot_Number">
</p>
<p>Series
<select name="Series" id="Series">
<option value="test">test</option>
</select>
&nbsp;&nbsp; <a href="series_edit.php">Manage Series</a> </p>
<p>Date
<input type="text" name="Creation_Date">
</p>
<p>Category #1
<select name="Category_One" id="Category_One">
<option value="0"><? echo $options ?> </option>
</select>
&nbsp;&nbsp; <a href="category_edit.php">Manage Categories</a>
</p>
<p>&nbsp;</p>
<p>
<input type="submit" name="Submit" value="Submit">
</p>
</form>[/PHP]

Then I have it going to a page called process.php that just inserts everything into the database, here is the page if that helps:

[PHP]<?PHP
$dbh=mysql_connect ("localhost", "", "") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("snmwebde_rynodb");
$Title=$_POST{'Title'};
$Spot_Number=$_POST{'Spot_Number'};
$Series=$_POST{'Series'};
$Creation_Date=$_POST{'Creation_Date'};
$Category_One=$_POST{'Category_One'};
?>
<?PHP
$query = "insert into spots values
('$Title','$Spot_Number','$Series','$Creation_Date ','$Category_One')";
$result = mysql_query($query);
?>
Entry added!!<br>
To view database click <a href="viewdb.php">here</a>.

</body>
</html>[/PHP]

Any help would be really great, seeing if i fix these 2 problems, I am done with this project. Thanks again

ronverdonk's Avatar
Moderator
 
Join Date: Jul 2006
Location: The Netherlands
Posts: 4,139
#2: Nov 20 '06

re: Dynamic dropdown menu to database


Your <option> statement is screwed up. Statement [php]<option value="0"><? echo $options ?> </option>[/php] generates [php]<option value="0">
<option value=xx>yy</option>
<option value=aa>bb</option>
</option>[/php]
I cannot predict the result of this. Fix that first and see the results.

Ronald :cool:
Newbie
 
Join Date: Nov 2006
Posts: 4
#3: Nov 20 '06

re: Dynamic dropdown menu to database


ya i noticed that after i posted, still nothing gets posted to the database for that field
ronverdonk's Avatar
Moderator
 
Join Date: Jul 2006
Location: The Netherlands
Posts: 4,139
#4: Nov 20 '06

re: Dynamic dropdown menu to database


Quote:

Originally Posted by Bertman105

Hey all,

Heres the situation:
I have a form page that needs to have multiple drop down menus that is pulling their options from a database. Right now I have one working out of 3. So that is one issue, now the issue that I am having with the one that is working, is that it is not writing anything to the database. No matter what you select, the database is empty. Any help would be sweet.
So here is the form page, with just a few text fields, and the one dynamic drop down that is working:

[PHP]<?PHP
//this code is bringing in the values for the dropdown.
$sql="SELECT * FROM category";
$result=mysql_query($sql);
$options="";

while ($row=mysql_fetch_array($result)) {
$id=$row["cat"];
$cat=$row["category"];
$options.="<OPTION VALUE=\"$id\">".$cat.'</option>';

}
?>
<html>
<head>
<title>Manage Spots</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body><form action="process.php" method="post" enctype="multipart/form-data">
<p>Title
<input type="text" name="Title">
</p>
<p>Spot #
<input type="text" name="Spot_Number">
</p>
<p>Series
<select name="Series" id="Series">
<option value="test">test</option>
</select>
&nbsp;&nbsp; <a href="series_edit.php">Manage Series</a> </p>
<p>Date
<input type="text" name="Creation_Date">
</p>
<p>Category #1
<select name="Category_One" id="Category_One">
<option value="0"><? echo $options ?> </option>
</select>
&nbsp;&nbsp; <a href="category_edit.php">Manage Categories</a>
</p>
<p>&nbsp;</p>
<p>
<input type="submit" name="Submit" value="Submit">
</p>
</form>[/PHP]

Then I have it going to a page called process.php that just inserts everything into the database, here is the page if that helps:

[PHP]<?PHP
$dbh=mysql_connect ("localhost", "", "") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("snmwebde_rynodb");
$Title=$_POST{'Title'};
$Spot_Number=$_POST{'Spot_Number'};
$Series=$_POST{'Series'};
$Creation_Date=$_POST{'Creation_Date'};
$Category_One=$_POST{'Category_One'};
?>
<?PHP
$query = "insert into spots values
('$Title','$Spot_Number','$Series','$Creation_Date ','$Category_One')";
$result = mysql_query($query);
?>
Entry added!!<br>
To view database click <a href="viewdb.php">here</a>.

</body>
</html>[/PHP]

Any help would be really great, seeing if i fix these 2 problems, I am done with this project. Thanks again

Have you investigated any errors coming out of the select, like this:
[php]$result=mysql_query($sql)
or die("Select query error: " . mysql_error());[/php]
At least you know then what the actual problem might be and where to look further.

Ronald :cool:
Newbie
 
Join Date: Nov 2006
Posts: 4
#5: Nov 20 '06

re: Dynamic dropdown menu to database


see there are no errors because everything else will write to the database, i just don't know if its even possible to take info like that and put it into a database
ronverdonk's Avatar
Moderator
 
Join Date: Jul 2006
Location: The Netherlands
Posts: 4,139
#6: Nov 20 '06

re: Dynamic dropdown menu to database


Have you print_r the $_POST array to see if anything is passed in Category_One?

Ronald :cool:
Newbie
 
Join Date: Nov 2006
Posts: 4
#7: Nov 20 '06

re: Dynamic dropdown menu to database


Quote:

Originally Posted by ronverdonk

Have you print_r the $_POST array to see if anything is passed in Category_One?

Ronald :cool:

how do i do that?
ronverdonk's Avatar
Moderator
 
Join Date: Jul 2006
Location: The Netherlands
Posts: 4,139
#8: Nov 21 '06

re: Dynamic dropdown menu to database


As the first statements in your script:[php]<?php
echo '<pre>'; // makes it better readable on screen
print_r($_POST);[/php]This will show you all values passed t and stored in the $_POST associative array.

Ronald :cool:
Reply