bagmaendene Posted February 19, 2013 Share Posted February 19, 2013 (edited) Hello all, I have a little problem updating my sql. This is my page: <?php session_start(); include_once('../includes/connection.php'); include_once('../includes/article.php'); $article = new Article; if (isset($_SESSION['logged_in'])) { if (isset($_GET['id'])) { $id = $_GET['id']; $query = $pdo->prepare('SELECT FROM articles WHERE article_id =?'); $query->bindValue(1, $id); $query->execute(); header('Location: edit.php'); } $articles = $article->fetch_all(); ?> <html> <head> </head> <body> <h4>Edit Article</h4> <?php if (isset($error)) { ?> <small style="color:#aa0000"><?php echo $error; ?> <?php } ?> <form action="edit.php" method="post"> <?php foreach ($articles as $article) { ?> ID: <input type="text" name="id" value="<?php echo $article['article_id']; ?>"><br /><br /> Article Title: <b><?php echo $article['article_title']; ?></b> <br /><br /> Text: <br/> <textarea rows="15" name="article_content" cols="50"><?php echo $article['article_content']; ?></textarea><br /><br /> <input type="submit" value="Edit Article"> <br/ > <?php } if( isset($_POST['article_content'])) { $newcontent = $_POST['article_content']; $id = $_POST['id']; $query = $pdo->query("UPDATE article_content SET article_content='$newcontent' WHERE id='$id'"); } ?> </form> <a href="index.php">← Back</a> </body> </html> <?php } else { header('Location: index.php'); } ?> Here is a screen of my database: http://imageshack.us...ing2013021.png/ Could somebody please help me? -edit- Its about the article_content row. That has to be updated. Thnx Edited February 19, 2013 by bagmaendene Quote Link to comment Share on other sites More sharing options...
requinix Posted February 19, 2013 Share Posted February 19, 2013 Your form contains many textboxes and textareas, all named "id" and "article_content". They will all overwrite each other and you'll only get the value from the last one in the form. You can a] Write a form for every article (move the inside the loop) which only lets you update one article at a time, orb] Make a smarter, more complicated form that lets you update more than one at a time. Quote Link to comment Share on other sites More sharing options...
bagmaendene Posted February 20, 2013 Author Share Posted February 20, 2013 Could you help me to fix it, i have been trying to fix this the whole day. Would be really great, Thnx in advance Quote Link to comment Share on other sites More sharing options...
requinix Posted February 20, 2013 Share Posted February 20, 2013 Fix it which way? Quote Link to comment Share on other sites More sharing options...
Mikey Posted February 20, 2013 Share Posted February 20, 2013 As has been said, move <form> inside your loop, that will create a new form per-article. Quote Link to comment Share on other sites More sharing options...
bagmaendene Posted February 20, 2013 Author Share Posted February 20, 2013 As has been said, move <form> inside your loop, that will create a new form per-article. could you be a bit more specific? Quote Link to comment Share on other sites More sharing options...
requinix Posted February 20, 2013 Share Posted February 20, 2013 could you be a bit more specific? Not a whole lot. The and are outside the loop. Put them inside the loop. Quote Link to comment Share on other sites More sharing options...
fenway Posted February 21, 2013 Share Posted February 21, 2013 Writing out N forms isn't great either -- what if they want to make multiple changes? Usually, you just need another field with the ID, and then append this ID to each field name. Quote Link to comment Share on other sites More sharing options...
Barand Posted February 22, 2013 Share Posted February 22, 2013 Usually, you just need another field with the ID, and then append this ID to each field name. You do mean as an array index (eg name="email[$id]" ) don't you? Quote Link to comment Share on other sites More sharing options...
fenway Posted February 23, 2013 Share Posted February 23, 2013 I suppose you can do it that way, though I never have -- what happens in PHP if the $id's aren't contigious? Will you get a sparsely populated array? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
× Pasted as rich text. Restore formatting
Only 75 emoji are allowed.
× Your link has been automatically embedded. Display as a link instead
× Your previous content has been restored. Clear editor
× You cannot paste images directly. Upload or insert images from URL.