Page 1 of 1

Save form data after submitting to a page within website II

Posted: Fri Mar 09, 2012 11:36 am
by Blaade
I am struggling for some days to make the submitted data to stay in the My profile page and made a little progress on my own but got stuck and i need professional help.
This is my "myprofile.php" script (the page where the data is submitted to)

Code: Select all

<html>
<body>
<?php
$con = mysql_connect("localhost","user","pass");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("database", $con);
$sql="INSERT INTO profile (FirstName, LastName, Dateofbirth, Gender, Country, City, AboutMe, Email)
VALUES
('$_GET[fname]','$_GET[lname]','$_GET[day], $_GET[month], $_GET[year]','$GET_[gender]','$_GET[country]','$_GET[city]','$_GET[aboutme]','$_GET[email]')";
if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
$result = mysql_query("SELECT * FROM profile");
while($row = mysql_fetch_array($result))
  {
  echo '<span style="color:#E3E9F2;font-family:Courier New;font-size:13px;font-weight:bold;">First name:</span><span style="color:#EFF3F7;font-family:Courier New;font-size:13px;">  ' . $row['FirstName'] . '</span>';   
  echo "<br />";
  echo "<br />";
  echo '<span style="color:#E3E9F2;font-family:Courier New;font-size:13px;font-weight:bold;">Last name:</span><span style="color:#EFF3F7;font-family:Courier New;font-size:13px;">  ' . $row['LastName'] . '</span>';   
  echo "<br />";
  echo "<br />";
  echo '<span style="color:#E3E9F2;font-family:Courier New;font-size:13px;font-weight:bold;">Date of birth:</span><span style="color:#EFF3F7;font-family:Courier New;font-size:13px;">  ' . $row['Dateofbirth'] . '</span>';   
  echo "<br />";
  echo "<br />";
  echo '<span style="color:#E3E9F2;font-family:Courier New;font-size:13px;font-weight:bold;">Gender:</span><span style="color:#EFF3F7;font-family:Courier New;font-size:13px;">  ' . $row['Gender'] . '</span>';   
  echo "<br />";
  echo "<br />";
  echo '<span style="color:#E3E9F2;font-family:Courier New;font-size:13px;font-weight:bold;">Country:</span><span style="color:#EFF3F7;font-family:Courier New;font-size:13px;">  ' . $row['Country'] . '</span>';   
  echo "<br />";
  echo "<br />";
  echo '<span style="color:#E3E9F2;font-family:Courier New;font-size:13px;font-weight:bold;">City:</span><span style="color:#EFF3F7;font-family:Courier New;font-size:13px;">  ' . $row['City'] . '</span>';   
  echo "<br />";
  echo "<br />";
  echo '<span style="color:#E3E9F2;font-family:Courier New;font-size:13px;font-weight:bold;">About me:</span><span style="color:#EFF3F7;font-family:Courier New;font-size:13px;">  ' . $row['Aboutme'] . '</span>';   
  echo "<br />";
  echo "<br />";
  echo '<span style="color:#E3E9F2;font-family:Courier New;font-size:13px;font-weight:bold;">Email:</span><span style="color:#EFF3F7;font-family:Courier New;font-size:13px;">  ' . $row['Email'] . '</span>';   
  echo "<br />";
  echo "<br />";
  }
mysql_close($con);
?>
</body>
</html>
Now...when a user logs in, the My profile and Edit profile links appear. If he wants to take a look at the My profile just to check it out, instead of a blank layer with the title My profile, it appears:
First Name:
Last Name:
....... etc. (the My profile title doesn't dissappear)
and they stay there. If he reloads the page or exits the page and enters again, they appear over and over again. I think it's because that While{}, it makes things repeat themselves every time someone enters My profile page. If the user enters in the Edit profile first and enters his data, after submitting, he is directed to My profile page where the data seems to appear (except "About me:" which is a text area and "Gender:" where i've put a radio button; don't know why the data doesn't appear) and stay there. This is my little progress i was talking about, the data stays there but if he reloads or re-enters the page, the fields appear once more, every time the page is accessed.
Saw on the internet that some say i should use if (isset ['submit']), $_SESSION, cookies..but since i don't know coding that good i don't know how and where to implement them and can't take all the scripts from the internet because they are so different, almost unique and can't implement them into my script, i only take bits and pieces and put them together in my script until i reach dead end like now. Checked tutorials but without examples they are hard to understand...
Hope you understood my problem and can help out. Thanks!

Re: Save form data after submitting to a page within website

Posted: Fri Mar 09, 2012 11:47 am
by Celauran
Right now, this page is trying to create a new SQL record every time the page is loaded, then proceeds to get every entry in the profile table and display that information. That doesn't look right at all.

I see a number of problems:
  • No user ID, so there's no way to get the information for the user currently logged in.
  • GET over POST seems a strange choice
  • No checking if a form was submitted; it always tries to insert to the DB
  • No checking if the current user exists (ie. duplicate email)
  • No input validation or escaping

Re: Save form data after submitting to a page within website

Posted: Fri Mar 09, 2012 12:30 pm
by Blaade
Thought of those things too but only if i would knew how to implement them....tutorials aren't a lot of help, from there i took that While loop that inserts data over and over again...Thought of putting a "WHERE id=" but don't know what too write after, someone said to add a SESSION but didn't said how. I used GET instead of POST because if i use POST on my Edit profile (page from where the data is submitted to) after pushing submit, instead of directing me to My profile page it directs me to the index page and logs me off...so it only wanted to work like it suppose to with GET. For checking if the form was submitted i tried to use a isset($_POST['Submit']) but don't think i used it right because it didn't change anything. For the duplicate mail and input validation haven't got a clue right now...Been 5 days and still can't solve this form and like i said tutorials and stuff are only giving examples for the big picture...the rest i still have to figure it out by myself and my brain isn't so advanced:P
Thank you for the reply!