简体   繁体   English

如何使用html和php安排我的表单

[英]How can I arrange my form with html and php

The problem I have is that there is no results in the "category" option. 我遇到的问题是"category"选项没有结果。 I tried to use two whiles but that doesn't work either. 我尝试使用两个whiles但这也不起作用。

This is my php: 这是我的PHP:

try{
             $sel=$pdo->prepare("Select * from
                         actors inner join cat
                         on actors.id=cat.id");

    $sel->bindColumn(1,$actors_id);
    $sel->bindColumn(2,$actors);
    $sel->bindColumn(3,$cat_id);
    $sel->bindColumn(4,$cat);
    $sel->execute();




}catch(PDOException $e){

     die("error: " . $e->getMessage());

 }

And This is my html: 这是我的HTML:

<form action="" method="post">

    <label for="author">Author</label>
       <Select id="author">

      //Show data from php file:

    <?php while($sel->fetch()):?>
        <?php echo "<option value='$actors_id'>$actors</option>";?>
    <?php endwhile; ?>
       </Select>

       <label for="category">Category</label>

       <Select id="category">

      //Show data again from php file:

    <?php while($sel->fetch()):?>
        <?php echo "<option value='$cat_id'>$cat</option>";?>
    <?php endwhile; ?>
       </Select>     
    </form>

And this is how it looks like: 这就是它的样子:

在此输入图像描述

As you can see, there is no info display in the "category" option. 如您所见,“类别”选项中没有信息显示。

If I change the html to something like this, it doesn't work either: 如果我将html更改为类似的东西,它也不起作用:

<Select id="author">

    <?php while($sel->fetch()):?>
        <?php echo "<option value='$actors_id'>$actors</option>";?>
       </Select>

       <label for="category">Category</label>
       <Select id="category">

        <?php echo "<option value='$cat_id'>$cat</option>";?>
    <?php endwhile; ?>
       </Select>

Now it looks like this: 现在它看起来像这样:

在此输入图像描述

Do you have any suggestion? 你有什么建议吗?

You need to be cleaner in your return - 你需要更清洁 -

<?php 
$result = $sel->fetchAll(PDO::FETCH_OBJ); //return an object
foreach($result as $actor) {
     echo '<option value=' . $actors->actor_id .'>'. $actor->actors .'</option>'; // note the quotes
}

?>

You can loop through the result as many times as you need to, without re-fetching the data. 您可以根据需要循环遍历结果,而无需重新获取数据。

Your first while loop fetches all rows from the PDOStatement object, so when you call while ($sel->fetch()) a second time, it immediately returns false . 你的第一个while循环从PDOStatement对象中获取所有行,所以当你第二次调用while ($sel->fetch())时,它会立即返回false If you want to use the results in essentially two separate loops, fetch all rows first into an array of rows, then loop over that array. 如果要在基本上两个单独的循环中使用结果,先将所有行提取到一个行数组中,然后遍历该数组。 Do so with fetchAll() instead of binding to variables via bindColumn() . 使用fetchAll()而不是通过bindColumn()绑定到变量。

In fact, I would almost always recommend fetching first rather than during the display logic, except for very large rowsets where the memory consumed by fetching it all at once would be taxing on your system. 事实上,我几乎总是建议首先获取而不是在显示逻辑期间,除了非常大的行集,其中通过一次获取所有内存消耗的内存将对您的系统造成负担。

// Get all the rows as an array
$rows = $sel->fetchAll(PDO::FETCH_ASOC);

// Later loop for the items you need as many times as you need
// Each new foreach will iterate the entire $rows array from start to end
<?php foreach ($rows as $row): ?>
<?php echo "<option value='{$row['actors_id']}'>" . htmlspecialchars($row['actors']) . "</option>";?>
<?php endforeach; ?>

<?php foreach ($rows as $row): ?>
<?php echo "<option value='{$row['cat_id']}'>" . htmlspecialchars($row['cat']) . "</option>";?>
<?php endforeach; ?>

Don't forget to call htmlspecialchars() on the output, to ensure it is properly encoded for HTML. 不要忘记在输出上调用htmlspecialchars() ,以确保它正确编码为HTML。 I haven't done so on cat_id,actors_id on the assumption those are known to be integers, but if not, then you should use htmlspecialchars($row['cat_id'], ENT_QUOTES) to ensure the HTML attributes are correctly quoted. 我没有在cat_id,actors_id上做过假设那些已知的整数,但如果没有,那么你应该使用htmlspecialchars($row['cat_id'], ENT_QUOTES)来确保HTML属性被正确引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM