Jump to content

[SOLVED] Insert form data into multiple tables


Nothingman0

Recommended Posts

I'm attempting to insert form data into multiple tables.  The user is already logged in and they are going to create a league.  The form asks for the league name, league password, and number of teams.

 

When the user submits the form I would like it to enter the league name, league password, and number of teams into the leagues table.  I would also like it to add the league name to a column in that users user table.  This is what I have so far.

 

<?php
session_start(); 
include("database.php");
include("loggedin.php");

if (!$conn)
  {
  die('Could not connect: ' . mysql_error());
  }mysql_select_db("my_db", $conn);
  
  $sql="INSERT INTO leagues VALUES
('$_POST[leaguename]','$_POST[leaguepass]', 'private', '$_POST[teams]', '$_SESSION[username]')";

if (!mysql_query($sql,$conn))
  {
  die('Error: ' . mysql_error());
  }
echo "<meta http-equiv=\"refresh\" content=\"0;URL=loggedin.php\">";
mysql_close($conn)
?> 

 

This does a great job of inserting the data into the leagues table.  Now I also need it to enter leaguename into the user's column named fbleague in the table named users.

 

I'm thinking it needs to be something like;

 

"INSERT INTO users (fbleague) WHERE username=$username VALUES

('$_POST[leaguename]')";

 

But I know that doesn't work.  I think I need to setup a $result variable but I just can't quite seem to figure this out.  It actually has been a lot of fun trying to figure this out but after hours of tinkering with it and another hour searching google I'm coming up empty handed.  Any help would be greatly appreciated.

 

 

As the record already exists, you need an update query

 

"UPDATE users SET fbleague = '{$_POST["leaguename"]}' WHERE username='$username' "

 

Thanks for the quick response.  That looks like it will do it for me but I'm not sure where to enter it in my code.

I tried to insert it directly below the first $sql but that didn't do the trick.  It seems to have cancelled out the first $sql where it enters the data into the leagues table and it doesn't insert leaguename in the correct place.  Nothing was inserted into the leagues table.  In the users table it created a new row with nothing in the username column and every other column null except the fbleague column which did have the correct leaguename.

 

<?php
session_start(); 
include("database.php");
include("loggedin.php");


if (!$conn)
  {
  die('Could not connect: ' . mysql_error());
  }mysql_select_db("my_db", $conn);
  
  $sql="INSERT INTO leagues 
VALUES
('$_POST[leaguename]','$_POST[leaguepass]','private', '$_POST[teams]', '$_SESSION[username]')";

  $sql="UPDATE users SET fbleague = '{$_POST["leaguename"]}' WHERE username='$username'";


if (!mysql_query($sql,$conn))
  {
  die('Error: ' . mysql_error());
  }
echo "<meta http-equiv=\"refresh\" content=\"0;URL=loggedin.php\">";
mysql_close($conn)
?> 

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.