i'm creating my personal forum website, in create new topic page i have a HTML drop down, i populate this drop down from my categories table from my databse. the problem is that when i select any value from drop down it didn't it's id from database and due to this it does not insert into posts table.
how can i do this. i also tried "$_POST['category']" //my select name
this is how i populate my dropdown from database.
Category :
<?php
include_once("database.php");
$database = new Database();
$database->connection();
$data = $database->ShowData("select * from categories");
echo "<select name='category'>";
echo '<option>Select Category</option>';
while($rows = mysql_fetch_array($data))
{
$c_id = $rows[0];
echo '<option>'.$rows[1].'</option>';
}
echo "</select>";
?>
and this is how tried to get it's id from database then insert into posts table but no success :(
<?php
include_once("database.php");
if(isset($_POST['ask']))
{
if(isset($_SESSION['login_name']))
{
$post_id = rand(0,9999);
$database = new Database();
$database->connection();
$cid = $database->ShowData("select * from categories");
while($c_row = mysql_fetch_array($cid))
{
//didn't get the category id
$id = $_POST['category'];
if($c_row[0] == $id)
{
$catid = $c_row[0];
}
}
//not inserting into posts table
$database->ExecuteQuery("insert into posts(post_id,title,description,post_by,cat_id,post_date) values('$post_id','$_POST[title]','$_POST[question]','$_SESSION[login_name]','$catid','curdate()')");
header("Location: home.php");
}
else
{
header("Location:error.php");
}
}
?>
i know i'm missing something but i can't figure it out.