在mysql中插入下拉列表


Insert dropdown list into mysql

我想用下拉列表将数据插入mysql数据库,但我遇到了以下错误:分析错误:语法错误,C:''wamp''www''oustadee''profile.php中第37行出现意外的"$bio"(T_VARIABLE)这是代码:奥斯塔迪

  • 联系人

奥斯塔迪

            <!-- PHP -->
            <?php 
            session_start();
            echo '<p>Bonjour,'. ' ' .  $_SESSION['email'] . '!</p>';
                if (isset($_POST['submit'])){
                $bio=htmlspecialchars(trim($_POST['bio']));
                $numero=htmlspecialchars(trim($_POST['numero']));
                $ville=htmlspecialchars(trim($_POST['ville']));
                $niveau=htmlspecialchars(trim($_POST['niveau']));
                $matiere=htmlspecialchars(trim($_POST['matiere']));
                            try
                {
                    $bdd = new PDO('mysql:host=localhost;dbname=test;charset=utf8', 'root', '',array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
                }
                catch (Exception $e)
                {
                        die('Erreur : ' . $e->getMessage());
                }
            $req=$bdd->prepare('INSERT INTO membres(bio,numero,ville,niveau,matiere) VALUES ('$bio', '$numero','$ville', '$niveau', '$matiere')');  
            $req->execute(array(
                'bio' => $bio,
                'numero' => $numero,
                'ville'=> $ville,
                'niveau' => $niveau,
                'matiere' => $matiere
                ));
            }
            ?>
            <!-- END PHP -->
            <p> Vous pouvez commencer à créer votre annonce! </p>
            <form method="post" action="profil.php">
            <label for="bio">Bio:</label><br/>
            <textarea rows="4" cols="50"></textarea><br/>
            <label for="numero">Numéro:</label><br />
            <input type="text"><br><br>
                <label for="ville">Ville:</label>
                    <select name="ville" id="ville">
                        <option value="casablanca">Casablanca</option>
                        <option value="rabat">Rabat</option>
                        <option value="marrakech">Marrakech</option>
                    </select>
                <label for="niveau">Niveau:</label>
                    <select name="niveau" id="niveau">
                        <option value="bac">Bac</option>
                        <option value="6éme">Prépa</option>
                        <option value="université">Université</option>
                    </select><p>    
                <label for="matiere">Matières:</label>
                <input type="checkbox" value="mathematique">Mathématiques
            <input type="checkbox" value="francais">Francais
            <input type="checkbox" value="informatique">Informatique
            <input type="checkbox" value="anglais">Anglais</p>  
            <input type="submit" value="Valider">
            </form>
            </body>
            </html>

这是第37行:

            $req=$bdd->prepare('INSERT INTO membres(bio,numero,ville,niveau,matiere) VALUES ('$bio', '$numero','$ville', '$niveau', '$matiere')');

尝试执行:

$req=$bdd->prepare("INSERT INTO membres(bio,numero,ville,niveau,matiere) VALUES ('$bio', '$numero','$ville', '$niveau', '$matiere')");

意思是加上双引号。

$req=$bdd->prepare('INSERT INTO membres(bio,numero,ville,niveau,matiere) VALUES (' . $bio . ', ' . $numero . ',' . $ville . ', ' . $niveau . ', ' . $matiere . ')');