Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- exer4.php
- <?php
- $action=$_GET['action'];
- $id=$_GET['id'];
- $db=mysqli_connect('localhost', 'root', '', 'carillo_exer4');
- if($action=='delete'){
- $sql="DELETE FROM user WHERE id=".$id;
- }else{
- $username=$_POST['username'];
- $password=$_POST['password'];
- $gender=$_POST['gender'];
- $source=$_POST['source'];
- $list=isset($_POST['list']);
- if($action =='edit'){
- $sql="UPDATE user SET
- username='$username',
- password='$password',
- gender='$gender',
- source='$source',
- list='$list'
- WHERE id=".$id;
- }
- else{
- $sql="INSERT INTO user(username, password, gender, source, list)
- VALUES('$username','$password','$gender','$source','$list')";
- }
- }
- mysqli_query($db, $sql);
- header('Location:exer4s.php');
- ?>
- exer4s.php
- <html>
- <head><title>CS321 Exercise 4</title></head>
- <body>
- <?php
- $labels=array('Id', 'Username', 'Password', 'Gender', 'Source', 'List', 'Action');
- ?>
- <table border="1">
- <?php
- foreach($labels as $label){?>
- <th>
- <?php echo $label?>
- </th>
- <?php } ?>
- <?php
- $ctr=1;
- $db=mysqli_connect('localhost', 'root', '', 'carillo_exer4');
- $sql="SELECT * FROM user";
- $result=mysqli_query($db, $sql);
- while($row=mysqli_fetch_array($result)){?>
- <tr>
- <td><?php echo $ctr++ ?></td>
- <td><?php echo $row['username']?></td>
- <td><?php echo $row['password']?></td>
- <td><?php echo $row['gender']?></td>
- <td><?php echo $row['source']?></td>
- <td><?php echo $row['list']?></td>
- <td><a href="exer4_view.php?action=edit&id=<?php echo $row['id']?>">Update</a>|
- <a href="exer4.php?action=delete&id=<?php echo $row['id']?>">Delete</a></td>
- </tr>
- <?php } ?>
- </table>
- <a href="exer4_view.php?action=add&id=new">Add</a>
- </body>
- </html>
- exer4_view.php
- <?php
- if(isset($_GET['action'])){
- $action=$_GET['action'];
- $id=$_GET['id'];
- if($action=='add'){
- $btn='Register Now';
- $row=null;
- }
- else{
- $db=mysqli_connect('localhost', 'root', '', 'carillo_exer4');
- $sql="SELECT * FROM user WHERE id=".$id;
- $result=mysqli_query($db, $sql);
- $row=mysqli_fetch_array($result);
- $btn="Update";
- }
- }
- else{
- $btn='Register Now';
- $row=null;
- }
- ?>
- <html>
- <head><title>CS321 Exercise 4</title></head>
- <link rel="stylesheet" href="exer2.css"></link>
- <body>
- <form class="exer2" action="exer4.php?action=<?php echo $action?>&id=<?php echo $id ?>" method="POST">
- <h1>User Registration</h1>
- <div class="h3">Please complete the following form to register with our site:</div>
- <br><br>
- <div class="a">User Name:</div>
- <div class="a"><input type="text" name="username" value="<?php echo $row['username']?>"></div>
- <br>
- <div class="a">Password:</div>
- <div class="b"><input type="password" name="password" value="<?php echo $row['password']?>"></div>
- <br><br><br>
- <div class="a">Gender:</div>
- <div class="b"><input type="radio" name="gender" value="male" value="<?php echo $row['gender']?>">Male
- <br><input type="radio" name="gender" value="female" value="<?php echo $row['gender']?>">Female</div>
- <br><br><br>
- <div class="a">How did you hear about us?</div>
- <div class="b">
- <select name="source" value="<?php echo $row['source']?>">
- <option>Select answer</option>
- <option>Through Internet</option>
- <option>Friends</option>
- </select></div>
- <br><br><br>
- <div class="a">
- Please select this box if you wish<br>
- to be added to our mailing list<br>
- We will not pass on your details to any third party.
- </div>
- <div class="div1">
- <input type="checkbox" name="list" value="<?php echo $row['list']?>">
- </div>
- <br>
- <div class="div2">
- <input type="submit" value='<?php echo $btn?>'>
- </div>
- </form>
- </body>
- </html>
- exer2.css
- .a{
- display:block;
- float:left;
- width:350px;
- padding-left:5px;
- padding-bottom:3px;
- font-size:17px;
- }
- .b{
- display:block;
- float:left;
- width:350px;
- padding-left:5px;
- padding-bottom:1px;
- font-size:16px;
- }
- .h3{
- float:left;
- font-size:18px;
- }
- .exer2{
- background-color:#FFFBD8;
- height:420px;
- width:725px;
- padding-left:7px;
- }
- .div1{
- display:block;
- float:left;
- width:350px;
- padding-left:5px;
- padding-top:20px;
- }
- .div2{
- display:block;
- float:left;
- width:350px;
- padding-left:5px;
- padding-top:30px;
- font-size:18px;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement