• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

AnswerTeller

Get the Answer!

  • APPEARANCE
    • Hair
    • Skin
      • Tattoo
      • Piercing
    • Breasts
    • Makeup
    • Clothing
      • Lingerie
    • Shoes
    • Fashion
  • RELATIONS
    • Dating
    • Love
    • Friendship
    • Sex
    • Family
      • Husband
      • Wife
      • Parents
      • Brother
      • Sister
      • Children
    • Marriage
    • Divorce
    • Teenagers
    • Gifts
  • HOME
    • House
    • Apartment
    • Renting
    • Cooking
    • Garden
    • Pets and Animals
    • Car
    • Bikes
  • HEALTH
    • Maternity
  • LEARNING
    • School
    • College
    • University
    • Education
    • Exams
    • Maths
      • Algebra
      • Geometry
    • Physics
    • Chemistry
    • Anatomy
    • Quiz
  • KNOWLEDGE
    • Life
    • Religion
      • Bible
    • Books
    • History
    • Law
      • Crime
    • Science
    • Economics
    • Politics
    • International
    • Languages
  • WORK
    • Careers
    • Jobs
    • Interview
    • Business
    • Real Estate
    • Office
  • MONEY
    • Credit
    • Loan
    • Buying
    • Selling
    • Shopping
    • Insurance
    • Taxes
    • Stocks
    • E-Money
    • Gambling
    • Scam
  • LEISURE
    • Entertainment
      • Home Cinema
    • Celebrities
    • Sports
    • Movies
    • Music
    • Dancing
      • Ballet
    • Dining Out
      • Restaurants
      • Fast Food
    • Vacations
    • Holidays
      • Christmas
      • New Year
      • Easter
      • Halloween
    • Photography
  • TRAVEL
    • Accommodation
      • Hotels
      • Motels
    • Flights
    • Airlines
    • Trains
  • TRIVIA
    • Fortune
      • Horoscope
    • How To
    • How Do I
    • Puzzle
    • Quiz
    • Miscellaneous
Browse: Home / HOME / Cooking

How can I insert data from 1 form into multiple MySQL tables?

Christopher: How can I insert data from 1 form into multiple MySQL tables?
——————————————————————Table Structure———————————————————
CREATE TABLE RecipeTable
(recipeID INTEGER NOT NULL,
recipe_title VARCHAR(100) NOT NULL,
recipe_creator VARCHAR(100) DEFAULT NULL,
recipe_servingqty INTEGER DEFAULT NULL,
recipe_servingsize VARCHAR(100) DEFAULT NULL,
recipe_preptime DOUBLE DEFAULT NULL,
recipe_cookingtime DOUBLE DEFAULT NULL,
recipe_category VARCHAR(255) DEFAULT NULL,
CONSTRAINT RecipeTable_PK PRIMARY KEY (recipeID),
CONSTRAINT RecipeTable_FK1 FOREIGN KEY (recipe_creator) REFERENCES UserTable (userID)
);

CREATE TABLE CompositeCategoryTable
(recipeID INTEGER NOT NULL,
categoryID INTEGER NOT NULL,
CONSTRAINT CompositeCategoryTable_PK PRIMARY KEY (recipeID, categoryID)
CONSTRAINT CompositeCategoryTable_FK1 FOREIGN KEY (recipeID) REFERENCES RecipeTable (recipeID),
CONSTRAINT CompositeCategoryTable_FK2 FOREIGN KEY (categoryID) REFERENCES CategoryTable (categoryID),
);

CategoryTable
CREATE TABLE CategoryTable
(categoryID INTEGER NOT NULL,
category_name VARCHAR(255) NOT NULL,
CONSTRAINT CategoryTable_PK PRIMARY KEY (categoryID),
);

——————————————————————PHP Code———————————————————
$ editFormAction = $ _SERVER[‘PHP_SELF’];
if (isset($ _SERVER[‘QUERY_STRING’])) {
$ editFormAction .= “?” . htmlentities($ _SERVER[‘QUERY_STRING’]);
}

if ((isset($ _POST[“MM_insert”])) && ($ _POST[“MM_insert”] == “New Recipe”)) {
$ query = sprintf(“INSERT INTO recipetable (recipeid, recipe_title, recipe_creator, recipe_servingqty, recipe_servingsize, recipe_preptime, recipe_cookingtime) VALUES (%s, %s, %s, %s, %s, %s)”,
‘NULL’,
GetSQLValueString($ _POST[‘tfield_title’], “text”),
GetSQLValueString($ _POST[‘tfield_creator’], “text”),
GetSQLValueString($ _POST[‘menu_serving_qty’], “int”),
GetSQLValueString($ _POST[‘menu_serving_size’], “int”),
GetSQLValueString($ _POST[‘tfield_preparation_time’], “double”),
GetSQLValueString($ _POST[‘tfield_cooking_time’], “double”));

//BEGIN Category Checkbox Array Statement
$ category = implode(“,”,$ _POST[‘category’]);
//END Category Checkbox Array Statement

$ query2 = sprintf(“INSERT INTO categorytable (categoryid, category_name) VALUES (%s, ‘$ category’)”,
‘NULL’,
‘$ category’);//category is a checkbox array

if (@mysql_query ($ query))
{
if (@mysql_query ($ query2))
{
print ‘

User Created.

‘;
}
else
{
print ‘

Could not create user in testtwo because: ” .mysql_error().”. The query was $ query2.

‘;

}
}
else
{
print ‘

Could not create user in test because: “.mysql_error().” . The query was $ query.

‘;
}
}
?>
——————————————————————PHP Code———————————————————

—————————————————————–HTML Code———————————————————

My form actionis: action=”
—————————————————————–HTML Code———————————————————

I am not able to submit my data successfully into these two tables from my one form. Can someone assist me with what I am doing wrong. I am very new to PHP and it seems everyone has a different solution, but not all work. More detail the better in your answers. Thank you!

Answers and Views:

Answer by rbjolly
Not to be harsh, but your code is so wrong it makes me wonder if you can write even a single query to properly insert data into just one simple table, much less two or more tables with key constraints.

There are so many errors just getting to and building the first query that it is obvious you need to learn the basics.

Make the effort to learn how to program in PHP/MySQL, then ask for help.

Related Questions:

  • 1.  Need cooking tips. I got hired as a cook and I dont know how to cook?
  • 2.  Looking for cooking lessons near Cleveland for my girlfriend and I, any ideas?
  • 3.  How did your cooking skills and food improve?
  • 4.  For a large tiered cake, how do I “cascade from top to bottom? Is there something to attach it to at the top?

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Categories

  • APPEARANCE
    • Breasts
    • Clothing
      • Lingerie
    • Fashion
    • Hair
    • Makeup
    • Shoes
    • Skin
      • Piercing
      • Tattoo
  • COMPUTER
    • Computing
    • Hardware
    • Internet
      • Google
      • Networking
      • Spam
      • Yahoo
    • Microsoft
    • Programming
    • Software
  • HEALTH
    • Maternity
  • HOME
    • Apartment
    • Bikes
    • Car
    • Cooking
    • Garden
    • House
    • Pets and Animals
    • Renting
  • KNOWLEDGE
    • Books
    • Economics
    • History
    • International
    • Languages
    • Law
      • Crime
    • Life
    • Politics
    • Religion
      • Bible
    • Science
  • LEARNING
    • Anatomy
    • Chemistry
    • College
    • Education
    • Exams
    • Maths
      • Algebra
      • Geometry
    • Physics
    • School
    • University
  • LEISURE
    • Celebrities
    • Dancing
      • Ballet
    • Dining Out
      • Fast Food
      • Restaurants
    • Entertainment
      • Home Cinema
    • Holidays
      • Christmas
      • Easter
      • Halloween
      • New Year
    • Movies
    • Music
    • Photography
    • Sports
    • Vacations
  • MONEY
    • Buying
    • Credit
    • E-Money
    • Gambling
    • Insurance
    • Loan
    • Scam
    • Selling
    • Shopping
    • Stocks
    • Taxes
  • Popular Questions
  • RELATIONS
    • Dating
    • Divorce
    • Family
      • Brother
      • Children
      • Husband
      • Parents
      • Sister
      • Wife
    • Friendship
    • Gifts
    • Love
    • Marriage
    • Sex
    • Teenagers
  • TRAVEL
    • Accommodation
      • Hotels
      • Motels
    • Airlines
    • Flights
    • Trains
  • TRIVIA
    • Fortune
    • Horoscope
    • How Do I
    • How To
    • Miscellaneous
    • Puzzle
    • Quiz
  • WORK
    • Business
    • Careers
    • Interview
    • Jobs
    • Office
    • Real Estate

Search for More!

Copyright AnswerTeller © 2025 · About · Privacy Policy · Contacts · Sitemap Disclaimer: AnswerTeller.com is an informational website, and its content does not constitute professional advice of any kind. As an Amazon Associate, we earn from qualifying purchases.