2013-03-05 52 views
-1
同一页

上,我需要一个PHP页面上添加一个回声我在同一页上创建的表中插入穿过它到一个不同的SQL数据库MySQL和PHP需要回声添加到表

以下是我的代码

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

mysql_select_db("****", $con); 

$result = mysql_query("SELECT * FROM members 
WHERE member_msisdn='$slusername'"); 
echo "<table border='1'> 
<tr> 
<th>Membership</th> 
<th>Number</th> 
<th>Registration Date</th> 
<th>End Date</th> 
<th>Copy of ID</th> 
</tr>"; 
while($row = mysql_fetch_array($result)) 
{ 
echo "<td><center>" . $row['member_id'] . "</td>"; 
echo "<td><font color=blue>" . $row['member_msisdn'] . "</td>"; 
echo "<td><center>" . $row['asdate'] . "</td>"; 
echo "<td><center>" . $row['aedate'] . "</td>"; 
echo "<td><center>" . $row['attid'] . "</td>"; 
echo "</tr>"; 
    } 
echo "</table>"; 

mysql_close($con); 
?></div> 
     <div class="Notes"><strong><br> 
      Emergency Contact Numbers:</strong><br> 
      <?php 
$con = mysql_connect("localhost","****","****"); 
if (!$con) 
    { 
    die('Could not connect: ' . mysql_error()); 
    } 

mysql_select_db("****", $con); 

$result = mysql_query("SELECT * FROM members a INNER JOIN recipients b ON a.member_id =  
b.member_id WHERE member_msisdn=$slusername"); 
echo "<table border='1'> 
<tr> 
<th>Number</th> 
<th>Name</th> 
<th>Surname</th> 
</tr>"; 
while($row = mysql_fetch_array($result)) 
{ 
echo "<td><font color=blue>" . $row['recipient_msisdn'] . "</td>"; 
echo "<td>" . $row['recipient_name'] . "</td>"; 
echo "<td>" . $row['recipient_surname'] . "</td>"; 
echo "</tr>"; 
    } 
echo "</table>"; 

mysql_close($con); 
?> 
     </div> 
     <p> 
      <label for="member_id"></label> 
      <input type="text" name="member_id" id="member_id"> // here i need the  
member_id of members to be printed so i can have it entered into another table on same  
sql DB 
     </p> 
     <p><br> 
     </p> 
     </div></td> 

基本上在上第一节呼应member_id,但我需要它在本身member_id上表可以inputed,然后用行动置于SQL数据库

+0

如果你有你的评论(“这里我需要member_id of members ...”),你是说你需要第一个数据库的'members.member_id'结果集,而不是第二个? – halfer 2013-03-05 22:56:55

+0

是的,这是正确的我想打印从第一个数据库的信息,并将其插入到第二个数据库 – 2013-03-06 03:25:26

回答

0

好了,好了你”在那里99%的路上。这里有一些事情要做:

  • 你正在打开和关闭数据库连接分开。我建议你在开始时一起打开它们,并在最后将它们关闭在一起
  • 数据库句柄在每种情况下的变量因此必须不同。我建议$con1$con2,但这取决于你。
  • 而不是每个查询前的mysql_select_db,只需要mysql_query($sql, $con1)(根据需要替换连接变量)。这使得哪个语句连接到哪个数据库更清晰。

从那里,它是那么容易在最后写一个额外的查询从第一数据库再次读取,所以你可以把它放在一组<input>标签值。


旁白:如果你尝试从“视图”(它是什么样子)分离出你的“商业逻辑”(它是如何工作)你的编码将变得更加容易。一个良好的开端(你的代码的第一位)可能是这样的:

<?php 
// This is the "logic" section, so we indent carefully, and get our 
// variables ready for the "view" to consume 
$con1 = mysql_connect("localhost", "****", "****"); 
if (!$con1) 
{ 
    die('Could not connect: ' . mysql_error()); 
} 

// Don't be afraid to wrap lines like this - makes them more readable, 
// and means less h-scrolling in your IDE 
$result = mysql_query(
    "SELECT * FROM members WHERE member_msisdn='$slusername'", 
    $con1 
); 
?> 

<!-- This is our 'view' section, which we write in proper HTML, rather 
than blobs of HTML trapped inside our echo statements --> 
<table border='1'> 
    <tr> 
    <th>Membership</th> 
    <th>Number</th> 
    <th>Registration Date</th> 
    <th>End Date</th> 
    <th>Copy of ID</th> 
    </tr> 
    <!-- One useful convention is to use the colon form of loops for views 
    - it is generally thought of as neater --> 
    <?php while($row = mysql_fetch_array($result)): ?> 
    <!-- You forgot to open (tr) --> 
    <tr> 
     <!-- Note! Always close tags you've opened --> 
     <!-- (center) and (font) are deprecated anyway, use CSS instead --> 
     <td><center> 
     <?php echo $row['member_id'] ?> 
     </center></td> 
     <td><font color="blue"> 
     <?php echo $row['member_msisdn'] ?> 
     </font></td> 
     <td><center> 
     <?php echo $row['asdate'] ?> 
     </center></td> 
     <td><center> 
     <?php echo $row['aedate'] ?> 
     </center></td> 
     <td><center> 
     <?php echo $row['attid'] ?> 
     </center></td> 
    </tr> 
    <?php endwhile ?> 
</table> 

<!-- You can do connection closing and other cleanup here --> 

现在,我们得到了语法着色,在我们真正 HTML的利益,和PHP只是打开了小小的一行声明。更干净!我也更好地进行了缩进,这有助于揭示缺少的开放(tr)和缺少关闭(字体,中心)标签。查看我添加的评论的代码。

+0

好吧,我已经做了清理有点看起来更好,但问题,我仍然不能调用member_id到字段自动打印它在那里为我把它移动到另一个sql – 2013-03-07 08:19:23

+0

这给我没有新的回应。也许添加一个链接到新代码的粘贴板(例如pastie.org)? – halfer 2013-03-07 11:10:16