I have two tables ( post , category )
CREATE TABLE post
(pid INT(10) NOT NULL AUTO_INCREMENT,
post_title VARCHAR(255),
post_result VARCHAR(255),
post_content VARCHAR(500),
cid INT(10),
PRIMARY KEY (pid),
FOREIGN KEY (cid) REFERENCES category(cid));
CREATE TABLE category (
cid INT(10) NOT NULL AUTO_INCREMENT,
cname VARCHAR(50),
PRIMARY KEY (cid));
This is my insert statement:
$id = isset($_POST['pid'])?$_POST['pid']:'';
$title=$_POST['post_title'];
$result=$_POST['post_result'];
$content=$_POST['post_content'];
$catid=$_POST['cid'];
$cat=$_POST['cname'];
$insertquery="INSERT INTO review (pid,post_title,post_result,post_content)
VALUES('','$title','$result','$content')";
Problem what im facing is, in my post table, the values of category id ( cid ) are NULL for every record.
How do make it to store the value of selected category.
This is my form:
<form id="create" action="insert.php" method="post">
<input type="text" name="post_title" placeholder="Title" /><br>
<input type="text" name="post_result" placeholder="Final comment" /><br>
<textarea name="post_content" placeholder="Review" ></textarea><br>
Category:
<select name=cname>
<option value="select"></option>
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
<option value="4">Option 4</option>
<option value="5">Option 5</option>
</select><br><br>
<input class="submit" name="submit" type="submit" value="Submit"/>
</form>
Do help in getting the output .
First of all I hope that you populate your category select from db and
valueattribute contains category id. That being said it’s better to changenameattribute tocid(because that what you expect in POST):Then
var_dump($_POST)will look like this:Secondly you can ditch following php lines:
because you don’t send those values with the form and don’t use them.
Thirdly you need to correct your insert statement
Other considerations:
post.