Jump to content

PHP PDO problem with 2 tables


CosminStan

Recommended Posts

Hello,
I have a little problem. This is what I want to achive:
I have 2 mysql tables (categories, channels), the channel table has a cat_id in it. I want to update/edit a product and place it in another category but the code that I've made shows just one catedory (id=1) even if the product has a parent id of 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>

The update parte is ommited as it works if there is no dropdown, just a plain text input.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.