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.
Leave a Reply