0

I have the code below. I take with post some variables from another page. Then I run my query and I store them to the table materials. Then I select for this table in order to take materialsid and then I want with insert into to store the value of materialsid in another table called material_damages

    <?php
    session_start();
    if(isset($_POST['submit'])) {
        include('dbConfig.php');

        $mname1=$_POST['name1'];
        $mcost1=$_POST['cost1'];    
        $mquantity=$_POST['quantity'];

        $res=mysql_query("INSERT INTO materials VALUES (NULL, '$mname1', '$mcost1','$mquantity')");
        if ($res) 
        {
            echo "Insert successful";
        }
        else 
        {
            echo "Insert failed";
        }
        $res1=mysql_query("SELECT * FROM materials");
        while ($row = mysql_fetch_array ($res1))
        {
            $id10=$row['materialsid'];
            $id11=(int)$id10;
            $res2=mysql_query("INSERT INTO damage_materials    (damage_materials_id,damage_id,materials_id) VALUES (NULL,NULL,'$id11')");
            if($res2)
            {
                echo "CORRECT";
            }
            else
            {
                echo "FALSE";
            }
        }
    }

    ?>

The material is stored at table materials but the id does not get stored in the table damage_material. It prints Insert succesful FALSE FALSE FALSE FALSE (false is as the number of my materials)

Any ideas?

Rumen Jekov
  • 1,665
  • 2
  • 17
  • 19
user3808157
  • 23
  • 1
  • 1
  • 5
  • For it to print false your queries did not work. It could be a number of things. For now, instead of false, echo `mysql_error()`. [Please also note that the mysql_ extension is deprecated and you should really switch to using PDO or mysqli.](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) – bcmcfc Jul 06 '14 at 12:58
  • it prints Column 'damage_id' cannot be null! but i want to be null at first – user3808157 Jul 06 '14 at 13:06
  • That means a setting on your `damage_id` field limits what you can put in that field. Why does it need to be `null`? – Jared Farrish Jul 06 '14 at 13:25
  • i need to be empty and then if i add a damage i will add it – user3808157 Jul 06 '14 at 13:27
  • If it's an integer field type, use `0`. If that field refers to a proper `id` field in SQL, that will mean `damage_id` dereferenced is an `autonumber` field, where `0` will never be a real reference. – Jared Farrish Jul 06 '14 at 14:38

0 Answers0