0

i have a recipe table and ingredient table the primary key of both tables are auto increament and primary key of recipe is foreign key in ingredient. i post data from html to php.Note that my ingredient textboxes are generated dynamically and successfully post the data to php script. posted data is correct when i insert this data to table my query working fine but data is not added to mysql table. my code and output is

$sql = "insert into recipe (rec_id, Name, Overview,category, Time, Image) values ('', '$name','$overview','$category','$time','$TARGET_PATH')";
    $result = mysql_query($sql) or die ("Could not insert data into DB: " . mysql_error());
        $rec_id = mysql_insert_id();

and for ingredient

$ingredient = $_POST['ingredient'];
$amount = $_POST['amount'];
$integer = 0;
while (count($ingredient)>$integer) {
if (($ingredient[$integer] <> "") && ($amount[$integer] <> "")){
$sql =  "INSERT INTO `cafe`.`ingredients` (`ingredient_id`, `ingredient_name`, `ammount`, `rec_id`,)
    VALUES ('', '".$ingredient[$integer]."', '".$amount[$integer]."', '$rec_id')";
mysql_query($sql);
echo $sql."
"; } else{ echo "ingredient number ".($integer+1)." is missing values and cannot be inserted."; } $integer = ($integer + 1); }

when i echo the queries the out put is

nsert into recipe (rec_id, Name, Overview,category, Time, Image) values ('', 'demo recipe','no overview','meal','10/12/10 : 13:02:33','http://www.localhost/cafe/pics/demo.gif')
INSERT INTO cafe.ingredients (ingredient_id, ingredient_name, ammount, rec_id,) VALUES ('', 'ingredient one', '3gm', '29')
INSERT INTO cafe.ingredients (ingredient_id, ingredient_name, ammount, rec_id,) VALUES ('', 'ingredient two', '3gm', '29')
INSERT INTO cafe.ingredients (ingredient_id, ingredient_name, ammount, rec_id,) VALUES ('', 'ingredient three', '3gm', '29')

but when i see the mysql table or retriew data from ingredient there is no data in ingredient.

1
  • can you please post the definitions for the recipe and the ingredient table? Commented Dec 10, 2010 at 8:24

5 Answers 5

2

You have an extra , after rec_id.

Remove it, so it looks like

INSERT INTO cafe.ingredients (ingredient_id, ingredient_name, ammount, rec_id) VALUES ('', 'ingredient one', '3gm', '29')

And you will be OK

Sign up to request clarification or add additional context in comments.

Comments

0

There seems to be a syntax error in your code:

if (($ingredient[$integer]  "") && ($amount[$integer]  ""))
                            ^^                         ^^

Looks like you are missing a comparison operator.

2 Comments

hay this not like this actually it is: if (($ingredient[$integer] <> "") && ($amount[$integer] <> ""))
that too and you're not checking if there where errors on insert query . please have a look at how to check it with mysql_error() , becouse for example let's say you have an auto increment on ingredient_id , you're sending a null string witch will throw an error becouse ingredient_id should be a number or if you whant to auto_increment don't send ingredient_id at all .
0

You might want to verify if you are using a BEGIN call and not committing after INSERT. You can refer to http://www.devarticles.com/c/a/MySQL/Using-Transactions-with-MySQL-4.0-and-PHP/

in that scenario.

Comments

0

When insert doesnt throw exceptions and doesnt insert data there are I think a couple of options

1) you use transaction somewhere and rollback it 2) your select query is bad and data is there, but you just dont select it

3 Comments

but when i checked data in phpmyadmin there is no record found for ingredient
well 1) look for uncommited transactions 2) try to delete everything from ingridents tables and run your code, then select * from there, if no exceptions are thrown it would require some magic I think for a 3rd solution
i delete all the records from recipe as well as ingredients table but still recipe is add but ingredient does't add to the table and queries are correct
0

remove cafe from the query

$sql =  "INSERT INTO ingredients (`ingredient_id`, `ingredient_name`, `ammount`, `rec_id`,)
    VALUES ('', '".$ingredient[$integer]."', '".$amount[$integer]."', '$rec_id')";

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.