php - 如何修复这个准备好的语句

标签 php mysql

我遇到问题 1 小时了,不知道解决方案在哪里。 我做了一个准备好的语句,将表单中的元素插入到 BDD,但我不知道问题是什么,问题来自“exectute”之前,因为当我提交 mi 表单时,我会转到“else”。

我的 try catch 看起来像:

try
{
    $db = new PDO('mysql:host=localhost;dbname=lucilledrx928', 'root', 'root');
}
catch (Exception $e)
{
    echo 'Erreur : ' . $e->getMessage();
    die();
}

我准备好的声明:

if (isset($_POST['titre'])) {
    $query = $db->prepare('INSERT INTO news(user_id, titre, sous_titre, date, commentaire) VALUES (?, ?, ?, ?, ?)');
    if ($query->execute(array(
        strip_tags($_POST['user_id']),
        strip_tags($_POST['titre']),
        strip_tags($_POST['sous_titre']),
        strip_tags($_POST['date']),
        strip_tags($_POST['commentaire']),
    ))) {
        $_SESSION['msg'] = "Votre news a été créer";
        header('location: add_news.php');
    } else {
        $_SESSION['error_msg'] = 'Erreur lors de l\'upload à la base de donnée';
    };
}

和我的表格:

<h2>Ajoutez une News</h2>
<?php
echo '<form style="min-width: 500px;" action="' . SITE_URL . '/admin/add_news.php" method="post" enctype="multipart/form-data">';
?>
    <p>Titre de l'article: <input type="text" name="titre"/></p>
    <p>Sous titre de l'article: <input type="text" name="sous_titre"/></p>
    <p>Les Models:
        <select name="user_id">
            <?php
            foreach ($users as $user) {
                echo '<option value="' . $user['id'] . '">' . $user['name'] . '</option>';
            }
            ?>
        </select>
    </p>
    <p>Commentaire: <br/><textarea name="commentaire" rows="5" cols="45"></textarea></p>
    <p>Selection de Photo: <input type="text" id="member" name="member"
                                  placeholder="Choisir le nombre d'image"><br/>
        <a id="filldetails" onclick="addFields()" style="cursor: pointer">Valider le nombre de photo</a>
    <div id="addField"></div>
    <p><input type="hidden" name="MAX_FILE_SIZE" value="10000000"></p>
    <p><input hidden value="<?php $date->format('Y-m-d') ?>" name="date"><br></p>
    <p><input type="submit" value="Envoyer"></p>
</form>

最佳答案

我知道这不是OP想要的,但是你是否尝试过以不同的方式构建它。 你可以试试这个:

if (isset($_POST['titre'])&& !empty($_POST['titre']){

$query = $db->prepare("INSERT INTO news (user_id, titre, sous_titre, date, commentaire) VALUES (:user_id, :titre, :sous_titre, :sous_titre, :date, :commentaire ) ");
    $query->bindParam(':user_id', $_POST['user_id'], PDO::PARAM_STR);
    $query->bindParam(':titre', $_POST['titre'], PDO::PARAM_STR);
    $query->bindParam(':sous_titre', $_POST['sous_titre'], PDO::PARAM_STR);
    $query->bindParam(':date', $_POST['date'], PDO::PARAM_STR);
    $query->bindParam(':commentaire', $_POST['commentaire'], PDO::PARAM_STR);
    $res = $insertNews->execute();

if($res){
$_SESSION['msg'] = "Votre news a été créer";
        header('location: add_news.php');
} else {
$_SESSION['error_msg'] = 'Erreur lors de l\'upload à la base de donnée';
}
} 

关于php - 如何修复这个准备好的语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56122136/

相关文章:

php - 如何在Mysql中使用Mssql中的top获取 "limit"范围内的数据

php - 在 MySQL 查询中传递变量

php - Symfony 5.x - 加载 Web 调试工具栏时发生错误

php - 从帖子的 h1 标签在 MYSQL 中创建标题 (WordPress)

mysql - 复杂字典的数据库设计

javascript - 基于浏览器的客户端抓取

php - 如何保持 session 登录直到单击注销

javascript - PHP主页重定向空白

php - 如何在mysql查询中创建自定义列的值

MySQL 两张表带键的设计