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.

    i'm sorry the second pic was wrong so i uploaded the right one. here it is:

    [ATTACH]4490[/ATTACH]

    IamME;10998876 wrote:

    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.

      I have only a passing familiarity with database CONSTRAINT definitions, but it sounds to me like the person who defined your tables established constraints which say that you can't insert a record in order_info unless its cust_id value matches some record in customer_info.

      Sounds to me like your SQL statement is not properly getting the current cust_id into it. Also, I see that you are cramming $_POST values directly into your SQL statement without first escaping them or filtering them. This is a pretty bad idea because it means your script will be vulnerable to SQL Injection (Q.v.).

        Write a Reply...