Advertisement
Guest User

Untitled

a guest
Feb 3rd, 2016
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.47 KB | None | 0 0
  1. exer4.php
  2. <?php
  3. $action=$_GET['action'];
  4. $id=$_GET['id'];
  5. $db=mysqli_connect('localhost', 'root', '', 'carillo_exer4');
  6.  
  7. if($action=='delete'){
  8. $sql="DELETE FROM user WHERE id=".$id;
  9. }else{
  10. $username=$_POST['username'];
  11. $password=$_POST['password'];
  12. $gender=$_POST['gender'];
  13. $source=$_POST['source'];
  14. $list=isset($_POST['list']);
  15.  
  16. if($action =='edit'){
  17. $sql="UPDATE user SET
  18. username='$username',
  19. password='$password',
  20. gender='$gender',
  21. source='$source',
  22. list='$list'
  23. WHERE id=".$id;
  24. }
  25. else{
  26. $sql="INSERT INTO user(username, password, gender, source, list)
  27. VALUES('$username','$password','$gender','$source','$list')";
  28. }
  29. }
  30. mysqli_query($db, $sql);
  31. header('Location:exer4s.php');
  32. ?>
  33.  
  34.  
  35. exer4s.php
  36. <html>
  37. <head><title>CS321 Exercise 4</title></head>
  38. <body>
  39. <?php
  40. $labels=array('Id', 'Username', 'Password', 'Gender', 'Source', 'List', 'Action');
  41. ?>
  42. <table border="1">
  43. <?php
  44. foreach($labels as $label){?>
  45. <th>
  46. <?php echo $label?>
  47. </th>
  48. <?php } ?>
  49. <?php
  50. $ctr=1;
  51. $db=mysqli_connect('localhost', 'root', '', 'carillo_exer4');
  52. $sql="SELECT * FROM user";
  53. $result=mysqli_query($db, $sql);
  54. while($row=mysqli_fetch_array($result)){?>
  55. <tr>
  56. <td><?php echo $ctr++ ?></td>
  57. <td><?php echo $row['username']?></td>
  58. <td><?php echo $row['password']?></td>
  59. <td><?php echo $row['gender']?></td>
  60. <td><?php echo $row['source']?></td>
  61. <td><?php echo $row['list']?></td>
  62. <td><a href="exer4_view.php?action=edit&id=<?php echo $row['id']?>">Update</a>|
  63. <a href="exer4.php?action=delete&id=<?php echo $row['id']?>">Delete</a></td>
  64. </tr>
  65. <?php } ?>
  66. </table>
  67. <a href="exer4_view.php?action=add&id=new">Add</a>
  68. </body>
  69. </html>
  70.  
  71. exer4_view.php
  72.  
  73. <?php
  74. if(isset($_GET['action'])){
  75. $action=$_GET['action'];
  76. $id=$_GET['id'];
  77.  
  78. if($action=='add'){
  79. $btn='Register Now';
  80. $row=null;
  81. }
  82. else{
  83. $db=mysqli_connect('localhost', 'root', '', 'carillo_exer4');
  84. $sql="SELECT * FROM user WHERE id=".$id;
  85. $result=mysqli_query($db, $sql);
  86. $row=mysqli_fetch_array($result);
  87. $btn="Update";
  88. }
  89. }
  90. else{
  91. $btn='Register Now';
  92. $row=null;
  93. }
  94.  
  95. ?>
  96. <html>
  97. <head><title>CS321 Exercise 4</title></head>
  98. <link rel="stylesheet" href="exer2.css"></link>
  99. <body>
  100. <form class="exer2" action="exer4.php?action=<?php echo $action?>&id=<?php echo $id ?>" method="POST">
  101. <h1>User Registration</h1>
  102. <div class="h3">Please complete the following form to register with our site:</div>
  103.  
  104. <br><br>
  105. <div class="a">User Name:</div>
  106. <div class="a"><input type="text" name="username" value="<?php echo $row['username']?>"></div>
  107. <br>
  108.  
  109. <div class="a">Password:</div>
  110. <div class="b"><input type="password" name="password" value="<?php echo $row['password']?>"></div>
  111.  
  112. <br><br><br>
  113. <div class="a">Gender:</div>
  114. <div class="b"><input type="radio" name="gender" value="male" value="<?php echo $row['gender']?>">Male
  115. <br><input type="radio" name="gender" value="female" value="<?php echo $row['gender']?>">Female</div>
  116. <br><br><br>
  117. <div class="a">How did you hear about us?</div>
  118. <div class="b">
  119. <select name="source" value="<?php echo $row['source']?>">
  120. <option>Select answer</option>
  121. <option>Through Internet</option>
  122. <option>Friends</option>
  123. </select></div>
  124.  
  125. <br><br><br>
  126. <div class="a">
  127. Please select this box if you wish<br>
  128. to be added to our mailing list<br>
  129. We will not pass on your details to any third party.
  130. </div>
  131. <div class="div1">
  132. <input type="checkbox" name="list" value="<?php echo $row['list']?>">
  133. </div>
  134. <br>
  135. <div class="div2">
  136. <input type="submit" value='<?php echo $btn?>'>
  137. </div>
  138. </form>
  139.  
  140. </body>
  141. </html>
  142.  
  143. exer2.css
  144. .a{
  145. display:block;
  146. float:left;
  147. width:350px;
  148. padding-left:5px;
  149. padding-bottom:3px;
  150. font-size:17px;
  151.  
  152. }
  153. .b{
  154. display:block;
  155. float:left;
  156. width:350px;
  157. padding-left:5px;
  158. padding-bottom:1px;
  159. font-size:16px;
  160. }
  161.  
  162. .h3{
  163. float:left;
  164. font-size:18px;
  165. }
  166.  
  167. .exer2{
  168. background-color:#FFFBD8;
  169. height:420px;
  170. width:725px;
  171. padding-left:7px;
  172. }
  173. .div1{
  174. display:block;
  175. float:left;
  176. width:350px;
  177. padding-left:5px;
  178. padding-top:20px;
  179.  
  180. }
  181. .div2{
  182. display:block;
  183. float:left;
  184. width:350px;
  185. padding-left:5px;
  186. padding-top:30px;
  187. font-size:18px;
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement