簡體   English   中英

帶有2個表的PHP PDO更新

[英]PHP PDO Update with 2 tables

我有一點問題。 這就是我要達到的目標:我有2個mysql表(類別,通道),通道表中有一個cat_id。 我想更新/編輯產品並將其放在另一個類別中,但是即使該產品的父ID(cat_id)為5,我編寫的代碼也僅顯示一個類別(id = 1)。

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>

代替

<?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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM