By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,521 Members | 1,420 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,521 IT Pros & Developers. It's quick & easy.

connection to two database and query for if data is recorded

P: 2
Hi

I want to make a time reservation system. And for that I have one form, which is connected to two databases on the same host. Form gets data from one(datetime table on calendar db) and after user submitted data stores on another one(is_availble table on college db).

Every thing work good. I use checkboxs as calendar, so each checkbox is one time, and I want that make checkbox unavailble if it is has been selected once!

I use this cods but it doesn't work. Could anyone tell me where is the problem

this is how my form looks like:




And this is my code for the first checkbox:

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. <?php
  4. $con = mysql_connect("localhost","root","");
  5. if (!$con)
  6.   {
  7.   die('Could not connect: ' . mysql_error());
  8.   }
  9.  
  10. mysql_select_db("college",$con);
  11. $v_datetime = 'datetime';
  12.  
  13.  
  14. mysql_select_db("calendar", $con);
  15. $result = mysql_query("SELECT * FROM datetime  ORDER BY id DESC LIMIT 1");
  16. while($row = mysql_fetch_array($result))
  17.   {
  18.  
  19.  
  20.  
  21. echo"<div class='month' align='center'><input type='hidden' name='month' value='".$row['month']."' ><l>".$row['month']."</l></div>";
  22.  
  23.  
  24. //---------------------------------- choeckbox 1(bookable time 1)------------------------\\
  25.  
  26. $sql="SELECT college FROM is_availble WHERE datetime=".$v_datetime;
  27. $r = mysql_query($sql);
  28. echo"<div  class='box'>
  29. <div class='boxbut'>";
  30. if ($v_datetime == "$row[day1]   $row[time1]") {
  31.  
  32.  echo"
  33. <d>". $row['day1'] ,"</d><br/><t>". $row['time1'] ."</t> <br/>is booked";
  34. }
  35. else{
  36. echo"<input type='radio' name='datetime' value='". $row['day1']."&nbsp;&nbsp;&nbsp;".$row['time1']."'>
  37. <d>". $row['day1'] ,"</d><br/><t>". $row['time1'] ."</t>
  38.  
  39. </div>
  40. </div>";
  41.  
  42.  }
  43.  
  44.  

Regards
May 3 '12 #1
Share this Question
Share on Google+
2 Replies


Dormilich
Expert Mod 5K+
P: 8,639
the problem is that you need two connections because mysql_select_db() only changes the default database on the given connection. hence you canít use two databases on one connection.

additionally, never connect as root, thatís for DB administration. should someone find an exploit (SQL Injection), youíre doomed. a restricted user may only select, but root can alter, delete and create.

note, with PDO/MySQLi this would be easier to handle:
Expand|Select|Wrap|Line Numbers
  1. $college = new PDO("mysql:host=localhost;dbname=college", $user, $pass);
  2. $calendar= new PDO("mysql:host=localhost;dbname=calendar", $user, $pass);
  3.  
  4. // using
  5. $result1 = $college->query($sql_1);
  6. // ...
  7.  
  8. $result2 = $calendar->query($sql_2);
  9. foreach ($result2 as $row)
  10. {
  11.     // ...
  12. }
May 3 '12 #2

P: 2
Thank you for you help

I have now this code, actually making connection is still not possible. I also tried your code, but no reasult!

Expand|Select|Wrap|Line Numbers
  1. $host='localhost';
  2. $user='valvoja';
  3. $passoerd='test123';
  4. $con = mysql_connect($host,$user,$password);
  5. $con2=mysql_connect($host,$user,$password);
  6. if (!$con)
  7.   {
  8.   die('Could not connect: ' . mysql_error());
  9.   }
  10.  
  11. mysql_select_db("college",$con);
  12. $v_datetime = 'datetime';
  13.  
  14.  
  15. mysql_select_db("calendar", $con2);
  16. $result = mysql_query("SELECT * FROM datetime  ORDER BY id DESC LIMIT 1");
  17. while($row = mysql_fetch_array($result))
  18.   {
  19.     $sql="SELECT college FROM is_availble WHERE datetime=".$v_datetime;
  20. $r = mysql_query($sql);
  21. echo"<div  class='box'>
  22. <div class='boxbut'>";
  23. if ($v_datetime == "$row[day1]   $row[time1]") {
  24.  
  25.  echo"
  26. <d>". $row['day1'] ,"</d><br/><t>". $row['time1'] ."</t> <br/>is booked";
  27. }
  28. else{
  29. echo"<input type='radio' name='datetime' value='". $row['day1']."&nbsp;&nbsp;&nbsp;".$row['time1']."'>
  30. <d>". $row['day1'] ,"</d><br/><t>". $row['time1'] ."</t>
  31.  
  32. </div>
  33. </div>";
  34.  
  35.  }
  36.  
  37.  

Actually the most importand thing, that I want to do, is that query, which check if checkbox's value exist on database.

thanx
May 4 '12 #3

Post your reply

Sign in to post your reply or Sign up for a free account.