0

我试图在值标签中默认显示一些文本。我试图在表单值中回显 $row。但是,我注意到未定义的变量。任何人都可以找出错误吗?

<?php  
$id=$_GET['id'];
$qry=mysql_query('SELECT * FROM pages WHERE pageid="$id"', $con);
if(!$qry)
{
die("Query Failed: ". mysql_error());
}
while($row=mysql_fetch_array($qry))
{
$row=mysql_fetch_array($qry);
}
?>

<div class="block2">
<p><b>Update Article</b></p>
<form action="article_edited.php" method="post" enctype="multipart/form-data"     name="form1" id="form1">
<p>Article Id &nbsp;&nbsp;&nbsp;:

<label for="image"></label>
<input type="text" name="pageid" id="pageid" value="<?php echo $row['pageid']?>" />
</p>
<label for="image"></label>
<p>Image Path:
<input type="text" name="imgpath" id="imgpath" value="<?php echo $row['imgpath'] ?>" />
</p>
<p>Contents &nbsp;&nbsp;&nbsp;:
<label for="cont"></label>
<textarea name="contents" id="contents" cols="50" rows="5" ><?php $row['text']     ?></textarea>
</p>
<p align="center">
<input type="submit" name="Submit" id="Submit" value="Submit" />
</p>
</form>

更新#1:这是我正在关注的教程:http ://www.vdesignourweb.com/cmsphpsqlb/cms_editarticle.html

更新#2:这是我的表结构:数据库信息

更新#3:我刚刚发现该表没有索引。这可能是问题吗?

4

6 回答 6

2

您只是$row在 while 循环中定义。它没有在 while 循环之外定义。您需要将值存储在数组或其他东西中,以便在while循环结束或在while循环期间执行操作!

    $results = array();

    while($row=mysql_fetch_array($qry)){
        $results [] = $row;
    }

    foreach($results as $row){
        echo $row['pageid'];
    }
于 2012-09-20T12:49:12.263 回答
1

如果它是一条记录,您可以检查是否有从查询返回的记录,然后使用

$row=mysql_fetch_array($qry);

代替

while($row=mysql_fetch_array($qry))
{
$row=mysql_fetch_array($qry);
}

你也可以看看mysql_fetch_row. 我建议不要使用 mysql_* ,因为它已经过时且已被弃用。

于 2012-09-20T12:51:43.150 回答
0

修改你的while循环如下:

$array=array();
while($row=mysql_fetch_array($qry))
{
    $array[]=mysql_fetch_array($qry);
}

然后,在你的文本区域中,让它说

<?php echo $row['text'] //you missed the echo statement ?>

或者,<?php echo //what you want to echo ?>您可以简单地使用<?= //what you want to echo here ?>

于 2012-09-20T12:52:42.623 回答
0

它正在获得一条记录,然后您必须使用单条记录

   $row=mysql_fetch_array($qry);
于 2012-09-20T12:52:51.200 回答
0

The last time the while loop executes, $row is set to false because mysql_fetch_array($qry) returns false when there are no more rows. After the loop, $row['pageid'] does not exist because $row is no longer an array.

I can't tell you how to fix it because I am not sure what you are trying to do in your loop. Should the HTML code after the loop be printed for each row?

<?php while ($row=mysql_fetch_array($qry)) { ?>
    <div class="block2">
    ...
    </div>
<?php } ?>

If you only want one result, you should write the query so you only get one result and skip the while loop.

$row = mysql_fetch_array($qry);
于 2012-09-20T13:23:48.240 回答
0

我认为你需要这样做,

<?php  
$id=$_GET['id'];
$qry=mysql_query('SELECT * FROM pages WHERE pageid="$id"', $con);
if(!$qry)
{
die("Query Failed: ". mysql_error());
}
while($row=mysql_fetch_array($qry))
{
    $rows[]=$row;
}
?>

<div class="block2">
<p><b>Update Article</b></p>
<form action="article_edited.php" method="post" enctype="multipart/form-data"     name="form1" id="form1">
<p>Article Id &nbsp;&nbsp;&nbsp;:
<?php 
foreach($rows as $row){
?>
<label for="image"></label>
<input type="text" name="pageid" id="pageid" value="<?php echo $row['pageid']?>" />
</p>
<label for="image"></label>
<p>Image Path:
<input type="text" name="imgpath" id="imgpath" value="<?php echo $row['imgpath'] ?>" />
</p>
<p>Contents &nbsp;&nbsp;&nbsp;:
<label for="cont"></label>
<textarea name="contents" id="contents" cols="50" rows="5" ><?php $row['text']     ?></textarea>
</p>
<?php }?>
<p align="center">
<input type="submit" name="Submit" id="Submit" value="Submit" />
</p>
</form>

我希望它可以帮助你。

于 2012-09-20T13:31:40.310 回答