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 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测