I have this two tables 'fcategory' and 'fthreads'. fcategory fields: category_id, category_name. fthreads fields: thread_id, thread_title, category_name, category_id, user_id.
when i am creating a new thread in php, i want category_id to get fetched along with the category_name and insert it into fthreads table.
here is my php file: threads.php
<form action="threadsp.php" name="myform" method="post">
<label for="field4"><span>Category</span>
<?php
$query = "select * from fcategory";
$result = mysqli_query($conn, $query);
$resultsearch = mysqli_fetch_array($result);
if(!$result){
die('could not get data:'.mysqli_error($conn));
}
echo '<select name="category" class="select-field">';
while ($row = mysqli_fetch_assoc($result)) {
echo '<option value="'.$row['category_name'].'">'.$row['category_name'].'</option>';
}
echo "</select>";
echo "</label>";
?>
<label for="field1"><span>Thread Title <span class="required">*</span></span>
<input type="text" class="input-field" name="title" value="" />
</label>
<label><span> </span>
<input type="submit" value="Create" />
</label>
</form>
second file: threadsp.php
<?php
$catg = $_POST['category'];
$title = $_POST['title'];
$userid = $_SESSION['uid'];
$sql = "select category_id from fcategory where category_name = '$catg'";
$result2 = mysqli_query($conn, $sql);
$catgidresult = mysqli_fetch_array($result2);
$query = "insert into fthreads (category_name,thread_title,user_id,category_id) values('".$catg."','".$title."','".$userid."','".$catgidresult."')";
$result = mysqli_query($conn, $query);
if(!$result){
echo "failed".mysqli_error($conn);
}else{
header("Location: question.php");
die();
}
?>
i am able to fetch category_name but value of category_id shows 0. any help will be appreciated. thank you