使用php& amp;填充数据内部联接

时间:2014-01-02 11:15:10

标签: php mysql sql

我想使用php在表格中填充数据。我得到的输出,但它的节目

Use of undefined constant classid - assumed 'classid'

我是否可以添加列并将这些值存储在新表中?

这是我的代码:

<html>
 <head>
   <title>grade1</title>
 </head>
 <body>
   <table border="1" cellspacing="1" cellpadding="1" width="200" height="200">
     <tr>
       <th>classid</th>
       <th>studentid</th>
       <th>teacherid</th>
       <th>locid</th>
       <th>date</th>
       <th>flag</th>
       <th>comments</th>
     </tr>
     <?php
        $host = "localhost";
        $user = "root";
        $pass = "";
        $dbname = "my_attendance";
        $prefix = "";
        $dbcon = mysql_connect($host, $user, $pass) or die("not connected");

        mysql_select_db($dbname, $dbcon) or die("not selected");
        $query = "(SELECT  a.classid, a.fname, b.teacherid, c.locid
                   FROM class_master c JOIN student_master a 
                   ON c.classid = a.classid JOIN teacher_link b
                   ON c.classid = b.classid 
                   WHERE c.classid = 'grade1' )";
        $result = mysql_query($query);

        while( $row = mysql_fetch_array($result))
        {
          echo "<form action=insertattend.php method=POST>";
          echo "<tr>";
          echo "<td>" . "<input type=text value=" .$row[classid]." </td>";
          echo "<td>" . "<input type=text value=" .$row[fname]." </td>";
          echo "<td>" . "<input type=text value=" .$row[teacherid]." </td>";
          echo "<td>" . "<input type=text value=" .$row[locid]." </td>";
          mysql_close($dbcon);
        }
     ?>
</html>

3 个答案:

答案 0 :(得分:0)

数组键周围缺少引号。

$row['classid']
$row['fname']
$row['teacherid']
$row['locid']

您还有其他问题。

echo "<td>" . "<input type=text value=" .$row['classid']." </td>";

您缺少html属性值周围的引号和<input>的缺少结束标记。正确的表格将是

echo "<td>" . "<input type=\"text\" value=\"" .$row['classid']."\"></td>";

在这些情况下,使用单引号可以避免转义双引号。

echo '<td>' . '<input type="text" value="' . $row['classid']. '>"</td>';

此外,您不应该使用mysql_ *函数。

Why shouldn't I use mysql_* functions in PHP?

答案 1 :(得分:0)

您需要对数组索引使用单引号,例如:

echo "<td><input type=text value=" .$row['classid']." /> </td>";

答案 2 :(得分:0)

这里有几个问题。您需要封装密钥,这就是导致您报告错误的原因

$row['classid']

(你需要像这样对待所有那些键,而不仅仅是classid。)

关闭输入标记

" /></td>";

用引号

包装HTML属性值
type=\"text\" value=\"" .$row[classid]."\"

引号需要在echo中转义,因为字符串是用引号封装的。

 echo "<td><input type=\"text\" value=\"" .$row['classid']."\" /></td>";

你可以使用单引号而不必逃避它们

 echo '<td><input type="text" value="' .$row['classid'].'" /></td>';