根据下拉选择将选项值添加到数据库

时间:2018-08-17 14:44:24

标签: php drop-down-menu

我想针对下拉框中选择了postTitle的行,用数据库的“ postSeq”列中的值“ 2”更新数据库。

                    <form action='' method='post'>
                    <div class="form-group">
                    <select class="form-control custom-select custom-select-md" id="Position" name="postSeq">
                    <?php 
                    $stmt = $db->query('SELECT postTitle, postStatus, postSeq FROM case_studies WHERE postStatus = "Published" ORDER BY postID DESC');
                    while($row = $stmt->fetch()){
                    ?>          
                    <option value="2"><?php echo $row['postTitle']; }?></option>
                    </select>
                    </div>
                    <button type="submit" class="btn btn-primary mb-2" name='submit'>Update</button>
                    </form> 




                <?php
                //if form has been submitted process it
                if(isset($_POST['submit'])){

                    $_POST = array_map( 'stripslashes', $_POST );

                    //collect form data
                    extract($_POST);

                    if(!isset($error)){

                        try {


                            //insert into database
                            $stmt = $db->prepare('UPDATE case_studies SET postSeq = :postSeq WHERE postTitle = :postTitle');
                            $stmt->execute(array(':postSeq' => $postSeq, ':postTitle' => $postTitle));

                            //redirect to index page
                            header('Location: case-study-showlist.php?action=updated');
                            exit;

                        } catch(PDOException $e) {
                            echo $e->getMessage();
                        }

                    }

                }

                ?>

我了解到它无法在循环中标识正确的postTitle以更新正确的行。但不知道如何解决。帮助将不胜感激。

0 个答案:

没有答案
相关问题