Jump to content

Is one SUBMIT into multiple tables possible?


nomadsoul

Recommended Posts

:confused:

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.

 

 

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

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

 

 

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.