Page 1 of 1

Text box with multiple records

Posted: Fri Sep 03, 2010 2:33 am
by SpiritWebb
I was wondering if anyone can point me in the right direction. I wanted to create a text box in php where you can input data, separated by commas and the script creates a record for each one.

For example:

I scan multiple bar codes, and in the text box each bar code is its own line. Now when I move to the drop down box it auto separates each with a comma, then in the drop down I can choose the location of where it is, and when I click the "add" button it then creates a new record for each new bar code added to the database. That way, when updating the bar code its "location" can be changed.

Any idea on how to do this? I am lost...

Thanks in advance!

Re: Text box with multiple records

Posted: Fri Sep 03, 2010 12:27 pm
by Chris
the explode() function is what you need.
http://php.net/explode

Here's sort of what your code would look like

Code: Select all

foreach( explode(",", $_POST['textbox']) as $key => $value )
{
    echo $key ." ". $value . "<br />";
}
 

Re: Text box with multiple records

Posted: Tue Sep 14, 2010 1:44 am
by SpiritWebb
I was wondering if this will work?

Code: Select all

# <?php
# // This is the string, delimited by a comma, rather than a new-line.
# $rawString = "piece1,piece2,piece3";
#  
# // Split the string into pieces
# $pieces = explode(",", $rawString);
#  
# // Build the top of the INSERT query
# $sql = "INSERT INTO `myTable`(`myField`) VALUES\n";
#  
# // Build the rest of the INSERT query by re-assembling the
# // pieces.
# $sql .= "('";
# $sql .= implode("'), ('", $pieces);
# $sql .= "')"; 
# ?>
Then the INSERT INTO should look like:

Code: Select all

# INSERT INTO `myTable`(`myField`) VALUES
# ('piece1'), ('piece2'), ('piece3')
I was wondering if this will work? However, if piece1, piece2 and piece3 there in the $rawstring am I only limited to 3 lines in the multiline text box? Due to what we do at work, I would need at least a 200 line limit. When adding data to the database...

Then when the info is added, how can I use the multi-line textbox to do a search on the database and update the record using a drop down box? Please advise...I am needing to get this done as quickly as possible...

Re: Text box with multiple records

Posted: Sat Sep 25, 2010 3:25 am
by Lord Strife
might work ;)

Code: Select all


<?php
if(isset($_POST['submit']) AND $_POST['textarea'] != '' AND $_POST['location'] != ''){
	$info  = $_POST['textarea'];
	$location = $_POST['location'];
	$tapes = explode("\n", $info);
	/*foreach ($tapes as $tapenumber) {
		$sql = "INSERT INTO tapes(id, tapenumber, location) VALUES('',$tapenumber,$location)";
	}*/
	foreach ($tapes as $tapenumber) {
		echo $tapenumber;
		echo "<br/>";
	}
	echo $location;
}
else{
?>
<form action="test.php" method="post">
<textarea name="textarea" col="30" rows="10"></textarea>
<br />
<select name="location">
<option value="usa">Usa</option>
<option value="mexico">Mexico</option>
</select>
<br />
<input type="submit" name="submit" value="TEST">
</form>
<?php
}
?> 


Re: Text box with multiple records

Posted: Sat Sep 25, 2010 4:29 am
by SpiritWebb
What I have now:

Code: Select all

    <center>
    <h1><p><center>Conway ODC TMS - New Tapes</center></p></h1> 
<?php
include 'connect.php';
?>

<?php
if(isset($_POST['submit']) AND $_POST['textarea'] != '' AND $_POST['location'] != ''){
   $info  = $_POST['textarea'];
   $location = $_POST['location'];
   $tapes = explode("\n", $info);
   foreach ($tapes as $tapenumber) {
      $sql = "INSERT INTO tapes(id, tapenumber, location) VALUES('',$tapenumber,$location)";
      mysql_query($sql) or die("could not update database" . mysql_error());
   }

}
else{
?>
<form action="tapeenter.php" method="post">
<textarea name="textarea" col="30" rows="10"></textarea>
<br />
Tape Location:
<select name="location">
<option selected>
<option>tape drive cwy
<option>tape drive lr
<option>Tape Storage Room
</select>
<br />
<input type="submit" name="submit" value="TEST">

</form>
<?php
}
?>

Re: Text box with multiple records

Posted: Sat Sep 25, 2010 4:39 am
by SpiritWebb
Thanks to Lord Strife, it is FULLY FUNCTIONAL now...WOOT! Thank you so much! Life saver!

Re: Text box with multiple records

Posted: Sat Sep 25, 2010 4:41 am
by Lord Strife
no problem , anytime

Re: Text box with multiple records

Posted: Tue Sep 28, 2010 7:52 pm
by SpiritWebb
The goal at which I am trying to accomplish is a fully working tape management system. Where basically you use the database to do the following:

Add new tapes (tape number, location, library)
Update tapes (keep the tape number and library the same, but change the location)
Search tapes (Be able to use a field to put x amount of tape numbers and do a search. Show the tape numbers and location of said tape(s))
Delete tapes (Be able to put a tape number(s) in a delete the volumes from the database - admin function)

And a few others, but that's what I am trying to fully accomplish. Then being able to upgrade it to do library searches to show who the customer is, the DBA's, their location, username/password if any, etc.


Thanks to Lord Strife, the Add new tapes function is working successfully, however the updating tapes doesn't work properly. Search and Delete isn't built yet.