PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/editProfile.php

https://bitbucket.org/wlynch92/cs336-dbproject
PHP | 280 lines | 236 code | 42 blank | 2 comment | 40 complexity | 4d5b057e9c5e2cabe5067a7ffdb3e7aa MD5 | raw file
  1. <html>
  2. <head><title>Edit Profile</title></head>
  3. <body>
  4. <a href="/"><h1>Music Box</h1></a>
  5. A social music site for everyone!
  6. <p><center><hr width=100% noshade=noshade></center>
  7. <h2> Edit Profile </h2>
  8. <i>* = required field</i><p>
  9. <form action="editProfile.php" method="POST">
  10. <?php
  11. session_start();
  12. $con = mysql_connect("cs336-64.rutgers.edu", "csuser", "cs277315");
  13. if (!$con) {
  14. die('cannot connect: '.mysql_error());
  15. }
  16. mysql_select_db("cs336",$con);
  17. /* User Information */
  18. echo "<b>User Information</b><br>";
  19. echo "First name: <input type=\"textbox\" name=\"fname\" value=\"".$_SESSION['firstname']."\"><br/>\n";
  20. echo "Last name: <input type=\"textbox\" name=\"lname\" value=\"".$_SESSION['lastname']."\"><br/>\n";
  21. echo "Birthdate: <input type=\"date\" name=\"bdate\" value=\"".$_SESSION['birth']."\">*<br/>\n";
  22. echo "Email: <input type=\"textbox\" name=\"email\" value=\"".$_SESSION['email']."\"><br/>\n";
  23. echo "Address: <input type=\"textbox\" name=\"address\" value=\"".$_SESSION['address']."\"><br/>\n";
  24. echo "Gender: <select name=\"gender\">\n";
  25. if ($_SESSION['gender']=="Male"){
  26. echo "<option value=\"Male\" selected>Male</option>\n";
  27. echo "<option value=\"Female\">Female</option>\n";
  28. } else {
  29. echo "<option value=\"Male\">Male</option>\n";
  30. echo "<option value=\"Female\" selected>Female</option>\n";
  31. }
  32. echo "</select>*<br/>\n";
  33. echo "Profile Picture URL: <input type=\"textbox\" name=\"purl\" value=\"".$_SESSION['picurl']."\"><br/>\n";
  34. echo "<input type=\"submit\" value=\"update\">\n";
  35. ?>
  36. </form>
  37. <p>
  38. <!-- education information -->
  39. <form action="editProfile.php" method="POST">
  40. <?php
  41. $curruser = $_SESSION['username'];
  42. $education = mysql_query("SELECT * FROM user u, attended a, school s WHERE u.uid=a.uid AND a.sid=s.sid AND u.username='$curruser'");
  43. $schoolrow = mysql_fetch_array($education);
  44. $schoollist = mysql_query("SELECT * FROM school");
  45. echo "<b>Education</b><br>";
  46. echo "School: <select name=\"school\">\n";
  47. echo "<option value=\"".$schoolrow['sname']."\" selected>".$schoolrow['sname']."<br>\n";
  48. while ($school = mysql_fetch_array($schoollist)) {
  49. if ($school['sname'] == $schoolrow['sname']) continue;
  50. echo "<option value=\"".$school['sname']."\">".$school['sname']."</option>";
  51. }
  52. echo "</select>*<br>\n";
  53. echo "Start date: <input type=\"date\" name=\"sdate\" value=\"".$schoolrow['start']."\"><br>\n";
  54. echo "End date: <input type=\"date\" name=\"edate\" value=\"".$schoolrow['end']."\"><br>\n";
  55. echo "Degree: <input type=\"textbox\" name=\"degree\" value=\"".$schoolrow['degree']."\">*<br>\n";
  56. echo "<input type=\"submit\" value=\"update\">\n";
  57. ?>
  58. </form>
  59. <!-- employment information -->
  60. <?php
  61. $curruser = $_SESSION['username'];
  62. $employment = mysql_query("SELECT * FROM user u, company c, employment e WHERE u.uid=e.uid AND c.cid=e.cid AND u.username='$curruser'");
  63. $jobinfo = mysql_fetch_array($employment);
  64. // can only edit information if user has employment
  65. if (mysql_num_rows($employment) != 0) {
  66. echo "<form action=\"editProfile.php\" method=\"POST\">\n";
  67. echo "<b>Employment</b><br>";
  68. echo "Employer: <input type=\"textbox\" name=\"employer\" value=\"".$jobinfo['employer_name']."\">*<br>\n";
  69. echo "Address: <input type=\"textbox\" name=\"address\" value=\"".$jobinfo['address']."\"><br>\n";
  70. echo "Position: <input type=\"textbox\" name=\"position\" value=\"".$jobinfo['job_title']."\"><br>\n";
  71. echo "Salary (per hour): <input type=\"textbox\" name=\"salary\" value=\"".$jobinfo['salary']."\"><br>\n";
  72. echo "Start date: <input type=\"date\" name=\"jsdate\" value=\"".$jobinfo['start']."\"><br>\n";
  73. echo "End date: <input type=\"date\" name=\"jedate\" value=\"".$jobinfo['end']."\"><br>\n";
  74. echo "<input type=\"submit\" name=\"editemployment\" value=\"update\">\n";
  75. echo "</form>\n";
  76. }
  77. ?>
  78. <h2>Add more information</h2>
  79. <!-- employment information -->
  80. <form action="editProfile.php" method="POST">
  81. <b>Employment</b><br>
  82. Employer: <input type="textbox" name="employer">*<br>
  83. Address: <input type="textbox" name="address"><br>
  84. Position: <input type="textbox" name="position"><br>
  85. Salary (per hour): <input type="textbox" name="salary"><br>
  86. Start date: <input type="date" name="jsdate"><br>
  87. End date: <input type="date" name="jedate"><br>
  88. <input type="submit" name="addemployment" value="add">
  89. </form><p>
  90. <!-- activities information -->
  91. <form action="editProfile.php" method="POST">
  92. <b>Interests</b><br>
  93. Activity: <input type="textbox" name="interest">*
  94. <input type="submit" value="add">
  95. </form>
  96. </body>
  97. </html>
  98. <?php
  99. function setval($post){
  100. if ($post) {
  101. return "'".$post."'";
  102. } else {
  103. return "NULL";
  104. }
  105. }
  106. session_start();
  107. if ($_POST['gender'] && $_POST['bdate']) {
  108. $fname=setval($_POST['fname']);
  109. $lname=setval($_POST['lname']);
  110. $bdate=setval($_POST['bdate']);
  111. $email=setval($_POST['email']);
  112. $address=setval($_POST['address']);
  113. $gender=setval($_POST['gender']);
  114. $purl=setval($_POST['purl']);
  115. $uid=$_SESSION['uid'];
  116. $query = "UPDATE user SET gender=$gender, address=$address, firstname=$fname, lastname=$lname, email=$email, birth=$bdate, picurl=$purl WHERE uid=$uid";
  117. $res = mysql_query($query,$con);
  118. if (!$res){
  119. echo "Error in updating user information: ".mysql_error()."\n<br/>\n";
  120. mysql_close($con);
  121. } else {
  122. echo "user info done\n";
  123. mysql_close($con);
  124. $_SESSION['gender']=$_POST['gender'];
  125. $_SESSION['address']=$_POST['address'];
  126. $_SESSION['firstname']=$_POST['fname'];
  127. $_SESSION['lastname']=$_POST['lname'];
  128. $_SESSION['email']=$_POST['email'];
  129. $_SESSION['birth']=$_POST['bdate'];
  130. $_SESSION['picurl']=$_POST['purl'];
  131. header("Location: /profile.php");
  132. exit;
  133. }
  134. }
  135. else if ($_POST['school'] && $_POST['degree']) {
  136. $uid = $_SESSION['uid'];
  137. $sname = setval($_POST['school']);
  138. $srow = mysql_fetch_array(mysql_query("SELECT sid FROM school WHERE sname=$sname"));
  139. $sid = $srow['sid'];
  140. $sdate = setval($_POST['sdate']);
  141. $edate = setval($_POST['edate']);
  142. $degree = setval($_POST['degree']);
  143. $row = mysql_fetch_array(mysql_query("SELECT a.uid, a.sid, a.degree FROM attended a, school s, user u WHERE u.uid=a.uid AND s.sid=a.sid AND u.uid=$uid"));
  144. $oldsid=$row['sid'];
  145. $olddegree=$row['degree'];
  146. $squery = "UPDATE attended SET start=$sdate, end=$edate, sid=$sid, degree=$degree WHERE uid=$uid AND sid=$oldsid AND degree='$olddegree'";
  147. $sres = mysql_query($squery, $con);
  148. if (!$sres){
  149. echo "Error in updating education information: ".mysql_error()."\n<br/>\n";
  150. mysql_close($con);
  151. } else {
  152. mysql_close($con);
  153. header("Location: /profile.php");
  154. exit;
  155. }
  156. }
  157. else if ($_POST['employer']) {
  158. $uid = $_SESSION['uid'];
  159. $curruser = $_SESSION['username'];
  160. $employer = setval($_POST['employer']);
  161. if (isset($_POST['editemployment'])) {
  162. $erow = mysql_fetch_array(mysql_query("SELECT e.cid FROM user u, company c, employment e WHERE u.uid=e.uid AND c.cid=e.cid AND u.username='$curruser'"));
  163. $cid = $erow['cid'];
  164. }
  165. else {
  166. $jquery = "SELECT MAX(cid) as max FROM company";
  167. $jres = mysql_query($jquery, $con);
  168. if (!$jres) {
  169. echo "error\n";
  170. die(mysql_error());
  171. }
  172. $row = mysql_fetch_array($jres);
  173. $cid = $row['max']+1;
  174. }
  175. $address = setval($_POST['address']);
  176. $position = setval($_POST['position']);
  177. $salary = setval($_POST['salary']);
  178. $jsdate = setval($_POST['jsdate']);
  179. $jedate = setval($_POST['jedate']);
  180. if (isset($_POST['editemployment'])) {
  181. echo "edit employment info\n";
  182. $cquery = "UPDATE company SET employer_name=$employer, address=$address WHERE cid=$cid";
  183. $jquery = "UPDATE employment SET job_title=$position, salary=$salary, start=$jsdate, end=$jedate WHERE cid=$cid AND uid=$uid";
  184. }
  185. else {
  186. echo "add employment info\n";
  187. $cquery = "INSERT INTO company VALUES ($cid, $employer, $address)";
  188. $jquery = "INSERT INTO employment VALUES ($position, $salary, $uid, $cid, $jsdate, $jedate)";
  189. }
  190. $cres = mysql_query($cquery, $con);
  191. $jres = mysql_query($jquery, $con);
  192. if (!$cres || !$jres) {
  193. echo "Error in updating employment information: ".mysql_error()."\n<br/>\n";
  194. mysql_close($con);
  195. }
  196. else {
  197. mysql_close($con);
  198. header("Location: /profile.php");
  199. exit;
  200. }
  201. }
  202. else if ($_POST['interest']) {
  203. $new = 0; // check if activity already exists
  204. $uid = $_SESSION['uid'];
  205. $aname = setval($_POST['interest']);
  206. $findact = mysql_query("SELECT aid FROM activity WHERE aname=$aname");
  207. if (mysql_num_rows($findact) == 0) {
  208. $ares = mysql_query("SELECT MAX(aid) as max FROM activity");
  209. $row = mysql_fetch_array($ares);
  210. $aid = $row['max']+1;
  211. $new = 1;
  212. }
  213. else {
  214. $actnum = mysql_fetch_array($findact);
  215. $aid = $actnum['aid'];
  216. }
  217. echo $aname;
  218. echo $aid;
  219. if ($new == 1) {
  220. $actquery = "INSERT INTO activity VALUES ($aname, $aid)";
  221. $actinsert = mysql_query($actquery, $con);
  222. if (!$actinsert) {
  223. echo "Error in inserting new activity: ".mysql_error()."\n<br>\n";
  224. mysql_close($con);
  225. }
  226. }
  227. $intquery = "INSERT INTO interested_in VALUES ($aid, $uid)";
  228. $ires = mysql_query($intquery, $con);
  229. if (!$ires) {
  230. echo "Error in inserting new activity: ".mysql_error()."\n<br/>\n";
  231. mysql_close($con);
  232. } else {
  233. mysql_close($con);
  234. header("Location: /profile.php");
  235. exit;
  236. }
  237. }
  238. ?>