简体   繁体   中英

INSERT INTO TBL_payments Undefined index and parent and child foreign key

So... this is my code:

$paymentType=$_POST["paymentType"]; // in the first two lines the $_POST data is copied into normal variables to make it easier to handle
$paymentMethod=$_POST["paymentMethod"];
$amount=$_POST["amount"];
$date=$_POST["date"];
$other=$_POST["other"];

$mysqlserver="localhost"; // in the next six lines the connection to the server is set up just like before and the database is chosen
$mysqlusername="jakedean";
$mysqlpassword="jakedean";
$link=mysql_connect($mysqlserver, $mysqlusername, $mysqlpassword) or die ("Error connecting to mysql server: ".mysql_error());

$dbname = 'jakedean';
mysql_select_db($dbname, $link) or die ("Error selecting specified database on mysql server: ".mysql_error());
            // the query which inserts the new data (from the variables) is set up and run

$addPaymentquery="INSERT INTO TBL_payments
    (paymentType, paymentMethod, amount, date, other)
    VALUES 
    ('$paymentType', '$paymentMethod', '$amount', '$date', '$other')";
mysql_query($addPaymentquery) or die("Query to insert new Payment into TBL_payments failed with this error: ".mysql_error());            


echo "<p class=\"thicker2\">You added a new Payment to Person ID:$personId! The Payment information added was:</p><p class=\"bold\">Payment Type:</p> <p class=\"thicker\">$paymentType</p> 
        <p class=\"bold\">Payment Method:</p> <p class=\"thicker\">$paymentMethod</p><p class=\"bold\">Amount Paid:</p> <p class=\"thicker\">$amount</p><p class=\"bold\">Date of Payment:</p> <p class=\"thicker\">$date</p>
        <p class=\"bold\">Additional Comments:</p> <p class=\"thicker\">$other</p>";

The error is a combination of undefined indexes' and a cannot update a child row because I've related the sql tables wrong...

Error message:

错误信息

I don't know if this helps, but inside my database there are some fields in different tables which have the same name...

I've been trying to fix this for days but I can't figure it out, this is my first database I've built! Appreciate any responses!

Undefined indexes is because of you tries to get unexistent property from an array. DB error (the one with foreign key) is there because your TBL_payments must be related to TBL_people - this means you must set TBL_payments . personId to the id of your current user (the one who is logged in or the one to whom this payment belongs).
UPD:
And, of course, don't use deprecated ones, as someone mentioned in comments!

You need to have already declared the table that the foreign key references, before you can define a foreign key that references it.

Once you declare the second table, you can then declare the first table. Tested here on MySQL 5.5.27.

If you need to bypass the validation, you can do this:

SET FOREIGN_KEY_CHECKS = 0; -- declare tables SET FOREIGN_KEY_CHECKS = 1; http://dev.mysql.com/doc/refman/5.5/en/server-system-variables.html#sysvar_foreign_key_checks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM