Nothingman0 Posted March 17, 2007 Share Posted March 17, 2007 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. Link to comment https://forums.phpfreaks.com/topic/43165-solved-insert-form-data-into-multiple-tables/ Share on other sites More sharing options...
Barand Posted March 17, 2007 Share Posted March 17, 2007 As the record already exists, you need an update query "UPDATE users SET fbleague = '{$_POST["leaguename"]}' WHERE username='$username' " Link to comment https://forums.phpfreaks.com/topic/43165-solved-insert-form-data-into-multiple-tables/#findComment-209611 Share on other sites More sharing options...
Nothingman0 Posted March 17, 2007 Author Share Posted March 17, 2007 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. Link to comment https://forums.phpfreaks.com/topic/43165-solved-insert-form-data-into-multiple-tables/#findComment-209614 Share on other sites More sharing options...
Nothingman0 Posted March 17, 2007 Author Share Posted March 17, 2007 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) ?> Link to comment https://forums.phpfreaks.com/topic/43165-solved-insert-form-data-into-multiple-tables/#findComment-209620 Share on other sites More sharing options...
Nothingman0 Posted March 17, 2007 Author Share Posted March 17, 2007 Amazingly I have figured it out. Not sure what I was thinking. Thanks for the help with this, Barand! mysql_query("UPDATE users SET fbleague = '{$_POST["leaguename"]}' WHERE username='{$_SESSION[username]}'"); Link to comment https://forums.phpfreaks.com/topic/43165-solved-insert-form-data-into-multiple-tables/#findComment-209625 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.