SitePoint Sponsor

User Tag List

Page 2 of 2 FirstFirst 12
Results 26 to 39 of 39
  1. #26
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,135
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    That error means that there is no row in the table order_table with a column id containing the $value.

    This can be confirmed by running the below query replacing the $value with the expected integer.

    Code SQL:
    SELECT COUNT(*) ct FROM order_table WHERE id = $value

    A single row should be returned with a value of 0.

    Once that is confirmed the solution is to reference a row that does exist or create the row in that table.

    It is good though that you are using a foreign key. Otherwise, you would essentially have an orphaned row in the order details table.
    The only code I hate more than my own is everyone else's.

  2. #27
    SitePoint Enthusiast
    Join Date
    Apr 2013
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    #1054 - Unknown column '$value' in 'where clause'

  3. #28
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,135
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    $value is meant to a token/placeholder for the actual value of the order_id. Go back to the printed query and copy the value of the order_id column that would be inserted.
    The only code I hate more than my own is everyone else's.

  4. #29
    SitePoint Enthusiast
    Join Date
    Apr 2013
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    when I run this: SELECT COUNT(*) ct FROM order_table WHERE id = 1
    it works with no error

    the id should be 1 right? first record. and that value should be entered into the foreign key

    how do I access the id of the first table in order to insert its value into the foreign key?

  5. #30
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,135
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    PHP Code:
    foreach($_POST['100type'] AS $key=>$value) {
        
    $sql "INSERT INTO order_details_table
               (id,order_id,block_type,block_quantity,block_pallets,block_extras)"
    .
                          
    "VALUES (null,{$value},"// <- you want the value of the $value variable
                        
    "'{$_POST['100type'][$key]}',
                        '
    {$_POST['100quantity'][$key]}',
                '
    {$_POST['100pallets'][$key]}',
                '
    {$_POST['100extras'][$key]}'"                    
                      
    )";

    echo "
    <p>$sql</p>"; // copy and paste this query into your MySQL client of choice and use the error to figure out what needs to be done

        
    $result = mysql_query($sql) or die('Could not insert data');

    The only code I hate more than my own is everyone else's.

  6. #31
    SitePoint Enthusiast
    Join Date
    Apr 2013
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    that is : 10.01
    which is the type.. ie. {$_POST['100type']

    so $value has the same value as the $_POST['100type']
    that shouldn't be like that right?

  7. #32
    SitePoint Enthusiast
    Join Date
    Apr 2013
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    foreach($_POST['100type'] AS $key=>$value) {
        
    $sql "INSERT INTO order_details_table
               (order_id,block_type,block_quantity,block_pallets,block_extras)
                          VALUES (
    {$value},'{$_POST['100type'][$key]}'
                        '
    {$_POST['100quantity'][$key]}',
                '
    {$_POST['100pallets'][$key]}',
                '
    {$_POST['100extras'][$key]}'                     
                      )"

    this does foreach($_POST['100type'] AS $key=>$value) , where 100type as $key should not be the key right? The foreign key should be the key???? but it is not in the POST data

    but it should be like that foreach($_POST['100type'] AS $key=>$value) cause there are more tables on the same page with different types. right?

    there is the 100type, the 150type, 200type so on. all going into the same table

  8. #33
    SitePoint Enthusiast rajeev13's Avatar
    Join Date
    Nov 2012
    Location
    New Delhi,India
    Posts
    35
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ROUBOS View Post
    the foreign key is causing it
    #1452 - Cannot add or update a child row: a foreign key constraint fails (`bots`.`order_details_table`, CONSTRAINT `order_details_table_ibfk_1` FOREIGN KEY (`order_id`) REFERENCES `order_table` (`id`) ON DELETE CASCADE ON UPDATE CASCADE)


    how do I copy the id from the first table to the order_id in the second table? that's the foreign key that needs to be populated. They are all in one form.
    some data are inputed in one table and some in the other
    mysql_insert_id() :- Retrieves the ID generated for an AUTO_INCREMENT column by the previous query (usually INSERT).
    you may retain this id to insert into your order_id column.

  9. #34
    SitePoint Enthusiast
    Join Date
    Apr 2013
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is the code:
    PHP Code:
    <?php
    include('db.php'); //connect

    //print_r($_POST); 

        
    echo '<pre style="text-align: left;">' print_r($_POSTtrue) . '</pre>';
        echo 
    '------------------------------------------------<br/>';
        
    $insert="INSERT INTO order_table (id, company_name, delivery_address, order_date, delivery_date, status, hb_checked, loaded_by, pallets, extra_blocks, extra_pallets, total_pallets)
    VALUES
    (NULL,'
    $_POST[companyname]','$_POST[deliveryaddress]','$_POST[orderdate]','$_POST[deliverydate]','$_POST[status]','$_POST[checked]','$_POST[loadedby]','$_POST[sumpallets]','$_POST[sumextras]','$_POST[makeup]','$_POST[grandtotal]')";

    $kavli mysql_insert_id();
    $kavli intval($kavli);
    echo 
    $kavli;

    echo 
    "<p>$insert</p>";


    foreach(
    $_POST['100type'] AS $key=>$value) {
        
    $sql "INSERT INTO order_details_table
               (id, order_id,block_type,block_quantity,block_pallets,block_extras)
                VALUES (NULL,'
    $kavli','{$_POST['100type'][$key]}','{$_POST['100quantity'][$key]}','{$_POST['100pallets'][$key]}','{$_POST['100extras'][$key]}')";

    echo 
    "<p>$sql</p>"// copy and paste this query into your MySQL client of choice and use the error to figure out what needs to be done

        
    $result mysql_query($sql) or die('Could not insert data');
    }  

    ?>
    And my result:
    PHP Code:
    Array
    (
        [
    companyname] => NACA
        
    [deliveryaddress] => Stuart Park
        
    [orderdate] => 2013-04-20
        
    [deliverydate] => 2013-05-22
        
    [status] => Completed
        
    [checked] => Yes
        
    [loadedby] => Me
        
    [sumpallets] => 4
        
    [sumextras] => 130
        
    [makeup] => 1
        
    [grandtotal] => 5
        
    [submit] => Submit Order
        
    [100type] => Array
            (
                [
    1] => 10.01
                
    [2] => 10.02
                
    [3] => 10.03
                
    [4] => 10.04
                
    [5] => 10.31
                
    [6] => 10.83
                
    [7] => 10.702
                
    [8] => 10.772
                
    [9] => 10.71
                
    [10] => 10.72
                
    [11] => 10.73
                
    [12] => 10.74
            
    )

        [
    100perpallet] => Array
            (
                [
    1] => 180
                
    [2] => 240
                
    [3] => 360
                
    [4] => 576
                
    [5] => 144
                
    [6] => 288
                
    [7] => 280
                
    [8] => 560
                
    [9] => 360
                
    [10] => 480
                
    [11] => 720
                
    [12] => 1152
            
    )

        [
    100quantity] => Array
            (
                [
    1] => 654
                
    [2] => 256
                
    [3] => 0
                
    [4] => 0
                
    [5] => 0
                
    [6] => 0
                
    [7] => 0
                
    [8] => 0
                
    [9] => 0
                
    [10] => 0
                
    [11] => 0
                
    [12] => 0
            
    )

        [
    100pallets] => Array
            (
                [
    1] => 3
                
    [2] => 1
                
    [3] => 0
                
    [4] => 0
                
    [5] => 0
                
    [6] => 0
                
    [7] => 0
                
    [8] => 0
                
    [9] => 0
                
    [10] => 0
                
    [11] => 0
                
    [12] => 0
            
    )

        [
    100extras] => Array
            (
                [
    1] => 114
                
    [2] => 16
                
    [3] => 0
                
    [4] => 0
                
    [5] => 0
                
    [6] => 0
                
    [7] => 0
                
    [8] => 0
                
    [9] => 0
                
    [10] => 0
                
    [11] => 0
                
    [12] => 0
            
    )

    )
    ------------------------------------------------
    0
    INSERT INTO order_table 
    (idcompany_namedelivery_addressorder_datedelivery_datestatushb_checkedloaded_bypalletsextra_blocksextra_palletstotal_palletsVALUES (NULL,'NACA','Stuart Park','2013-04-20','2013-05-22','Completed','Yes','Me','4','130','1','5')

    INSERT INTO order_details_table (idorder_id,block_type,block_quantity,block_pallets,block_extrasVALUES (NULL,'0','10.01','654','3','114')

    Could not insert data 
    My sql statement gives me this error:
    #1452 - Cannot add or update a child row: a foreign key constraint fails (`bots`.`order_details_table`, CONSTRAINT `order_details_table_ibfk_1` FOREIGN KEY (`order_id`) REFERENCES `order_table` (`id`) ON DELETE CASCADE ON UPDATE CASCADE)

  10. #35
    SitePoint Enthusiast
    Join Date
    Apr 2013
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    when creating the relationship, how should I setup UPDATE and DELETE options?

  11. #36
    SitePoint Enthusiast
    Join Date
    Apr 2013
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK I'm not going to create a relationship.

    I could just get the last id from the order_table, and hardcode insert it into the order_id column of the order_details_table

    How do I get the last id of a table??? tried a few google searches with no luck

  12. #37
    SitePoint Enthusiast
    Join Date
    Apr 2013
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    GOT IT


    <?php
    include('db.php'); //connect

    echo '<pre style="text-align: left;">' . print_r($_POST, true) . '</pre>';

    mysql_query("INSERT INTO order_table (company_name, delivery_address, order_date, delivery_date, status, hb_checked, loaded_by, pallets, extra_blocks, extra_pallets, total_pallets)
    VALUES
    ('$_POST[companyname]','$_POST[deliveryaddress]','$_POST[orderdate]','$_POST[deliverydate]','$_POST[status]','$_POST[checked]','$_POST[loadedby]','$_POST[sumpallets]','$_POST[sumextras]','$_POST[makeup]','$_POST[grandtotal]')");

    $currentid = mysql_insert_id();

    foreach($_POST['100type'] AS $key=>$value) {
    $sql = "INSERT INTO order_details_table
    (order_id, block_type, block_quantity, block_pallets, block_extras)
    VALUES ('$currentid', '{$_POST['100type'][$key]}','{$_POST['100quantity'][$key]}','{$_POST['100pallets'][$key]}','{$_POST['100extras'][$key]}')";

    $result = mysql_query($sql) or die('Could not insert data');
    }

    ?>

  13. #38
    SitePoint Enthusiast rajeev13's Avatar
    Join Date
    Nov 2012
    Location
    New Delhi,India
    Posts
    35
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ROUBOS View Post
    GOT IT


    <?php
    include('db.php'); //connect

    echo '<pre style="text-align: left;">' . print_r($_POST, true) . '</pre>';

    mysql_query("INSERT INTO order_table (company_name, delivery_address, order_date, delivery_date, status, hb_checked, loaded_by, pallets, extra_blocks, extra_pallets, total_pallets)
    VALUES
    ('$_POST[companyname]','$_POST[deliveryaddress]','$_POST[orderdate]','$_POST[deliverydate]','$_POST[status]','$_POST[checked]','$_POST[loadedby]','$_POST[sumpallets]','$_POST[sumextras]','$_POST[makeup]','$_POST[grandtotal]')");

    $currentid = mysql_insert_id();

    foreach($_POST['100type'] AS $key=>$value) {
    $sql = "INSERT INTO order_details_table
    (order_id, block_type, block_quantity, block_pallets, block_extras)
    VALUES ('$currentid', '{$_POST['100type'][$key]}','{$_POST['100quantity'][$key]}','{$_POST['100pallets'][$key]}','{$_POST['100extras'][$key]}')";

    $result = mysql_query($sql) or die('Could not insert data');
    }

    ?>
    that is the spirit of learning.

  14. #39
    SitePoint Enthusiast
    Join Date
    Apr 2013
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)


    thanks for all the help


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •