Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

Insert multiple rows into one table

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

Insert multiple rows into one table

Postby Jacques » Tue Nov 19, 2002 12:12 pm

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>
Jacques
New php-forum User
New php-forum User
 
Posts: 10
Joined: Fri Oct 18, 2002 8:07 am

Postby Joan Garnet » Mon Nov 25, 2002 5:24 pm

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: Select all
</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: Select all
<?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
;)
User avatar
Joan Garnet
Moderator
Moderator
 
Posts: 387
Joined: Sat Aug 03, 2002 2:56 am
Location: Mars


Return to PHP coding => General

Who is online

Users browsing this forum: Google [Bot] and 3 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.