nomadsoul Posted April 1, 2011 Share Posted April 1, 2011 I'm not sure if this belongs in the PHP or SQL sections or both but- Is it possible to insert form data (one record) into 2 different tables when I click submit? As is, I have 2 distinct log-in scripts each linked to 2 separate databases . I want to merge them into one log-in form that populates both tables(in different dbs). I don't want my visitors to log into 2 different forms. Can one form's action attribute send the POST data to two different processing scripts? and can the input from a text box be sent to 2 different fields? I don't have any code, I just wanted to know if it can be done or point me to a sample that I can work from. Quote Link to comment https://forums.phpfreaks.com/topic/232382-is-one-submit-into-multiple-tables-possible/ Share on other sites More sharing options...
j9sjam3 Posted April 1, 2011 Share Posted April 1, 2011 So you want to have one form to interact with two different databases? <?php if(isset($_POST['submit'])) { $field1 = $_POST['field1']; $field2 = $_POST['field2']; // etc $con1 = mysql_connect("localhost", "root", "password", "db1") or die(mysql_error()); $con2 = mysql_connect("somewhere", "root", "password", "db2") or die(mysql_error()); mysql_query("INSERT INTO one (field1, field2) VALUES ($field1, $field2)", $con1) or die(mysql_error($con1)); mysql_query("INSERT INTO two (field1, field2) VALUES ($field1, $field2)", $con2) or die(mysql_error($con2)); } Remember always to clean your inputs Quote Link to comment https://forums.phpfreaks.com/topic/232382-is-one-submit-into-multiple-tables-possible/#findComment-1195431 Share on other sites More sharing options...
nomadsoul Posted April 1, 2011 Author Share Posted April 1, 2011 Yes, thanks. And I was wondering if I can do this with the form: <form action="mysql_insert.php" method="POST"> Firstname: <input type="text" name='field1','field2' /> <input type="submit" /> </form> Seems like this should be possible. -Your script being mysql_insert.php i will try it out later Quote Link to comment https://forums.phpfreaks.com/topic/232382-is-one-submit-into-multiple-tables-possible/#findComment-1195451 Share on other sites More sharing options...
j9sjam3 Posted April 1, 2011 Share Posted April 1, 2011 If that doesn't work, then have hidden fields. onSubmit update the hidden fields to the values you want them. Quote Link to comment https://forums.phpfreaks.com/topic/232382-is-one-submit-into-multiple-tables-possible/#findComment-1195452 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.