princeofpersia Posted January 3, 2011 Share Posted January 3, 2011 Hi guys, I have an update page where users can update their information, i have a different update page which works fine with almost the same code, but this one wont update anything. it does show the data but it wont update it, can u help please? <?php session_start(); include ("../../global.php"); //welcome messaage $username=$_SESSION['username']; echo "$username"; $query=mysql_query("SELECT id FROM users WHERE username='$username'"); while($row = mysql_fetch_assoc($query)) { $user_id = $row['id']; } $ref=$_GET['reference']; if (isset($_POST['register']) && $_POST['register']){ $title = addslashes(strip_tags($_POST['title'])); $update=mysql_query("UPDATE msg SET title ='$title' WHERE reference='$ref'"); } ?> <html> <head> </head> <body> <form action='edit-msgs.php' method='POST' enctype='multipart/form-data'> Title:<br /> <input type='text' name='title' id='title' value='<?php $getdata = "SELECT title FROM msg WHERE reference='$ref' AND referal_id='$user_id'"; $result = mysql_query($getdata); $row = mysql_fetch_assoc($result); echo $row['title'];?>'><p /> <input type='submit' name='register' value='Update'> </form> </body> </html> Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted January 3, 2011 Share Posted January 3, 2011 $ref is never set, so there is never a match to reference. $ref=$_GET['reference']; Quote Link to comment Share on other sites More sharing options...
princeofpersia Posted January 3, 2011 Author Share Posted January 3, 2011 can i ask what exactly do u mean? its there, how come it has not been set? Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted January 3, 2011 Share Posted January 3, 2011 1. there is no form field with name='reference' in your form. You need to add a field called reference to the form. 2. the form is POST'ed, so you won't GET any variables from it. You need to change it to $ref = $_POST['reference']; if you simply echo out your SQL, you'll see the missing value. $sql = "UPDATE msg SET title ='$title' WHERE reference='$ref'"; echo "sql: $sql <br />"; Quote Link to comment Share on other sites More sharing options...
princeofpersia Posted January 3, 2011 Author Share Posted January 3, 2011 it wont update the title, reference is used as users can edit their sent msgs and choose from a pagination, every msg has a reference number. so u mean i should change the GET to POST then? Quote Link to comment Share on other sites More sharing options...
princeofpersia Posted January 3, 2011 Author Share Posted January 3, 2011 when i changed it, it didnt show the data anymore Quote Link to comment Share on other sites More sharing options...
princeofpersia Posted January 3, 2011 Author Share Posted January 3, 2011 the sql echo gives me only sql: Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted January 3, 2011 Share Posted January 3, 2011 where is this value coming from? $_GET['reference']; Quote Link to comment Share on other sites More sharing options...
princeofpersia Posted January 3, 2011 Author Share Posted January 3, 2011 from previous page, as i mentioned, i have listed list of msgs belongs to a user, they can choose and edit each one in reference to their reference number for each msg I have used the same techniq to delete and it works <td><a href='edit-msgs.php?reference=".$row['reference']."'>Edit</a></td> Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted January 3, 2011 Share Posted January 3, 2011 yes. you use GET when the reference is passed in the URL. But when you check to see if the form is POST'ed, $_GET['reference'] is no longer set. anyway, this updated code should work. <?php session_start(); include ("../../global.php"); //welcome messaage $username=$_SESSION['username']; echo "$username"; $query=mysql_query("SELECT id FROM users WHERE username='$username'"); $row = mysql_fetch_assoc($query); $user_id = $row['id']; if ($_SERVER['REQUEST_METHOD'] == "POST"){ $ref = $_POST['ref']; $title = addslashes(strip_tags($_POST['title'])); $update=mysql_query("UPDATE msg SET title ='$title' WHERE reference='$ref'"); } else { $ref=$_GET['reference']; } ?> <html> <head> </head> <body> <form action='edit-msgs.php' method='POST' enctype='multipart/form-data'> Title:<br /> <input type='text' name='title' id='title' value='<?php $getdata = "SELECT title FROM msg WHERE reference='$ref' AND referal_id='$user_id'"; $result = mysql_query($getdata); $row = mysql_fetch_assoc($result); echo $row['title'];?>'><p /> <input type='hidden' name='reference' value='<?php echo $ref; ?>'> <input type='submit' name='register' value='Update'> </form> </body> </html> Quote Link to comment Share on other sites More sharing options...
princeofpersia Posted January 4, 2011 Author Share Posted January 4, 2011 thanks for help but i think you are missing the point, page should be loaded before the post, users see the information in text and post the modified changes, Quote Link to comment Share on other sites More sharing options...
jake2891 Posted January 4, 2011 Share Posted January 4, 2011 when you on the page that has the href tag with the reference if you hover over the edit link do you see a value for example ?reference=foo in the browser? I dont know if you echo out the content for the link tag or if its just html output but here are some things to try. if content is been echoded <?php $row['reference'] = 2; echo "<td><a href='edit-msgs.php?reference=".$row['reference']."'>Edit</a></td>"; ?> else if just html in php page <td><a href='edit-msgs.php?reference=<?=$row['reference']?>'>Edit</a></td> 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.