Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am having trouble getting an sql query to go through to the database. I am checking this with an if ($sql) {echo "success";} else {echo "error";} and I keep getting "error". The code preceding my query all seems to work. If the query syntax is correct, what could be potential trip-ups that I have overlooked? Here is the code in question:

$sql = mysql_query(
    "INSERT INTO monthly_balances (
        bal_id,
        account_id,
        bal_date,

        bal_month_sales,
        bal_month_returns,
        bal_month_net,
        bal_month_coop,

        bal_YTD_sales,
        bal_YTD_returns,
        bal_YTD_net,

        bal_lastYTD_sales,
        bal_lastYTD_returns,
        bal_lastYTD_net
    ) 
    VALUES (
        DEFAULT,
        '$account_id',
        '$bal_date',

        '$bal_month_sales',
        '$bal_month_returns',
        '$bal_month_net',
        '$bal_month_coop',

        '$bal_YTD_sales',
        '$bal_YTD_returns',
        '$bal_YTD_net',

        '$bal_lastYTD_sales',
        '$bal_lastYTD_returns',
        '$bal_lastYTD_net'
    )
 ");


if($sql) {
    echo 'success';
}
else {
    echo 'error';
}

Thank you

share|improve this question
2  
Replace error with mysql_error() –  I Can Has Kittenz Apr 15 '14 at 17:22
    
did you try echoing the error. also please update your code to use mysqli or pdo! –  Logan Murphy Apr 15 '14 at 17:22
    
I have echoed the error. It reads: Cannot add or update a child row: a foreign key constraint fails (coop.monthly_balances, CONSTRAINT monthly_balances_ibfk_1` FOREIGN KEY (account_id) REFERENCES entries (account_id))` –  bsapaka Apr 15 '14 at 17:28

1 Answer 1

up vote 2 down vote accepted

From the docs:

MySQL rejects any INSERT or UPDATE operation that attempts to create a foreign key value in a child table if there is no a matching candidate key value in the parent table.

So basically, the value of account_id that you are entering in monthly_balances is not present in account_id of table entries.

When a column is declared as foreign key in reference to some column in another table, the foreign column cannot contain a value before the parent column has it.

Note:

Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

share|improve this answer
    
What you are saying makes sense to me, however, I have set the foreign key to account_id of table accounts in mysql, which exists. So how is this possible? –  bsapaka Apr 15 '14 at 17:46
1  
@bsapaka From the error it seems to be referring to the entries table. You should probably re-check it. –  I Can Has Kittenz Apr 15 '14 at 17:48
    
I did recheck it. Turns out I have dislexia Thanks for your help. –  bsapaka Apr 15 '14 at 17:53
    
@bsapaka Aw sorry to hear that, and you're welcome. –  I Can Has Kittenz Apr 15 '14 at 17:56

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.