php - Record is added but category id is 0 in mysql database -
this code when submit button clicked; adds records in database, category id 0; although there categories in category table
$db = new database(); $query = "select * categories"; $categories = $db->select($query); if (isset($_post['submit'])) { //assign variables $title = mysqli_real_escape_string ($db->link,$_post['title']); $body = mysqli_real_escape_string ($db->link,$_post['body']); $category = mysqli_real_escape_string($db->link,$_post['category']); $author = mysqli_real_escape_string ($db->link,$_post['author']); $tags = mysqli_real_escape_string ($db->link,$_post['tags']); //validate not empty if ($title == '' || $body == '' || $category = '' || $author == '' || tags == '') { $error = 'please fill out fields'; } else { $query = "insert posts (title,body,category,author,tags) values ('$title', '$body', '$category', '$author', '$tags')" ; $insert_row = $db->insert($query); } }
this html block issue:
<div class="form-group"> <label>category</label> <select name="category" class="form-control"> <?php while($row = $categories->fetch_assoc()) : ?> <option value="<?php echo $row['id'];?>"><?php echo $row['name'];?> </option> <?php endwhile; ?> </select> </div>
check if using auto_increment on category.id (or named.)
if yes, can check status of auto_increment with:
select auto_increment information_schema.tables table_name = 'categories' , table_schema = database( ) ;
or
show table status 'categories'
(btw can reset auto_increment field with:
alter table categories auto_increment = 100;
but thinking don't have auto_increment there @ all.)
Comments
Post a Comment