Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Inserting Multiple Records in one Table using PHP Access database 1

Status
Not open for further replies.

bigcat48

Programmer
Aug 27, 2008
72
US
All - I have searched and searched for help in the forums on this website and google and can't seem to find an answer.

What I want to do is insert multiple records in one table using one form with php, odbc, & access db.
The records that I am trying to add will have only one similar field, which will be the "Symbol" field. Let me explain what I mean.

Table fields:
ID, LE, Symbol, RIN

Sample records:
1250, L, 405, 12345
1251, E, 405, 12346
1252, L, 405, 12347
1253, L, 405, 12348
....


Add Rin form
Code:
<div id="oneColumn">
		<h2>Add New RIN</h2>
		
		<p><span class="required">*</span> = Required Fields</p>
		
		<form id="ContactForm" action="insert_rin.php" method="post">
			<label><span class="required">*</span> L/E:</label><input type="text" name="LE" />
			<label><span class="required">*</span> Symbol:</label><input type="text" name="Symbol" />
			<label>RIN:</label>
				<input type="text" name="RIN[]" class="rinL" /><input type="text" name="RIN[]" class="rinR" />
				<input type="text" name="RIN[]" class="rinL1" /><input type="text" name="RIN[]" class="rinR1" />
				<input type="text" name="RIN[]" class="rinL1" /><input type="text" name="RIN[]" class="rinR1" />
				<input type="text" name="RIN[]" class="rinL1" /><input type="text" name="RIN[]" class="rinR1" />
				<input type="text" name="RIN[]" class="rinL1" /><input type="text" name="RIN[]" class="rinR1" />
			<input type="submit" value="Submit" id="submitbutton130" class="button positive" />
		</form>
		
	</div>


InsertRin.php
Code:
<div id="oneColumn">
		<h2>Add RIN</h2>
		<?php
		// create database connection
		$conn=odbc_connect('cf','','');
		if (!$conn)
			{exit("Sorry, could not connect: " . $conn);}
		
		foreach($_POST['RIN'] as $idx => $val)
		{
			$RIN = $val;
			$le = $_POST['LE'][$idx];
			$sym = $_POST['Symbol'][$idx];			
			
			$query = "INSERT INTO tblsymbols_rin (LE, Symbol, RIN) VALUES ('$RIN','$le','$sym')";
			$rs = odbc_exec($conn,$query)
			if(!rs)
				{exit("Error, insert query failed. No record(s) added.");}
		}
		echo "<p>Records added.</p>";
		
		odbc_close($conn)
		?>

		<p><a href="add_rin.php" class="button positive">Add Another RIN</a> 
		<a href="rins.php" class="button positive">Return to RINs List</a></p>
		
	</div>
 
your code looks ok in the abstract. what behaviour are you getting?
 
I reverted to what I had there before. What's happening is that the multiple records are being added but the "RIN" field won't change.

If I type in the following in the text boxes:
LE|SYMBOL|RIN
L, 404, 123456
L, 404, 123457

I view the display pages and it shows this:
LE|SYMBOL|RIN
L, 404, 123456
L, 404, 123456


Here is the code that I am using now:

Code:
<div id="oneColumn">
		<h2>Add RIN</h2>
		<?php
		// create database connection
		$conn=odbc_connect('crewforecast20080804','','');
		if (!$conn)
			{exit("Sorry, could not connect: " . $conn);}
		
		// Insert Record #1
		$sql="INSERT INTO tblsymbols_rin (LE, Symbol, RIN) VALUES ('$_POST[LE]','$_POST[Symbol]','$_POST[RIN]')";
		// run query
		$rs=odbc_exec($conn,$sql);		
		if (!$rs) {exit("Error Occured: No record added");}
		
		// Insert Record #2		
		$sql1="INSERT INTO tblsymbols_rin (LE, Symbol, RIN)	VALUES ('$_POST[LE]','$_POST[Symbol]','$_POST[RIN]')";
		// run query
		$rs1=odbc_exec($conn,$sql1);		
		if (!$rs1) {exit("Error Occured: No record added");}
		
		/*foreach($_POST[RIN] as $idx => $val)
		{
			$RIN = $val;
			$le = $_POST[LE][$idx];
			$sym = $_POST[Symbol][$idx];			
			
			$query = "INSERT INTO tblsymbols_rin (LE, Symbol, RIN) VALUES ('$RIN','$le','$sym')";
			$rs = odbc_exec($conn,$query)
			if(!rs)
				{exit("Error, insert query failed. No record(s) added.");}
		}*/
		echo "<p>Records added.</p>";
		
		odbc_close($conn)
		?>

		<p><a href="add_rin.php" class="button positive">Add Another RIN</a> 
		<a href="rins.php" class="button positive">Return to RINs List</a></p>
		
	</div>


I commented out the "foreach" code because it would only create one record with the word "array" in the "RIN" field on the display page.
Example:
LE|SYMBOL|RIN
L, 404, array

Need help.
 
you need the foreach clause to iterate the text boxes
is not the order of your variables in the values part of your insert query incorrect?

Code:
$le = $_POST['LE'];
$symbol = $_POST['Symbol'];
foreach ($_POST['RIN'] as $val){
 $query = "INSERT INTO tblsymbols_rin (LE, Symbol, RIN) VALUES ('$le','$symbol', '$val')";
 $result = odbc_exec($conn, $query);
 if(!rs){
    exit("Error, insert query failed. No record(s) added.");
  }
}
 
This works great but now what's happening is if I only fill in two boxes out of ten; it would created the two records with the information submitted and it would create eight more records without the "Symbol" field.

Is there anyway were I can not create the other records if it does not contain the "Symbol"?

Please help. Thanks.
 
sure.

Code:
$le = $_POST['LE'];
$symbol = $_POST['Symbol'];
foreach ($_POST['RIN'] as $val){
 [red]if(empty($val)) continue;[/red]
 $query = "INSERT INTO tblsymbols_rin (LE, Symbol, RIN) VALUES ('$le','$symbol', '$val')";
 $result = odbc_exec($conn, $query);
 if(!rs){
    exit("Error, insert query failed. No record(s) added.");
  }
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top