Adding 2 entries to 1 table (categories)

What I am trying to do is when I add a new product, I may need to put it into a new Category which I specify with text.

I have a dropdown of existing categories, and below that I have a field that is “New Category”.

How would I would I add the text field into the column category as a new one? I tried but I got the error “Column ‘category’ specified twice”

Here is the code I have.


<div class="col-md-8">
  <select name="category">
	<option value="">Choose Category</option>
	<option value="Category1">Category1</option>
	<option value="Category2">Category2</option>
</div> <!-- /.col -->
<div class="col-md-8">
   <input type="text" name="addcat" value="" class="form-control" />
</div> <!-- /.col -->


$category = $_POST['category'];	
$addcat = $_POST['category'];	

$query="INSERT INTO new_equip(`id`,`category`,`category`,)

Would I need to add something like this?

@mysql_query("ALTER TABLE `categories` ADD `$addcat` VARCHAR( 100 )") or die(mysql_error());

I would not specify a product category in the same table as the product.
I would have another table for product categories with fields, id, product_id, category

This table might have 4 or 5 records with the same product id.

  • Sports car
  • Classic
  • Chevrolet
  • Corvette
    All having the same id for the same car. So if someone searches for any of these categories this product is displayed.

I understand that, but for my products, there isnt different makes or models.

I just dont know how to add the category to the database when the user inputs it manually instead of the dropdown.

How is the dropdown currently populated?
Still there should be a category table, if not one that holds product id’s then one just for categories. Then when user wants to add a category, the category table is updated. It would then be from this table that the dropdown is populated from.

You’re a sitting duck with that code to potential SQL Injection attack as no attempt is made to validate the user submitted data and it’s not even escaped when inserting it into the database!

Also just as important you need to be aware that the old mysql_* extension which you’re using is deprecated (as of version 5.5 of PHP and is being removed in the next version of PHP (version 7), any code that uses the old mysql_* extension will not work on PHP 7. You need to migrate over to using either the mysqli_* extension or PDO (PDO is better as it allows the use of named parameters and is database server independent (apart from any places that you might use any syntax specific to a given database server).

Once you’ve migrated over to either the mysqli_* extension or PDO you need to always validate the user submitted data and then use prepared statements when sending the data to the database. This article covers the migration from the old mysql_* extension over to PDO.

Getting back to the sql problem, is there a separate table for categories?

If you are not worried about adding the category to the dropdown but just adding it for the record you could use an IF/ELSE type of statement saying IF not empty post addcat set variable category addcat, else set variable category category.

$category = (!empty($_POST['addcat']) ? trim($_POST['addcat']) : trim($_POST['category']));

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.