MySQL using interesting set of arrays...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Jim
Forum Contributor
Posts: 238
Joined: Fri Apr 19, 2002 5:26 am
Location: Near Austin, Texas

MySQL using interesting set of arrays...

Post by Jim »

Hey all. I'm trying to use a for loop and some arrays to pass a mysql_query for multiple like form-fields.

Here's what I'm doing:

Code: Select all

<?php

class Units 
{

	function Units() 
	{
	
		$this->game = "empires";
		$unit_connect = mysql_connect ("localhost" , "Jim" , "b6514");
		$unit_select = mysql_select_db ("content" , $unit_connect);
	
	}
	
	
	function report()
	{
	
		echo "<form method="post" action="index.php?f=import_units">";
	
		echo "<table cellpadding="0" cellspacing="0" border="0" align="center">";
		echo "<tr><td><b>First Unit Name</b></td><td><input type="text" name="unit_name['0']"></td></tr>";
		echo "<tr><td><b>Unit Description:</b></td><td><textarea name="unit_description['0']"></textarea></td></tr>";
		echo "<tr><td><b>Unit Image:</b></td><td><input type="file" name="unit_image['0']"></td></tr>";
		echo "</table>";
		
		echo "<BR><BR><BR>";
		
		echo "<table cellpadding="0" cellspacing="0" border="0" align="center">";
		echo "<tr><td><b>First Unit Name</b></td><td><input type="text" name="unit_name['1']"></td></tr>";
		echo "<tr><td><b>Unit Description:</b></td><td><textarea name="unit_description['1']"></textarea></td></tr>";
		echo "<tr><td><b>Unit Description:</b></td><td><input type="file" name="unit_image['1']"></td></tr>";		
		echo "</table>";
		
		echo "<BR><BR><BR>";
		
		echo "<table cellpadding="0" cellspacing="0" border="0" align="center">";
		echo "<tr><td><b>First Unit Name</b></td><td><input type="text" name="unit_name['2']"></td></tr>";
		echo "<tr><td><b>Unit Description:</b></td><td><textarea name="unit_description['2']"></textarea></td></tr>";
		echo "<tr><td><b>Unit Image:</b></td><td><input type="file" name="unit_image['2']"></td></tr>";
		echo "</table>";
		
		echo "<BR><BR><BR>";
		
		echo "<table cellpadding="0" cellspacing="0" border="0" align="center">";
		echo "<tr><td><b>First Unit Name</b></td><td><input type="text" name="unit_name['3']"></td></tr>";
		echo "<tr><td><b>Unit Description:</b></td><td><textarea name="unit_description['3']"></textarea></td></tr>";
		echo "<tr><td><b>Unit Image:</b></td><td><input type="file" name="unit_image['3']"></td></tr>";
		echo "</table>";
		
		echo "<BR><BR><BR>";
		
		echo "<table cellpadding="0" cellspacing="0" border="0" align="center">";
		echo "<tr><td><b>First Unit Name</b></td><td><input type="text" name="unit_name['4']"></td></tr>";
		echo "<tr><td><b>Unit Description:</b></td><td><textarea name="unit_description['4']"></textarea></td></tr>";
		echo "<tr><td><b>Unit Image:</b></td><td><input type="file" name="unit_image['4']"></td></tr>";
		echo "</table>";
		
		echo "<BR><BR><BR>";
		
		echo "<table cellpadding="0" cellspacing="0" border="0" align="center">";
		echo "<tr><td><b>First Unit Name</b></td><td><input type="text" name="unit_name['5']"></td></tr>";
		echo "<tr><td><b>Unit Description:</b></td><td><textarea name="unit_description['5']"></textarea></td></tr>";
		echo "<tr><td><b>Unit Image:</b></td><td><input type="file" name="unit_image['5']"></td></tr>";
		echo "</table>";
		
		echo "<BR><BR><BR>";
		
		echo "<center><input type="submit" value="Add Units">";
		echo "</form>";
	
	}
	
	
	function import()
	{
	
		$n = 0;
		
			for ( $n=0; $n < 5; $n++ )
			{
			
				$unit_name['$n'] = $_POST['unit_name[''$n'']'];
				$unit_description['$n'] = $_POST['unit_description[''$n'']'];
				$unit_image['$n'] = $_POST['unit_image[''$n'']'];
				
				echo $unit_name['$n'];
				
				$sql = mysql_query("insert into ".$this->game."_unit_db ( name , description  ) values ( '$unit_name[$n]' , '$unit_description[$n]' )"); 
					if (!$sql) { echo mysql_error(); }
			}
	
	}

}

?>
I get no errors and the MySQL DB is actually filled... but it's filled with blank cells in the fields.

Any idea how to pass these array values on properly?

Thanks!
[]InTeR[]
Forum Regular
Posts: 416
Joined: Thu Apr 24, 2003 6:51 am
Location: The Netherlands

Post by []InTeR[] »

The code:

Code: Select all

$unit_name['$n'] = $_POST['unit_name[''$n'']']; 
$unit_description['$n'] = $_POST['unit_description[''$n'']']; 
$unit_image['$n'] = $_POST['unit_image[''$n'']'];
Must be:

Code: Select all

$unit_name['$n'] = $_POST['unit_name'][$n]; 
$unit_description['$n'] = $_POST['unit_description'][$n]; 
$unit_image['$n'] = $_POST['unit_image'][$n];
Post Reply