Highland3r Posted December 22, 2008 Share Posted December 22, 2008 i have gained a little knowledge in php and mySQL now the problem i have is i am trying to develop a code to edit articles now almost the whole code works apart from the submit part of the code i think? when i log-in to edit the Articles it displays the data allows me to edit the data when i click submit it should post the new querys to mySQL insted it just takes me back to the editArticle.php page and does not edit the article, Can anybody help please Here is the code <?php require_once("../includes/Sentry.php"); $sentry = new Sentry(); if ($HTTP_POST_VARS['user'] != ''){ $sentry->checkLogin($HTTP_POST_VARS['user'],$HTTP_POST_VARS['pass'],4,'welcome.php','failed.php'); } if ($HTTP_GET_VARS['action'] == 'logout'){ if ($sentry->logout()){ echo '<center>You have been logged out</center><br>'; } } ?> <? // Require the classes require_once('../includes/DbConnector.php'); require_once('../includes/Validator.php'); // Create an object (instance) of the DbConnector and Validator $connector = new DbConnector(); $validator = new Validator(); //select which database you want to edit mysql_select_db("web176-contentms"); //If cmd has not been initialized if(!isset($cmd)) { //display all the thearticle $result = mysql_query("select * from cmsarticles order by id"); //run the while loop that grabs all the thearticle scripts while($r=mysql_fetch_array($result)) { //grab the title and the ID of the thearticle $title=$r["title"];//take out the title $id=$r["ID"];//take out the id //make the title a link echo "<a href='editArticle.php?cmd=edit&id=$id'>$title - Edit</a>"; echo "<br>"; } } ?> <? if($_GET["cmd"]=="edit" || $_POST["cmd"]=="edit") { if (!isset($_POST["Submit"])) { $id = $_GET["id"]; $sql = "SELECT * FROM cmsarticles WHERE id=$id"; $result = mysql_query($sql); $myrow = mysql_fetch_array($result); ?> <form action="editArticle.php" method="post"> <input type=hidden name="ID" value="<?php echo $myrow["id"] ?>"> Title:<INPUT TYPE="TEXT" NAME="title" VALUE="<?php echo $myrow["title"] ?>" SIZE=30><br> Message:<TEXTAREA NAME="thearticle" ROWS=10 COLS=30><? echo $myrow["thearticle"] ?></TEXTAREA> <br> <input type="hidden" name="cmd" value="edit"> <input type="Submit" name="Submit" value="Submit"> </form> <? } ?> <? if ($_POST["$Submit"]) { $title = $_POST["title"]; $thearticle = $_POST["thearticle"]; $sql = "UPDATE cmsarticles SET title='$title',thearticle='$thearticle' WHERE id=$id"; //replace theArticles with your table name above $result = mysql_query($sql); echo "Thank you! Information updated."; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/138023-solved-content-managment-system/ Share on other sites More sharing options...
ngreenwood6 Posted December 22, 2008 Share Posted December 22, 2008 First of all you should use code tags when you post on this site, it make the code much easier to read. Secondly, It looks like to me that you have too much going on in one page, is there any reason that you can't separate the pages Quote Link to comment https://forums.phpfreaks.com/topic/138023-solved-content-managment-system/#findComment-721397 Share on other sites More sharing options...
Highland3r Posted December 22, 2008 Author Share Posted December 22, 2008 there is no reason for it all being on same page still learning, any help would be greatly appreciated Quote Link to comment https://forums.phpfreaks.com/topic/138023-solved-content-managment-system/#findComment-721404 Share on other sites More sharing options...
ngreenwood6 Posted December 22, 2008 Share Posted December 22, 2008 I would take this form and make it post to another page like this: <form action="article_edited.php" method="post"> <input type=hidden name="ID" value="<?php echo $myrow["id"] ?>"> Title:<INPUT TYPE="TEXT" NAME="title" VALUE="<?php echo $myrow["title"] ?>" SIZE=30><br> Message:<TEXTAREA NAME="thearticle" ROWS=10 COLS=30><? echo $myrow["thearticle"] ?></TEXTAREA> <br> <input type="hidden" name="cmd" value="edit"> <input type="Submit" name="Submit" value="Submit"> </form> then on the article_edited.php page you would then put this: <? if ($_POST["$Submit"]) { $id = $_POST["id"]; $title = $_POST["title"]; $thearticle = $_POST["thearticle"]; $sql = "UPDATE cmsarticles SET title='$title',thearticle='$thearticle' WHERE id=$id"; //replace theArticles with your table name above $result = mysql_query($sql); echo "Thank you! Information updated."; } } of course this is just a sample of what you would do and you would have to do the mysql connectiosn again on this page. hopefully that helps. Quote Link to comment https://forums.phpfreaks.com/topic/138023-solved-content-managment-system/#findComment-721598 Share on other sites More sharing options...
Highland3r Posted December 22, 2008 Author Share Posted December 22, 2008 i have noticed that in the last bit of code <? if ($_POST["[b]$[/b]Submit"]) <-------Here should only be if ($_POST["Submit"]) now i am having a problem with this part i beleve $sql="UPDATE cmsarticles SET thearticle='$thearticle' WHERE ID =$id"; $result = mysql_query($sql); echo "Thank you! Information updated."; as i get the message Thank you! Information updated. but does not update the data any help would be great Quote Link to comment https://forums.phpfreaks.com/topic/138023-solved-content-managment-system/#findComment-721607 Share on other sites More sharing options...
ngreenwood6 Posted December 22, 2008 Share Posted December 22, 2008 have you tried doing this: $result = mysql_query($sql) or die(mysql_errror()); Let me know if it gives you an error. Quote Link to comment https://forums.phpfreaks.com/topic/138023-solved-content-managment-system/#findComment-721632 Share on other sites More sharing options...
Highland3r Posted December 22, 2008 Author Share Posted December 22, 2008 i have aplied the code to the script with no success for some reason now when i click my edit button after making changes it updates the information in id 3 even tho i changed the information in ID 1 or 2 so i cant be far from a solution Quote Link to comment https://forums.phpfreaks.com/topic/138023-solved-content-managment-system/#findComment-721660 Share on other sites More sharing options...
ngreenwood6 Posted December 22, 2008 Share Posted December 22, 2008 can you post both of the pages that you have now? Please use the code tags. Quote Link to comment https://forums.phpfreaks.com/topic/138023-solved-content-managment-system/#findComment-721666 Share on other sites More sharing options...
Highland3r Posted December 22, 2008 Author Share Posted December 22, 2008 the code looks like <? // Require the classes require_once('../includes/DbConnector.php'); require_once('../includes/Validator.php'); // Create an object (instance) of the DbConnector and Validator $connector = new DbConnector(); $validator = new Validator(); //select which database you want to edit mysql_select_db("web176-contentms"); //If cmd has not been initialized if(!isset($cmd)) { //display all the thearticle $result = mysql_query("select * from cmsarticles order by id"); //run the while loop that grabs all the thearticle scripts while($r=mysql_fetch_array($result)) { //grab the title and the ID of the thearticle $title=$r["title"];//take out the title $id=$r["ID"];//take out the id echo "<br>"; } } ?> <? if($_GET["cmd"]=="edit" || $_POST["cmd"]=="edit") { if (!isset($_POST["Submit"])) { $id = $_GET["id"]; $sql = "SELECT * FROM cmsarticles WHERE id=$id"; $result = mysql_query($sql); $myrow = mysql_fetch_array($result); ?> </span></span> <form action="editArticle.php" method="POST"> <input type=hidden name="ID" value="<?php echo $myrow["id"] ?>"> <TEXTAREA NAME="thearticle" COLS=40 ROWS=10 class="quoteEdit"><? echo $myrow["thearticle"] ?></TEXTAREA> <br> <input type="hidden" name="cmd" value="edit"> <input name="Submit" type="Submit" id="Submit" value="Edit"> </form> <? } ?> <? if ($_POST["Submit"]) { $thearticle = $_POST["thearticle"]; $sql = "UPDATE cmsarticles SET thearticle='$thearticle' WHERE ID =$id"; //replace thearticle with your table name above $result = mysql_query($sql); $result = mysql_query($sql) or die(mysql_error()); echo "Thank you! Information updated."; } } ?> i was unable to place it onto 2 pages just gives loads of errors Quote Link to comment https://forums.phpfreaks.com/topic/138023-solved-content-managment-system/#findComment-721670 Share on other sites More sharing options...
ngreenwood6 Posted December 22, 2008 Share Posted December 22, 2008 Try this: <?php // Require the classes require_once('../includes/DbConnector.php'); require_once('../includes/Validator.php'); //get the id $id = $_GET["id"]; // Create an object (instance) of the DbConnector and Validator $connector = new DbConnector(); $validator = new Validator(); //select which database you want to edit mysql_select_db("web176-contentms"); //If cmd has not been initialized if(!isset($cmd)) { //display all the thearticle $result = mysql_query("select * from cmsarticles order by id"); //run the while loop that grabs all the thearticle scripts while($r=mysql_fetch_array($result)) { //grab the title and the ID of the thearticle $title=$r["title"];//take out the title $id=$r["ID"];//take out the id echo "<br>"; } } if($_GET["cmd"]=="edit") { if (!isset($_POST["Submit"])) { $sql = "SELECT * FROM cmsarticles WHERE id='$id'"; $result = mysql_query($sql); $myrow = mysql_fetch_array($result); ?> </span></span> <form action="editArticle.php" method="POST"> <input type=hidden name="ID" value="<?php echo $id; ?>"> <TEXTAREA NAME="thearticle" COLS=40 ROWS=10 class="quoteEdit"><? echo $myrow["thearticle"] ?></TEXTAREA> <br> <input type="hidden" name="cmd" value="edit"> <input name="Submit" type="Submit" id="Submit" value="Edit"> </form> <?php } if ($_POST["Submit"]) { $thearticle = $_POST["thearticle"]; $sql = "UPDATE cmsarticles SET thearticle='$thearticle' WHERE ID ='$id'"; //replace thearticle with your table name above $result = mysql_query($sql) or die(mysql_error()); echo "Thank you! Information updated."; } } ?> I cleaned up the code a little bit. You should use <?php instead of <? because it makes it more universal. Secondly you only need to end php ?> when you are switching to html. Also I changed it to get the id without requiring the cmd to be edit. it does not necessarily need to be set to get the id and set it to a variable. if you get any errors please let me know. Quote Link to comment https://forums.phpfreaks.com/topic/138023-solved-content-managment-system/#findComment-721685 Share on other sites More sharing options...
Highland3r Posted December 22, 2008 Author Share Posted December 22, 2008 ok without the $_POST["cmd"]=="edit") i was just getting returned to the editArticle.php with no change so i put that back in and now when i submit an update it changes all the "thrarticle" to my update ? so instead of it updating id 3 it changes id 1,2,3 to all the same article getting closer to the fix tho Quote Link to comment https://forums.phpfreaks.com/topic/138023-solved-content-managment-system/#findComment-721700 Share on other sites More sharing options...
ngreenwood6 Posted December 22, 2008 Share Posted December 22, 2008 the problem here is this line i believe: if(!isset($cmd)) the reason is because $cmd does not get a value set until after you post the form so it is running that section which gets all the articles. is the cmd passed through the url? Quote Link to comment https://forums.phpfreaks.com/topic/138023-solved-content-managment-system/#findComment-721706 Share on other sites More sharing options...
ngreenwood6 Posted December 22, 2008 Share Posted December 22, 2008 The other problem i see is in the last query (sorry just noticed it): if ($_POST["Submit"]) { $thearticle = $_POST["thearticle"]; $sql = "UPDATE cmsarticles SET thearticle='$thearticle' WHERE ID ='$id'"; //replace thearticle with your table name above $result = mysql_query($sql) or die(mysql_error()); echo "Thank you! Information updated."; } should be: if ($_POST["Submit"]) { $thearticle = $_POST["thearticle"]; $sql = "UPDATE cmsarticles SET thearticle='$thearticle' WHERE id ='$id'"; //replace thearticle with your table name above $result = mysql_query($sql) or die(mysql_error()); echo "Thank you! Information updated."; } the only thing I did was change the ID to id in the query. the fields are case sensitive. Quote Link to comment https://forums.phpfreaks.com/topic/138023-solved-content-managment-system/#findComment-721709 Share on other sites More sharing options...
Highland3r Posted December 22, 2008 Author Share Posted December 22, 2008 nope still does the same ??? i really cannot understand this problem Quote Link to comment https://forums.phpfreaks.com/topic/138023-solved-content-managment-system/#findComment-721711 Share on other sites More sharing options...
ngreenwood6 Posted December 22, 2008 Share Posted December 22, 2008 try this: <?php // Require the classes require_once('../includes/DbConnector.php'); require_once('../includes/Validator.php'); //get the id $id = $_GET["id"]; $cmd = $_GET["cmd"]; // Create an object (instance) of the DbConnector and Validator $connector = new DbConnector(); $validator = new Validator(); //select which database you want to edit mysql_select_db("web176-contentms"); //If cmd has not been initialized if(!isset($cmd)) { //display all the thearticle $result = mysql_query("select * from cmsarticles order by id"); //run the while loop that grabs all the thearticle scripts while($r=mysql_fetch_array($result)) { //grab the title and the ID of the thearticle $title=$r["title"];//take out the title $id=$r["ID"];//take out the id echo "<br>"; } } if($_GET["cmd"]=="edit" || $_POST["cmd"]=="edit") { if (!isset($_POST["Submit"])) { $sql = "SELECT * FROM cmsarticles WHERE id='$id'"; $result = mysql_query($sql); $myrow = mysql_fetch_array($result); ?> </span></span> <form action="editArticle.php" method="POST"> <input type=hidden name="ID" value="<?php echo $id; ?>"> <TEXTAREA NAME="thearticle" COLS=40 ROWS=10 class="quoteEdit"><? echo $myrow["thearticle"] ?></TEXTAREA> <br> <input type="hidden" name="cmd" value="edit"> <input name="Submit" type="Submit" id="Submit" value="Edit"> </form> <?php } if ($_POST["Submit"]) { $thearticle = $_POST["thearticle"]; $sql = "UPDATE cmsarticles SET thearticle='$thearticle' WHERE id='$id'"; //replace thearticle with your table name above $result = mysql_query($sql) or die(mysql_error()); echo "Thank you! Information updated."; } } ?> let meknow if you get any errors also let me know what the url looks like when you first visit the page. Quote Link to comment https://forums.phpfreaks.com/topic/138023-solved-content-managment-system/#findComment-721714 Share on other sites More sharing options...
Highland3r Posted December 22, 2008 Author Share Posted December 22, 2008 Nope didnt work once again if i update id on when i click the submit it updates the highest id in the table but leaves the rest of the id the way they were before ? Quote Link to comment https://forums.phpfreaks.com/topic/138023-solved-content-managment-system/#findComment-721725 Share on other sites More sharing options...
ngreenwood6 Posted December 22, 2008 Share Posted December 22, 2008 what does the url look like when you first visit the page: http://yoursite.com/index.php?cmd=edit and what does it look like after you hit submit. Quote Link to comment https://forums.phpfreaks.com/topic/138023-solved-content-managment-system/#findComment-721727 Share on other sites More sharing options...
Highland3r Posted December 22, 2008 Author Share Posted December 22, 2008 dont get any errors when i go to my editing page the url looks like this http://mydomain.co.uk/cmsadmin/editArticle.php?cmd=edit&id=1 when i click submit/edit it sends me to http://mydomain.co.uk/cmsadmin/editArticle.php with a message of Thank you! Information updated. no errors ??? the thing is it only updates id=3 or the highest id i should say as 3 is the highest id in my table Quote Link to comment https://forums.phpfreaks.com/topic/138023-solved-content-managment-system/#findComment-721740 Share on other sites More sharing options...
ngreenwood6 Posted December 22, 2008 Share Posted December 22, 2008 ok I think I have got it now try this: <?php // Require the classes require_once('../includes/DbConnector.php'); require_once('../includes/Validator.php'); //get the id $id = $_GET["id"]; $cmd = $_GET["cmd"]; // Create an object (instance) of the DbConnector and Validator $connector = new DbConnector(); $validator = new Validator(); //select which database you want to edit mysql_select_db("web176-contentms"); //If cmd has not been initialized if(!isset($cmd)) { //display all the thearticle $result = mysql_query("select * from cmsarticles order by id"); //run the while loop that grabs all the thearticle scripts while($r=mysql_fetch_array($result)) { //grab the title and the ID of the thearticle $title=$r["title"];//take out the title $id=$r["ID"];//take out the id echo "<br>"; } } if($_GET["cmd"]=="edit" || $_POST["cmd"]=="edit") { if (!isset($_POST["Submit"])) { $sql = "SELECT * FROM cmsarticles WHERE id='$id'"; $result = mysql_query($sql); $myrow = mysql_fetch_array($result); ?> </span></span> <form action="editArticle.php?cmd=<?php echo $cmd; ?>&ID=<?php echo $id; ?>" method="POST"> <TEXTAREA NAME="thearticle" COLS=40 ROWS=10 class="quoteEdit"><? echo $myrow["thearticle"] ?></TEXTAREA> <br> <input type="hidden" name="cmd" value="edit"> <input name="Submit" type="Submit" id="Submit" value="Edit"> </form> <?php } if ($_POST["Submit"]) { $thearticle = $_POST["thearticle"]; $sql = "UPDATE cmsarticles SET thearticle='$thearticle' WHERE id='$id'"; //replace thearticle with your table name above $result = mysql_query($sql) or die(mysql_error()); echo "Thank you! Information updated."; } } ?> the code was not being passed to the second page. well it was but by a post and you were not getting if from the post so I changed it so that when it posts it posts the url. Quote Link to comment https://forums.phpfreaks.com/topic/138023-solved-content-managment-system/#findComment-721753 Share on other sites More sharing options...
Highland3r Posted December 22, 2008 Author Share Posted December 22, 2008 u are a star mate canot thank you enough the code was perfect only one thing <form action="editArticle.php?cmd=<?php echo $cmd; ?>&ID=<?php echo $id; ?>" method="POST"> the >&ID needed to be lowercase but fixed it for me thanks pal Quote Link to comment https://forums.phpfreaks.com/topic/138023-solved-content-managment-system/#findComment-721756 Share on other sites More sharing options...
ngreenwood6 Posted December 22, 2008 Share Posted December 22, 2008 no problem, yeah sorry about the uppercase I have looked at this post so many times I forgot which one it was lol. glad to see it working. If you have any more questions feel free to ask. Quote Link to comment https://forums.phpfreaks.com/topic/138023-solved-content-managment-system/#findComment-721758 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.