justsomeguy Posted January 28, 2010 Share Posted January 28, 2010 If you use that code like you have, and someone clicks on the link to go your form, PHP will immediately try to grab everything from $_POST, update the database, and print the message. The user hasn't even submitted the form yet, and it's already trying to update the database. Earlier I posted how to use isset to check if they submitted the form. Link to comment Share on other sites More sharing options...
olilish Posted January 30, 2010 Author Share Posted January 30, 2010 Ok thanks, i understand that.However regarding my overall problem, I just want to check that I am going about things in the correct way.I have my database with a unique id for each record. I want to be able to edit a row in any record called 'body', like a very simple cms.I have a display all page where everything in the database appears with an edit link next to it. To reach the record you have clicked on I have used<a href=edit.html?id=" . $row['id'] . ">edit</a>. Which I understand to be appending (adding) the information in 'id' row to edit.html using GET.Once in the edit page I have used a similar piece of script to my diplay all page in between the <teaxarea> tags. Again as I understand this is calling the 'id' for the record using GET and filling the text area with the correct text for that record.So, Once the user clicks the submit button and the code mysql_query("UPDATE pages SET body='$body', h1='$h1' WHERE id=" . $ID);is run. I know from testing that the $body is being posted. But, I don't understand how I get my $ID because it does not seem to being passed.Is the way I am approcahing this correct, espsically from the point of view of expansion or should I be using SESSIONS.Thanks again.------------------------also, since then i seem to have been able to pass the 'edited' text and the id, as below:UPDATE pages SET body='ccc', h1='' WHERE id=Array ( [id] => 36 ) Thank you! Information updated. but i am still not seeing any changes? Link to comment Share on other sites More sharing options...
olilish Posted January 30, 2010 Author Share Posted January 30, 2010 Ok, I now seem to have it working. The problem is I do not really understand why! below are my html and the php i have the form posted to. could you briefly explain how i am managing to pass the id and if any of the code below is not really the correct way to do things? - and again thanks for all your help!my html edit form:<html><body><?php$ID = $_GET['id'];?><form method="post" action="edit.php?id=<?php echo $ID; ?>"> <textarea name="body"><?php$con = mysql_connect("xxx","xxx","xxx");if (!$con) { die('Could not connect: ' . mysql_error()); }mysql_select_db("xxx", $con);$ID = $_GET['id'];$result = mysql_query("SELECT * FROM pages WHERE id=" . $ID);while($row = mysql_fetch_array($result)) { echo $row['id'];; echo "Body: "; echo $row['body']; echo "<br>"; }mysql_close($con);?> </textarea> <input name="submit" type="submit"></form> </body></html>---------------------------MY php:<?php$con = mysql_connect("xxx","xxx","xxx");if (!$con) { die('Could not connect: ' . mysql_error()); }mysql_select_db("xxx");$ID=$_GET['id'];$body=$_POST['body'];mysql_query("UPDATE pages SET body='$body' WHERE id=" . $ID);echo "Thank you! Information updated.";mysql_close($con);?> Link to comment Share on other sites More sharing options...
justsomeguy Posted February 1, 2010 Share Posted February 1, 2010 You're passing the ID here:<form method="post" action="edit.php?id=<?php echo $ID; ?>">It's going into the URL. So the URL would look like this:edit.php?id=123Any variable like that on the URL is available via $_GET. Link to comment Share on other sites More sharing options...
olilish Posted February 4, 2010 Author Share Posted February 4, 2010 Thanks for your help! Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.