Board index   FAQ   Search  
Register  Login
Board index php forum :: Database mySQL & php coding

How to connect one form to two db

Codes here !

Moderators: macek, egami, gesf

How to connect one form to two db

Postby diipora » Mon Apr 30, 2012 6:51 am

Hi,

I have a registration form, which gets data from a database and after user submitted the form data stores into another database.

Here you can see my form:

Image

Date and time are data, which come from "datetime"table on "calendar" database. And I have put times as checkboxs and I want after being selected once, they become unselectables or unavailbles! Data will be stored on For this I have this code:

Code: Select all
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("calendar", $con);

$result = mysql_query("SELECT * FROM datetime  ORDER BY id DESC LIMIT 1");

while($row = mysql_fetch_array($result))
  {

[b][i]// for each checkbox[/i][/b]
 echo"
<div  class='box'>
<div class='boxbut'>
if($_POST['datetime'] != ($row['day1'] . "   " . $row['time1'])) {
    echo"
    <input type='radio' name='datetime' value='". $row['day1']."&nbsp;&nbsp;&nbsp;".$row['time1']."' disabled>
    <p>". $row['day1'] ,"</p><br/>". $row['time1'] ."
    // etc
} else {
    echo"
    <input type='radio' name='datetime' value='". $row['day1']."&nbsp;&nbsp;&nbsp;".$row['time1']."'>
    <p>". $row['day1'] ,"</p><br/>". $row['time1'] ."
}

</div>
</div>";


This code is ok, but for making selected checkbox unavailble I have connect this form also to other table on other dabase, where data will be stored. My table name's for this is "students" on "college" database.
In the students I have also "datetime" row.

After I fill form and chose one checkbox and submit it, I got this error. Because form does'nt have connection to validate if data is already recorded:

datetime in C:\xampp\htdocs\form.php on line 98 and here is line 98

Question is how can I connect this form to those two database?
Last edited by diipora on Tue May 01, 2012 7:40 am, edited 1 time in total.
diipora
New php-forum User
New php-forum User
 
Posts: 11
Joined: Mon Apr 30, 2012 6:14 am

Re: How to connect one form to two db

Postby minimihi » Mon Apr 30, 2012 7:57 am

If you want to connect to different databases on same server with different accounts. You can do it like this
Code: Select all
$db1 = mysql_connect($server, $db_username, $db_password);
$db2 = mysql_connect($server, $db_username2, $db_password2);
mysql_select_db('calendar', $db1);
mysql_select_db('college', $db2);

$query = "SELECT * FROM datetime";
mysql_query($query, $db1);
$query2 = "SELECT * FROM students";
mysql_query($query, $db2); 


If account has rights on both databases, all you need is to add database name next to table name
Code: Select all
SELECT * FROM calendar.datetime;
SELECT * FROM college.students; 


As I see you're using root account, then second option fits better
Last edited by minimihi on Tue May 01, 2012 2:59 am, edited 1 time in total.
User avatar
minimihi
New php-forum User
New php-forum User
 
Posts: 238
Joined: Sat Apr 14, 2012 11:57 am
Location: Vilnius, Lithuania

Re: How to connect one form to two db

Postby diipora » Mon Apr 30, 2012 5:30 pm

Unfortunately doesn't work. Is this correct?

Code: Select all
<?php
$con = mysql_connect("localhost","root","");




    $query = "SELECT * FROM datetime";
    mysql_query($query, $con);
   
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("calendar", $con);
mysql_select_db('college', $con);


$result = mysql_query("SELECT * FROM datetime  ORDER BY id DESC LIMIT 1");
$result2 = mysql_query("SELECT * FROM students");
while($row = mysql_fetch_array($result)&&($result2))
  {
   


echo"<div class='month' align='center'><input type='hidden' name='month' value='".$row['month']."' ><l>".$row['month']."</l></div>";



 echo"
<div  class='box'>
<div class='boxbut'>
<input type='radio' name='datetime' value='". $row['day1']."&nbsp;&nbsp;&nbsp;".$row['time1']."'>
<d>". $row['day1'] ,"</d><br/><t>". $row['time1'] ."</t>
</div>
</div>";
   
diipora
New php-forum User
New php-forum User
 
Posts: 11
Joined: Mon Apr 30, 2012 6:14 am

Re: How to connect one form to two db

Postby minimihi » Tue May 01, 2012 3:09 am

It seem you didn't even read what I wrote. Only copied parts of code and hoped for it to work.

See what this code does, inspect it and adapt to your code whatever you have learned from it.
Code: Select all
<?php
$con 
= mysql_connect("localhost","root","");    
if 
(!$con) {
    die('Could not connect: ' . mysql_error());
}

echo "<pre>";
// selecting and printing 'datetime' table from 'calendar' database
$result = mysql_query("SELECT * FROM calendar.datetime");
while(
$row = mysql_fetch_array($result)) {
   var_dump($row);
}

// selecting and printing 'students' table from 'college' database
$result2 = mysql_query("SELECT * FROM college.students"); 
while($row2 = mysql_fetch_array($result2)) {
    var_dump($row2);
}
echo "</pre>";
?>
User avatar
minimihi
New php-forum User
New php-forum User
 
Posts: 238
Joined: Sat Apr 14, 2012 11:57 am
Location: Vilnius, Lithuania

Re: How to connect one form to two db

Postby diipora » Tue May 01, 2012 7:37 am

Code: Select all
// selecting and printing 'students' table from 'college' database
$result2 = mysql_query("SELECT * FROM college.students"); 
while($row2 = mysql_fetch_array($result2)) {
    var_dump($row2);
}
echo "</pre>";
?>


Thank you for you help, but I don't want to print data from students table on college database,I just want to make connection to the database, and make query if datetime exists on the students table, the code makes checkbox unselectable!

I have this code and because form is not connected to database it doesn't recognize $v_datetime, which stores data on datetime on students table.

this is my code for each checkbox:
Code: Select all
echo"
<div  class='box'>
<div class='boxbut'>
<input type='radio' name='datetime' value='". $row['day1']."&nbsp;&nbsp;&nbsp;".$row['time1']."' " . ( $v_datetime == "$row[day1]   $row[time1]" ? "checked" : "" ) . ">
<d>". $row['day1'] ,"</d><br/><t>". $row['time1'] ."</t>
</div>
</div>";


and this is my students table on college database


Image
diipora
New php-forum User
New php-forum User
 
Posts: 11
Joined: Mon Apr 30, 2012 6:14 am


Return to mySQL & php coding

Who is online

Users browsing this forum: No registered users and 2 guests

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

cron