Insert multiple rows into one table

A

Anonymous

Guest
I have searched for this one for ages before resorting to ask for help!

I have two tables
Members and Categories

I want members to insert their data into table.members and up to 4 categories into table.categories

I have no problem with the first insert. The second insert though always show the last category. ie. If member types math, physics, chemistry, here is what I get

Name of member, chemistry
Name of member, chemistry

Here is the code:

$table = "categories";

$sql = "SELECT id, user FROM $mysql_cat WHERE user = '$user'";
$result = mysql_query($sql);

while ( $row = mysql_fetch_array( $result ) ) {

$user = $row["user"];
$category = $row["category"];

$table = "categories";

mysql_query("INSERT INTO $table (id, user, category) VALUES ('','$user', '$category')");
}
?>

<form action=add-multiple.php?l=<?php echo $l; ?>&page=add method=post enctype="multipart/form-data"><center>
<div align="center"> </div>
<Table width=719 Border="1" align="center" CellPadding="4" CellSpacing="0" bordercolor=black bgcolor=<?php echo $color3; ?>>

</tr>
<td><input class=input type=text name=category></td>
</tr>
</tr>
<td><input class=input type=text name=category></td>
</tr>

<input class=input type=submit value="Submit">
</table>
</form>
 
I don't see it clear at all...
I don't understand part of your code..
But it looks like you are using the same name in the input textfield

Code:
</tr> 
<td><input class=input type=text name=category></td> 
</tr> 
</tr> 
<td><input class=input type=text name=category></td> 
</tr>

I've done this for you, (there are a couple of things which I think they are useless, but I tried to keep your structure just in case you need it for anything)
I'm only using one file.
It sends the values from the three textfields, the user and the mysql_cat to himself and then updates categories one by one to the given user.

Code:
<?php
if (isset($HTTP_GET_VARS['submit'])){
	$sql = "SELECT id, user FROM ".$HTTP_GET_VARS['mysql_cat']." WHERE user = ".$HTTP_GET_VARS['user']; 
	$result = mysql_query($sql); 
	while ( $row = mysql_fetch_array( $result ) ) { 
			for($i=0;$i<3;$i++){
				mysql_query("INSERT INTO categories (id, user, category) VALUES ('',".$row["user"].", ".$HTTP_GET_VARS['category'.$i].")"); 
			}
		} 
	}
}else{
?> 
	<form action= <?php $php_self ?>>

	<table>
	<tr> 
		<td><input type=text name=category0></td> 
	</tr> 
	<tr> 
		<td><input type=text name=category1></td> 
	</tr> 
	<tr> 
		<td><input type=text name=category2></td> 
	</tr> 
	<tr> 
		<td><input type=hidden name=mysql_cat value=chemistry></td> 
	</tr> 
	<tr> 
		<td><input type=hidden name=user value=Joahn></td> 
	</tr> 
			<input type=submit value="Submit"> 
	</table> 

	</form>
<?php
}//end of else
?>

I hope it helps
;)
 
Back
Top