MYSQL和PHP需要在同一页面上的表中添加echo

时间:2013-03-05 22:31:36

标签: php

我需要在php页面上添加一个echo到我在同一页面上创建的表格,以便将其交叉插入到不同的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上的操作

1 个答案:

答案 0 :(得分: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)和缺少关闭(字体,中心)标签。请参阅我添加的评论代码。