Jump to content

inserting records php/mysql


yankeesjtj

Recommended Posts

Hello, I'm trying to create a simple form that will allow me to insert  records into a data base. There are 3 fields. Date, Customer name and number of purchase orders  What i have so far works for the first time. But the insert fails when you try to insert a second set of records

 

Here is my code. Thanks for the help

 

<html>
<head>
<title>Insert PO's</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<h1 align="center">EDI PO's - Add a record</h1>

<?php

if(isset($_POST['insert']))
{
include 'library/po_config.php';
include 'library/opendb.php';
$curr_date = $_REQUEST["curr_date"];
$custname = $_REQUEST["custname"];
$po_quant = $_REQUEST["po_quant"];

$query = "INSERT INTO purch_orders (date, customer_name, po_quantity) VALUES ('$curr_date', '$custname', '$po_quant')";
mysql_query($query) or die('Error, insert query failed');

$query = "FLUSH PRIVILEGES";
mysql_query($query) or die('Error, insert query failed'); 

include 'library/closedb.php';
echo "Data Added";
$curr_date = trim('curr_date');
$custname = trim('custname');
$po_quant = trim('po_quant');

echo <<<HERE
<br>
<form method="post">
<input name="another" type="submit" id="another" value="Insert Another Record">
</form>
HERE;

}
else
{ 
?>
<form method="post">
<table align = "center" width="400" border="0" cellspacing="1" cellpadding="2">
<tr> 
<td width="100">Date (yyyy-mm-dd) : </td>
<td><input name="curr_date" type="text" id="curr_date"></td>
</tr>
<tr> 
<td width="100">Customer Name</td>

<td><select name="custname">
<option value="Wallys Wallets">Wally's Wallets</option>
<option value="Franks Fish Farm">Franks Fish Farm</option>
<option value="Sals Smoke Shop">Sal's Smoke Shop</option>
<option value="Leroys Leather Goods">Leroys Leather Goods</option>
<option value="Carls Computers">Carl's Computers</option></td>
</tr>
<tr> 
<td width="100">PO Quantity</td>
<td><input name="po_quant" type="text" id="po_quant"></td>
</tr>
<tr> 
<td width="100"> </td>
<td><input name="insert" type="submit" id="add" value="Insert record"></td>
</tr>
</table>
</form>
<?php
} 
?> 
</body>
</html>

 

Jason

Link to comment
https://forums.phpfreaks.com/topic/95901-inserting-records-phpmysql/
Share on other sites

<html>
<head>
<title>Insert PO's</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<h1 align="center">EDI PO's - Add a record</h1>

<?php

if(isset($_POST['insert']))
{
include 'library/po_config.php';
include 'library/opendb.php';
$curr_date = $_REQUEST["curr_date"];
$custname = $_REQUEST["custname"];
$po_quant = $_REQUEST["po_quant"];

$query = "INSERT INTO purch_orders (date, customer_name, po_quantity) VALUES ('$curr_date', '$custname', '$po_quant')";
mysql_query($query) or die("Error in query: $query. ".mysql_error());

$query = "FLUSH PRIVILEGES";
mysql_query($query) or die("Error in query: $query. ".mysql_error()); 

include 'library/closedb.php';
echo "Data Added";
$curr_date = trim('curr_date');
$custname = trim('custname');
$po_quant = trim('po_quant');

echo <<<HERE
<br>
<form method="post">
<input name="another" type="submit" id="another" value="Insert Another Record">
</form>
HERE;

}
else
{ 
?>
<form method="post">
<table align = "center" width="400" border="0" cellspacing="1" cellpadding="2">
<tr> 
<td width="100">Date (yyyy-mm-dd) : </td>
<td><input name="curr_date" type="text" id="curr_date"></td>
</tr>
<tr> 
<td width="100">Customer Name</td>

<td><select name="custname">
<option value="Wallys Wallets">Wally's Wallets</option>
<option value="Franks Fish Farm">Franks Fish Farm</option>
<option value="Sals Smoke Shop">Sal's Smoke Shop</option>
<option value="Leroys Leather Goods">Leroys Leather Goods</option>
<option value="Carls Computers">Carl's Computers</option></td>
</tr>
<tr> 
<td width="100">PO Quantity</td>
<td><input name="po_quant" type="text" id="po_quant"></td>
</tr>
<tr> 
<td width="100"> </td>
<td><input name="insert" type="submit" id="add" value="Insert record"></td>
</tr>
</table>
</form>
<?php
} 
?> 
</body>
</html>

 

Try that and tell us what they error says.  I changed the error to add they mysql error

replace "error, insert query failed with mysql_error() and it will give us a better idea, use that from now on too.

 

Error in query: INSERT INTO purch_orders (date, customer_name, po_quantity) VALUES ('2008-02-24', 'Franks Fish Farm', '34'). Duplicate entry '2008-02-24' for key 1

 

it seems the only way right now it doesn't give me this duplicat error message is if i clear my browser cache before entering the 2nd set...

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.