duandongji2231 2016-09-25 16:54
浏览 99
已采纳

我的INSERT INTO查询无效

I'm trying to make a news system so I just have to fill the form to post an article. Unfortunately, it seems that after many tries and methods, my INSERT INTO query doesn't seem to do anything. I work with 2 separate files: addnews.php (the form) and confirm.php. I translated everything from the french so it's easier for you to read.

Here is the form code which is working fine:

        <div id="centerbox" class="news1" align="center">
        <h1>Write a news</h1><br>
        <form method="post" action="confirm.php">
            <p>Title:</p>
            <input type="text" name="title"><br>

            <p>Content:</p>
            <textarea name="content" rows="8" cols="45"></textarea> 
            <br>
            <p>Picture to display:</p>
            <select name="picture">
                <option value="news">pics/INFO_IMPORTANTE.jpg</option>
                <option value="event">pics/EVENEMENT.jpg</option>
                <option value="helpme">pics/DEMANDE_AIDE.jpg</option>
                <option value="helpyou">pics/PROPOSITION_AIDE.jpg</option>
            </select>

            <p>Your name:</p>
            <input type="text" name="author"><br>

            <input type="hidden" name="date" value="<?php echo date("d/m/Y"); ?>">

                <input type="submit" value="Confirm">
        </form>
    </div>

And here is the code in confirm.php:

        <?php
    try
    {
        $bdd = new PDO('mysql:host=localhost;dbname=L1-EVMAN', 'root', '');
    }
    catch (Exception $e)
    {
        die('Error : ' . $e->getMessage());
    }
    ?>

        <?php
        $title = htmlspecialchars($_POST['title']);
        $picture = $_POST['picture'];
        $content = htmlspecialchars($_POST['content']);
        $author = htmlspecialchars($_POST['author']);
        $time = date('d/m/Y');

        if (empty($title) OR empty($content) OR empty($author))
        {
            echo '<font color="red">You have to fill everything !</font><br>
            <a href="http://localhost/sourcemaster/addnews>Start over</a>';
        }
        else 
        {
            $addnews = $bdd->prepare('INSERT INTO news (id, title, pic, content, author, time) VALUES(NULL, :$title, :$picture, :$content, :$author, :$time)');
            $addnews->execute(array(
                'title' => $title,
                'pic' => $picture,
                'content' => $content,
                'author' => $author,
                'time' => $time
                ));

            echo 'The news has been added correctly.';
            }
        ?>

The problem is that when I fill the form and confirm, it does say that the news has been added but nothing appears on the news page (which works fine when I manually add everything in the table). I'm really kind of new in all this and I can tell you that I improvised a bit at the end, following some tutorials I found but none of them seem to help me.

So I'm asking you guys for help. Feel free to ask for more details or anything.

Thank you very much!

  • 写回答

1条回答 默认 最新

  • dongyou2305 2016-09-25 16:59
    关注

    Your insert query parameter is incorrect.

    :$title for example will expand to :"whatever the user entered"

    And you don't have such parameter bound.

    You have to remove the "$" from the bind parameters:

    'INSERT INTO news (id, title, pic, content, author, time) VALUES(NULL, :title, :picture, :content, :author, :time)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 springboot接入微信支付SDK
  • ¥50 大区域的遥感影像匹配 怎么做啊
  • ¥15 求解答:pytorch跑yolov8神经网络受挫
  • ¥20 Js代码报错问题不知道怎么解决
  • ¥15 gojs 点击按钮node的position位置进行改变,再次点击回到原来的位置
  • ¥15 计算决策面并仿真附上结果
  • ¥20 halcon 图像拼接
  • ¥15 webstorm上开发的vue3+vite5+typeScript打包时报错
  • ¥15 vue使用gojs,需求在link中的虚线上添加方向箭头
  • ¥15 CSS通配符清除内外边距为什么可以覆盖默认样式?