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.

Recommended Answers

All 25 Replies

Add a value attribute to each option tag:

echo '<option value="' . $rows[1] . '">'.$rows[1].'</option>';

And make sure you enclose the select element in <form> </form> tags?

i already tried this but nothing happend

Can you post the code for the whole script.

you mean include html too ?

Yes, only for the page with the select element (drop down). I will try it in my environment.

ok here is the full script::

<?php
session_start();
?>
<html>
<head>
<title>Create New Post</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div id="main_div">
<div align="right" style="width:96%; padding-right:50px; color:#4760C0; font-size:18px; font-weight:300; text-align:right;">
<table align="right">
<tr>
<td align="right">
<?php
if(isset($_SESSION['login_name']))
{
$name = $_SESSION['login_name'];
echo '<table>';
echo '<tr>';
echo '<td align="left">
<a href="Logout.php">Logout</a>&nbsp;|&nbsp;<a href="new_user.php">Sign Up</a><br>
<p style="font-size:18px;font-weight:700;">Welcome '.$name.'</p>';
echo '</td>';
echo '</tr>';
echo '</table>';
}
else
{
    echo '<table>';
echo '<tr>';
echo '<td align="left">
<a href="Logout.php">Login</a>&nbsp;|&nbsp;<a href="new_user.php">Sign Up</a>';
echo '</td>';
echo '</tr>';
echo '</table>'; 
}
?>
</td>
</tr>
</table>
</div><br ><br>
<div style="width:100%;margin:auto; padding-top:20px;">
<div id="nav_div">
<ul>
<li><a href="index.php">HOME</a></li>
</ul>
<ul>
<li><a href="forums.php">FORUMS</a></li>
</ul>
<ul>
<li><a href="programming.php">PROGRAMMING</a></li>
</ul>
<ul>
<li><a href="web_design.php">WEB DESIGNING</a></li>
</ul>
<ul>
<li><a href="databse.php">DATABASES</a></li>
</ul>
<ul>
<li><a href="new_post.php">NEW POST</a></li>
</ul>
</div>
<br />
<form id="form1" method="post">
<table cellpadding="2" cellspacing="5" style="width:80%;background-color:#E2E2E2; margin:auto;">
<tr>
<th align="left" colspan="2">What's your Question ?</th>
</tr>
<tr>
<td>Title : <input type="text" name="title" size="150"/>
</td>
</tr>
<tr>
<td>
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 value="'.$rows[1].'">'.$rows[1].'</option>';
    }
    echo "</select>";
?>
</td>
</tr>
<tr>
<td><textarea name="question" cols="112" rows="10"></textarea>
</td>
</tr>
<tr>
<td>
<img style="padding-left:82px;" src="captcha.php" /></td>
</tr>
<tr>
<td>
Enter code : <input type="text" name="match_code" />
</td>
</tr>
<tr>
<td><input type="submit" name="ask" value="Post your Question" /></td>
</tr>
</table>
</form>
</div>
</div>
</div>
</body>
</html>
<?php
include_once("database.php");
if(isset($_POST['ask']))
{
    if(isset($_SESSION['login_name']))  
    {
        $post_id = rand(0,9999);
        $database = new Database();
        $database->connection();

        //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]','$_POST[category]','curdate()')");

        header("Location: home.php");   
    }
    else
    {       

        header("Location:error.php");   
    }
}

?>

It works in my improvised environment. Can you please put this line of code on the line 126 in your code above:

die("insert into posts(post_id,title,description,post_by,cat_id,post_date) values('$post_id','$_POST[title]','$_POST[question]','$_SESSION[login_name]','$_POST[category]','curdate()')");

It will print out the query and stop the script. Please post the output here. You can also copy the query into phpmyadmin and test it there.

what about the statement in line 128 ????
and yes it prints the query now what ??

Post the query here.

One of the reasons for not inserting could be in the fact that you are missing spaces after the database name and after the VALUES keyword. And surround the array elements in the query with curly braces. Try to change the query this way:

$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]}','{$_POST[category]}','curdate()')");

this is a query it prints
insert into posts(post_id,title,description,post_by,cat_id,post_date) values('1621','How to insert into two tables in mysql at the same time','how to insert into two tables at the same tome ??','waqar123','MYSQL','curdate()')

and it still not inserting into database :(

I see you generate a random post id. What type of column is this in the database?

You should remove the single quotes around curdate(), as it is a MySQL function.

Have tried to copy the displayed query into phpmyadmin (after removing quotes arround curdate)?

sorry i was busy somewhere ye i tried but the error is i need to insert cat_id in posts table which is a foreign key in categories table.

my actual question is how to get category id when i select a category from drop down list

everything ok now but unable to insert into database.

If the category ID is the first field in the database then change the value part of the select code:

echo '<option value="'.$rows[0].'">'.$rows[1].'</option>';

If does not work post the structure of the categories table here.

i already did this but no success :D, um database table category structure??

Run this query in phpmyadmin and post the output here:

SHOW CREATE TABLE categories

CREATE TABLE categories ( cat_id int(11) NOT NULL AUTO_INCREMENT, category varchar(50) DEFAULT NULL, PRIMARY KEY (cat_id)) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=latin1

Can you change the query this way and post what gets displayed by the die() statement:

echo '<option value="' . $rows['cat_id'] . '">' . $rows['category'] . '</option>';

it prints the same thing

insert into posts(post_id,title,description,post_by,cat_id,post_date) values('2933','How to insert into two tables in mysql at the same time','Hi How can insert into two tables in mysql at the same time ??','waqar123','8','curdate()')

as we did

echo '<option value="'.$rows[0].'">'.$rows[1].'</option>';

if i just copy pase this query into phpmyadmin directly it works, but not from php

You use the Database class for handling DB. Does this class have a method for displaying errors? You should use it to figure out what is wrong.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.