phatgreenbuds Posted November 27, 2007 Share Posted November 27, 2007 So below is the code. I have this page that pulls fields from a database and populates accordingly. The idea is that they can then edit that field and when they click submit it will update the relevant record in the database. Its not doing though...it acts like it is but its not actually changing anything. Any ideas out there? <?php if (isset($_GET["id"])) { $selected = $_GET["id"]; $query = "SELECT * FROM blogger WHERE id = {$_GET["id"]}"; $target = mysql_query($query); // uses the query variable defined above. confirm_query($target); // calls to the function file. while ($final_target = mysql_fetch_array($target)) { $userx = "{$final_target["user"]}"; $blogtextx = "{$final_target["blogtext"]}"; $datex = "{$final_target["date"]}"; ?> <form action="blogedit.php" method="post"> Date: <br><input name="Date" type="text" value="<?php echo $datex; ?>"><br> <br> Name: <br><input name="Name" type="text" value="<?php echo $userx; ?>"><br> <br> Entry: <br><textarea name="Entry" cols="100" rows="10" wrap="PHYSICAL"><?php echo $blogtextx; ?></textarea><br> <br> <input type="submit" name="update" value="update"> </form> <br><br> <?php if (isset($_POST['update'])) { mysql_query("UPDATE blogger SET blogtext = '$new' WHERE 'blogger'.'id' = '$selected' LIMIT 1"); } } } ?> <?php include("../admin/includes/dbclose.php"); ?> Quote Link to comment Share on other sites More sharing options...
phatgreenbuds Posted November 27, 2007 Author Share Posted November 27, 2007 whoops...had some errors in the last bit of code that I forgot to fix...here is the latest: <?php if (isset($_GET["id"])) { $selected = $_GET["id"]; $query = "SELECT * FROM blogger WHERE id = {$_GET["id"]}"; $target = mysql_query($query); // uses the query variable defined above. confirm_query($target); // calls to the function file. while ($final_target = mysql_fetch_array($target)) { $userx = "{$final_target["user"]}"; $blogtextx = "{$final_target["blogtext"]}"; $datex = "{$final_target["date"]}"; ?> <form action="blogedit.php" method="post"> Date: <br><input name="Date" type="text" value="<?php echo $datex; ?>"><br> <br> Name: <br><input name="Name" type="text" value="<?php echo $userx; ?>"><br> <br> Entry: <br><textarea name="Entry" cols="100" rows="10" wrap="PHYSICAL"><?php echo $blogtextx; ?></textarea><br> <br> <input type="submit" name="update" value="update"> </form> <br><br> <?php if (isset($_POST['update'])) { mysql_query("UPDATE blogger SET blogtext = '$_POST[Entry]' WHERE 'blogger'.'id' = '$selected' LIMIT 1"); } } } ?> <?php include("../admin/includes/dbclose.php"); ?> Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted November 27, 2007 Share Posted November 27, 2007 Well, firstly there's a syntax error in your query. It should be: mysql_query("UPDATE blogger SET blogtext = '$new' WHERE id = '$selected' LIMIT 1"); You don't use single quotes around table and column names, and there's no need to specify the table name since we're only using one table. Second, $selected is undefined. Whilst it is defined when the page originally loads, this value isn't passed along when you submit the form. Thirdly, you need to restructure your code otherwise the changes wont be shown at first, since the update gets done after the form is displayed. Try: <?php if(isset($_GET['id'])){ $id = $_GET['id']; if(isset($_POST['update'])){ $text = $_POST['Entry']; mysql_query("UPDATE blogger SET blogtext = '$text' WHERE id = $id") or die(mysql_error());//i hope there's no need for the limit clause, since id should be a unique field } $sql = mysql_query("SELECT * FROM blogger WHERE id=$id") or die(mysql_error()); confirm_query($target); // no idea what this does, but ill leave it in $row = mysql_fetch_assoc($sql); //no need for a while statement - we're only working with one row $userx = $row['user']; $blogtextx = $row['blogtext']; $datex = $row['date']; ?> <form action="blogedit.php" method="post"> Date: <br><input name="Date" type="text" value="<?php echo $datex; ?>"><br> <br> Name: <br><input name="Name" type="text" value="<?php echo $userx; ?>"><br> <br> Entry: <br><textarea name="Entry" cols="100" rows="10" wrap="PHYSICAL"><?php echo $blogtextx; ?></textarea><br> <br> <input type="submit" name="update" value="update"> </form> <br><br> <?php }else{ echo 'No ID selected'; } ?> You'll notice i put the code to display the form inside the if statement that checks if $_GET['id'] was set, since we only want to show the edit form if this is the case. Also, you'll want to add some error checking into that. Quote Link to comment Share on other sites More sharing options...
phatgreenbuds Posted November 27, 2007 Author Share Posted November 27, 2007 i should have mentioned that there is actually a previous page that lists all entries with an edit link next to each one which brings you to this page where is shows the populated field pulled from the ID. When you submit the changes here it directs back to the previous page showing the list again and hopefully with the updates. confirm_query($target); this is a function that does all the database error checking. I am gonna try what you posted and see if that works for me. Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted November 27, 2007 Share Posted November 27, 2007 When you submit the changes here it directs back to the previous page showing the list again and hopefully with the updates. Not with the code you posted it doesn't. When you submit the form, all it does is update the database. Nothing else. Thats assuming the code you postes is for blogedit.php By the way, i missed something out. I forgot to add the id in the url of the form's action: <?php if(isset($_GET['id'])){ $id = $_GET['id']; if(isset($_POST['update'])){ $text = $_POST['Entry']; mysql_query("UPDATE blogger SET blogtext = '$text' WHERE id = $id") or die(mysql_error());//i hope there's no need for the limit clause, since id should be a unique field } $sql = mysql_query("SELECT * FROM blogger WHERE id=$id") or die(mysql_error()); confirm_query($target); // no idea what this does, but ill leave it in $row = mysql_fetch_assoc($sql); //no need for a while statement - we're only working with one row $userx = $row['user']; $blogtextx = $row['blogtext']; $datex = $row['date']; ?> <form action="blogedit.php?id=<?php echo $id; ?>" method="post"> Date: <br><input name="Date" type="text" value="<?php echo $datex; ?>"><br> <br> Name: <br><input name="Name" type="text" value="<?php echo $userx; ?>"><br> <br> Entry: <br><textarea name="Entry" cols="100" rows="10" wrap="PHYSICAL"><?php echo $blogtextx; ?></textarea><br> <br> <input type="submit" name="update" value="update"> </form> <br><br> <?php }else{ echo 'No ID selected'; } ?> confirm_query($target); this is a function that does all the database error checking. You can't do the error checking AFTER you've queried the database. It kinda defeats the object of the error checking. Quote Link to comment Share on other sites More sharing options...
phatgreenbuds Posted November 27, 2007 Author Share Posted November 27, 2007 ok you fixed it. I was having this page (blogeditor.php) post back to the originating page (blogedit.php) for some reason it would not update and I think its because blogedit.php had no processing for the posted values. Once I set this to post to itself it updated fine. I need to rethink how I want this to work. Thank you for the help it made this a little more clear as to what I was doing wrong. Quote Link to comment Share on other sites More sharing options...
phatgreenbuds Posted November 27, 2007 Author Share Posted November 27, 2007 for the record... blogedit.php: <?php require_once("../admin/includes/dbopen.php"); ?> <?php include("../admin/functions/functions.php"); ?> <html> <head> <title>Update</title> <div id="bloggin"> <?php $date = date("m-d-Y"); ?> <form action="blogedit.php" method="post"> Date: <br><input name="Date" type="text" value="<?php echo $date; ?>"><br> <br> Name: <br><input name="Name" type="text" ><br> <br> Entry: <br><textarea name="Entry" cols="100" rows="10" wrap="PHYSICAL"></textarea><br> <br> <input type="submit" name="submit" value="update"> </form> <br><br> <?php if (empty($_POST['Entry'])){ //echo "Sorry but you must enter some text."; //exit; } elseif (isset($_POST['submit'])) { $user = $_POST['Name']; $entry = $_POST['Entry']; $query="INSERT INTO blogger (blogtext, user, date) VALUES ('$entry','$user','$date')"; $addblogs = mysql_query($query); confirm_query($addblogs); //calls to a function in the functions file. } $bloggins = get_all_blogs(); //calls to a function in the functions file. while ($blog = mysql_fetch_array($bloggins)) { ?> <table width="50%" border="1"> <tr><td width="15%" valign="top"><?php echo $blog["date"]; ?></td> <td width="75%"><?php echo $blog['blogtext']; ?></td> <td width="10%" valign="top"><?php echo "<a href=\"blogeditor.php?id={$blog['id']}\">Modify/Delete</a>"; ?></tr> </table> <?php } ?> <?php include("../admin/includes/dbclose.php"); ?> </div> </body> </html> and blogeditor.php: <?php require_once("../admin/includes/dbopen.php"); ?> <?php include("../admin/functions/functions.php"); ?> <html> <head> <title>Update</title> <div id="bloggin"> <?php if(isset($_GET['id'])){ $id = $_GET['id']; if(isset($_POST['update'])){ $text = $_POST['Entry']; mysql_query("UPDATE blogger SET blogtext = '$text' WHERE id = $id") or die(mysql_error()); } $sql = mysql_query("SELECT * FROM blogger WHERE id=$id") or die(mysql_error()); confirm_query($sql); $row = mysql_fetch_assoc($sql); $userx = $row['user']; $blogtextx = $row['blogtext']; $datex = $row['date']; ?> <form action="blogeditor.php?id=<?php echo $id; ?>" method="post"> Date: <br><input name="Date" type="text" value="<?php echo $datex; ?>"><br> <br> Name: <br><input name="Name" type="text" value="<?php echo $userx; ?>"><br> <br> Entry: <br><textarea name="Entry" cols="100" rows="10" wrap="PHYSICAL"><?php echo $blogtextx; ?></textarea><br> <br> <input type="submit" name="update" value="update"> </form> <br><br> <?php }else{ echo 'No ID selected'; } ?> <?php include("../admin/includes/dbclose.php"); ?> </div> </body> </html> 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.