WebbieWorks
WebbieWorks

Reputation: 163

Adding 2 entries to 1 table (categories)

I am trying to learn php and I have read and looked but cannot find a solution to this.

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". (where I can input a new category if needed)

How would I would I add the text field into the table 'category' as a new one? I tried but I got the error "Column 'category' specified twice"

Here is the code I have.

HTML

    <div class="col-md-8">
      <select name="category">
        <option value="">Choose Category</option>
        <option value="Category1">Category1</option>
        <option value="Category2">Category2</option>
      </select>
    </div> <!-- /.col -->

<div class="col-md-8">
   <input type="text" name="addcat" value="" class="form-control" />
</div> <!-- /.col -->

PHP

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

$query="INSERT INTO new_equip(`id`,`category`,`category`,)
VALUES('$id','$category','$addcat')";

mysql_query($query)  or die(mysql_error());
echo mysql_error();

Would I need to add something like this?

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

Upvotes: 0

Views: 23

Answers (1)

dstudeba
dstudeba

Reputation: 9038

If I understand your database schema correctly, you have a categories table and a new_equip table.

When you add a new category it will be inserted to the categories table as such: (you do not want to add a new column)

INSERT INTO categories (category) VALUES ($addcat)

This will then show up with the other categories in the dropdown in your PHP.

You also can add it to the new_equip table with:

INSERT INTO new_equip(`id`,`category`) VALUES('$id', '$addcat')

That being said, please use mysqli and parameter binding, or PDO to avoid SQL injection attacks.

Upvotes: 1

Related Questions