-1

我需要将 php 页面上的回显添加到我在同一页面上创建的表中,以将其交叉插入到不同的 SQL DB

以下是我的代码

<?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 中,然后通过放置在 SQL DB 上的操作

4

1 回答 1

0

好吧,你已经完成了 99% 的工作。这里有一些事情要做:

  • 您正在分别打开和关闭数据库连接。我建议你一开始它们一起打开,最后一起关闭它们
  • 因此,每种情况下数据库句柄的变量必须不同。我建议$con1and $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)和缺少的结束(字体,中心)标签。请参阅我添加的注释的代码。

于 2013-03-06T09:35:23.967 回答