hey guys! i'm a newbie here. And i know only a few about php connecting to database mysql. This is now my problem. I want to input new record to my database using php. Unfortunately, i have foreign key which generate my code i think to create a problem.
This is where i want to add my new record:
[ATTACH]4488[/ATTACH]
cust_id here is the foreign key.
This is where i input/select my new record:
[ATTACH]4489[/ATTACH]
Now, this is my code for inputing:
<div id="panel">
<form action="insert.php" method="post">
<table id="contable">
<tr><th colspan="2">Add an order</th></tr>
<tr><td colspan="2">You will be contacted by an employee to confirm your order.</td></tr>
<tr><td colspan="2">Editing is allowed <u>only within 24 hours</u> after initial input.</td></tr>
<tr><td colspan="2">Once confirmed and finalized, you are advised to visit the office for down payment.</td></tr>
<tr><td colspan="2"><hr/></td></tr>
<tr><td>Product and Quantity</td><td><input type="radio" value="Slimfit Shirt" name="prod"/>Slimfit Shirt<select style="float:right" name="qty"><option value="30">30</option><option value="50">50</option><option>75</option><option value="100">100</option><option value="200">200+</option></select><br/>
<input type="radio" value="T-Shirt" name="prod"/>T-shirt <select style="float:right" name="qty"><option value="30">30</option><option value="50">50</option><option>75</option><option value="100">100</option><option value="200">200+</option></select><br/>
<input type="radio" value="Polo Shirt" name="prod"/>Polo Shirt <select style="float:right" name="qty"><option>30</option><option>50</option><option>75</option><option>100</option><option>200+</option></select><br/>
<input type="radio" value="Baller" name="prod"/>Baller <select style="float:right" name="qty"><option value="500">500</option><option value="1000">1000</option></select><br/>
<input type="radio" value="Lanyard" name="prod"/>Lanyard <select style="float:right" name="qty"><option value="100">100</option><option value="1000">1000</option></select><br/>
</td></tr>
<tr><td>Custom Design<br/><small><small>(If no design is uploaded, we will create the design for you.)</small></small></td><td><input type="file"/></td></tr>
<tr><td><input type="submit" value="Submit"/></td></tr>
</table>
</form>
</div>
This is the code for putting the add record to the database(insert.php):
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("new", $con);
$sql="INSERT INTO order_info(prod_type,prod_qty,cust_id)
VALUES
('$POST[prod]','$POST[qty]','$_POST[$sql]')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record added";
mysql_close($con);
?>
This is the result:
Notice: Undefined variable: sql in C:\xampp\htdocs\UI-alternativ\insert.php on line 12
Notice: Undefined index: in C:\xampp\htdocs\UI-alternativ\insert.php on line 12
Error: Cannot add or update a child row: a foreign key constraint fails (new
.order_info
, CONSTRAINT order_info_ibfk_4
FOREIGN KEY (cust_id
) REFERENCES customer_info
(cust_id
))
How will I solve this problem? can you help me please? I really need it for my database project. Hope you can answer my problem.