【问题标题】:PHP PDO Update with 2 tables带有 2 个表的 PHP PDO 更新
【发布时间】:2013-11-08 14:30:59
【问题描述】:

我有一个小问题。这就是我想要实现的: 我有 2 个 mysql 表(类别、频道),频道表中有一个 cat_id。我想更新/编辑产品并将其放在另一个类别中,但我制作的代码仅显示一个类别 (id=1),即使该产品的父 id(cat_id) 为 5。

try {
    //prepare query
    $query = "select channel_id, name, category_id from channels where channel_id = ? limit 0,1";
    $stmt = $pdo->prepare( $query );
    //this is the first question mark
    $stmt->bindParam(1, $_REQUEST['id']);
    //execute our query
    $stmt->execute();
    //store retrieved row to a variable
    $row = $stmt->fetch(PDO::FETCH_ASSOC);
    //values to fill up our form
    $channel_id = $row['channel_id'];
    $name = $row['name'];
    $category_id = $row['category_id'];
}catch(PDOException $exception){ //to handle error
    echo "Error: " . $exception->getMessage();
}
$query2 = "SELECT * FROM categories";
$stmt2 = $pdo->prepare( $query2 );
$stmt2->execute();
$results = $stmt2->fetchAll(PDO::FETCH_ASSOC);
?>
<!--we have our html form here where new user information will be entered-->
<form action='#' method='post' border='0'>
<table>
    <tr>
        <td>Channel Name</td>
        <td><input type='text' name='name' value='<?php echo $name;  ?>' /></td>
    </tr>
    <tr>
        <td>Category</td>
        <td>
        <?php foreach($results as $rows) {?>
        <select name="fileselect">
            <option name='cat_id' value=" <?php echo $rows['category_id']; ?>"> <?php echo $rows['name']; ?></option>
            <!-- <input type='text' name='category_id' value='<?php //echo $category_id;  ?>' /> -->
        <?php } ?>
        </select>
        </td>
    </tr>
    <tr>
        <td></td>
        <td>
            <!-- so that we could identify what record is to be updated -->
            <input type='hidden' name='channel_id' value='<?php echo $channel_id ?>' /> 
            <!-- we will set the action to edit -->
            <input type='hidden' name='action' value='update' />
            <input type='submit' value='Edit' />
        </td>
    </tr>
</table>
</form>

【问题讨论】:

  • 你有什么错误吗?最后一句话是什么意思?
  • 没有错误,一切正常,但我在下拉列表中只得到 1 项..

标签: php mysql pdo


【解决方案1】:

而不是

<?php foreach($results as $rows) {?>
    <select name="fileselect">
        <option name='cat_id' value=" <?php echo $rows['category_id']; ?>"> <?php echo $rows['name']; ?></option>
        <!-- <input type='text' name='category_id' value='<?php //echo $category_id;  ?>' /> -->
    <?php } ?>
    </select>

试试:

<select name="fileselect">
<?php foreach($results as $rows) {?>
    <option name='cat_id' value=" <?php echo $rows['category_id']; ?>"> <?php echo $rows['name']; ?></option>
    <!-- <input type='text' name='category_id' value='<?php //echo $category_id;  ?>' /> -->
<?php } ?>
</select>

【讨论】:

  • 我觉得自己很蠢,花了 5 个小时检查了所有内容,这是因为语法错误:(
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-08-16
  • 2017-11-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-12
  • 2017-08-27
相关资源
最近更新 更多