Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jul 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Article isnt updating

    Any idea why post isn't updating in mysql ?
    Code:
    <?php
    
    session_start();
    
    include_once '../includes/connection.php';
    
    if (isset($_SESSION['logged_in'])) {
        if (isset($_POST['title'], $_POST['content'])) {
            $title = $_POST['title'];
            $content = nl2br($_POST['content']);
            
            if (empty($title) or empty ($content)) {
                $error = 'All fiels are required!';
            } else {
                
                $query = $pdo->prepare("UPDATE articles (article_title, article_content) VALUES (?, ?)");
                
                $query->bindValue(1, $title);
                $query->bindValue(2, $content);
                $query->bindValue(3, time());
                
                $query->execute();
                
                header('Location: index.php');
            }
        }
        //dispaly add page
    ?>
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <title></title>
            <link rel="stylesheet" href="../style.css" />
        </head>
        <body>
            <div class="container">
                <a href="index.php" id="logo">CMS</a>
                <h4>Edit article</h4>
                <?php if (isset($error)) { ?>
                    <small><?php echo $error; ?></small>
                <?php } ?>
                <form action="edit_post.php" method="post" autocomple="off">
                    <input type="text" name="title" placeholder="Title" /><br><br>
                    <textarea row="50" cols="50" placeholder="Content" name="content"/></textarea><br><br>
                    <input type="submit" value="Edit article"/>
                </form>
                
            </div>
        </body>
    </html>
    <?php
    } else {
        header('Location: index.php');
    }
    
    ?>

  • #2
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts
    At first glance, your query is wrong. It should be along the lines of:

    UPDATE table SET field = :value, field2 = :value2 WHERE id = :id

    http://dev.mysql.com/doc/refman/5.0/en/update.html

    HTH
    Dan
    PHP Tip: If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.

    Don't forget to save everyone time and mark your thread as Resolved :)

    "Also note that it is your responsibility to die() if necessary."

    DON'T USE THE MYSQL_ EXTENSION

  • #3
    Regular Coder
    Join Date
    Nov 2012
    Posts
    115
    Thanks
    7
    Thanked 12 Times in 12 Posts
    I'm not familiar with PDO, nor the bind-statements, but something that occured to me: in your query you have 2 question marks, presumably for binded variables right? Well, you're actually binding 3 variables, $query->bindValue(3, time()); being the third one.

    Could this throw an error of some kind?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •