0

我有一个必须将他的值显示到 html 页面的函数,但它不起作用。

PHP代码:

$template = file_get_contents("main.tpl.php");

function updateForm($id)
{
    $query = "SELECT * FROM platen WHERE ID =". $id;
    $result = mysql_query($query) or die (mysql_error());

    while($row = mysql_fetch_array($result))
    { 
        $editForm = "<form action='index.php?update&id=". $row['ID'] ."' method='post'>";
        $editForm .= "<label for='id'><span>ID:</span></label> <input name='id' id='id' type='text' value='". $row['ID'] ."'></br>";
        $editForm .= "<label for='album'><span>Album:</span></label> <input name='album' id='album' type='text' value='". $row['Album'] ."'></br>";
        $editForm .= "<label for='band'>Band:</label> <input name='band' id='band' type='text' value='". $row['Band'] ."'></br>";
        $editForm .= "<label for='stijl'>Stijl:</label> <input name='stijl' id='stijl' type='text' value='". $row['Stijl'] ."'></br>";
        $editForm .= "<label for='medium'>Medium:</label> <input name='medium' id='medium' type='text' value='". $row['Medium'] ."'></br>";
        $editForm .= "<label for='datum'>Datum:</label> <input name='datum' id='datum' type='text' value='". $row['Datum'] ."'></br>";
        $editForm .= "<label for='prijs'>Prijs:</label> <input name='prijs' id='prijs' type='text' value='". $row['Prijs'] ."'></br>";
        $editForm .= "<input class='submit' type='submit' value='Update'>";
        $editForm .= "</form>";
    }
$template = str_replace("%%editForm%%",  $editForm, $template);
} 

echo $template;

在我的 HTML 文件中,我得到了 %%editForm%% 但是当我尝试这个时,它说 $template 是未定义的。希望您能帮助我或知道另一种在 HTML 文件中显示数据的解决方案。

4

3 回答 3

0

或者只是将它传递给函数。

$template = file_get_contents("main.tpl.php");

function updateForm($id, $template)
{
    $query = "SELECT * FROM platen WHERE ID =". $id;
    $result = mysql_query($query) or die (mysql_error());

    while($row = mysql_fetch_array($result))
    { 
        $editForm = "<form action='index.php?update&id=". $row['ID'] ."' method='post'>";
        $editForm .= "<label for='id'><span>ID:</span></label> <input name='id' id='id' type='text' value='". $row['ID'] ."'></br>";
        $editForm .= "<label for='album'><span>Album:</span></label> <input name='album' id='album' type='text' value='". $row['Album'] ."'></br>";
        $editForm .= "<label for='band'>Band:</label> <input name='band' id='band' type='text' value='". $row['Band'] ."'></br>";
        $editForm .= "<label for='stijl'>Stijl:</label> <input name='stijl' id='stijl' type='text' value='". $row['Stijl'] ."'></br>";
        $editForm .= "<label for='medium'>Medium:</label> <input name='medium' id='medium' type='text' value='". $row['Medium'] ."'></br>";
        $editForm .= "<label for='datum'>Datum:</label> <input name='datum' id='datum' type='text' value='". $row['Datum'] ."'></br>";
        $editForm .= "<label for='prijs'>Prijs:</label> <input name='prijs' id='prijs' type='text' value='". $row['Prijs'] ."'></br>";
        $editForm .= "<input class='submit' type='submit' value='Update'>";
        $editForm .= "</form>";
    }
$template = str_replace("%%editForm%%",  $editForm, $template);
} 
于 2013-06-22T10:48:06.667 回答
0

它说 $template 是未定义的,因为它在 functionscope 中不可见。您需要将 $template 传递给函数或在函数global $template;内部添加:

 function updateForm($id)
 {
   global $template;
   $query = "SELECT * FROM platen WHERE ID =". $id;
于 2013-06-22T10:39:46.273 回答
0

尝试

function updateForm($id,$template)
于 2013-06-22T10:45:36.500 回答