Jump to content

Bhekstar007

Members
  • Posts

    10
  • Joined

  • Last visited

Everything posted by Bhekstar007

  1. <?php //updateemployee.php session_start(); //Resume session if(isset($_SESSION['name'])){// If $_SESSION['name'] not set, force redirect to home page $name = $_SESSION['name']; $status_msg=""; if (isset($_GET['data'])){ $data = $_GET['data']; if(isset($_GET['update'])){ if(isset($_GET['name']) && isset($_GET['email'])&& isset($_GET['gender']) && isset($_GET['faculty'])){ if(!empty($_GET['name']) && !empty($_GET['email'])&& !empty($_GET['gender']) && !empty($_GET['faculty'])){ //$data = $_GET['data']; $new_name = $_GET['name']; $new_email = $_GET['email']; $new_gender = $_GET['gender']; $new_school = $_GET['faculty']; $conn= connectDB(); $status_msg=updateRecord($new_name,$new_school,$new_gender,$conn,$new_email,$data); echo $new_name; echo $new_school; echo $new_gender; echo $new_email; echo $data; }else{ $status_msg="<h2 style='color:red;'>Incomplete Input. Please try again</h2>"; } } } }}else{ header('Location: index.php'); } if(isset($_GET['logout'])){ session_destroy(); header('Location: index.php'); } ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> </head> <body> <?php if(isset($_GET['data'])){ $data = $_GET['data']; echo $data;//To test if the Id matches the profile that needs to be updated } $status_msg = ""; function connectDB(){//Function to connect to database $servername = "localhost"; $username = "root"; $password = ""; $db= "staff_db"; $conn = mysqli_connect($servername,$username,$password,$db); if(!$conn){ die('Connection Failed: '.mysqli_connect_error()); } return $conn; } //$sql = "SELECT * FROM staff_table WHERE staff_id='$data'"; $conn = connectDB(); function updateRecord($new_name,$new_gender,$new_school,$conn,$new_email,$data){ $sql = "UPDATE staff_table SET name='$new_name',gender='$new_gender',school='$new_school',email='$new_email' WHERE staff_id='$data'"; if (mysqli_query($conn,$sql)){ $status_msg="<h3 style= 'color:green;'>Account details are successfully updated.</h3>"; }else{ $status_msg= "ERROR: Could not execute SQL".mysqli_error($conn); } mysqli_close($conn); return $status_msg; } ?> <h1>Update Staff Profile</h1> <form action="UpdateEmployee.php" method="GET"> <fieldset> <legend>Personal Information</legend> <p><span class="error">* required field</span></p> <label for="name">Full Name: <input type="text" id="name" name="name"><span class="error">*</span></label><br> <label for="sID">Staff ID: <input type="text" id="sID" name="sID" placeholder="<?php echo $data;?>" disabled="disabled"></label><br> <label for="email">Email: <input type="text" id="email" name="email"><span class="error">*</span></label><br> <label for="gender">Gender:</label> <select id="gender" name="gender"> <option value = "-1" selected>[Gender]</option> <option value = "Male">Male</option> <option value = "Female">Female</option> </select><br> <label for="faculty">School/Faculty</label> <select id="faculty" name="faculty"> <option value = "-1" selected>[School/Faculty]</option> <option value = "SFS">SFS</option> <option value = "FBDA">FBDA</option> <option value = "FECS">FECS</option> </select><br> <p><input type="submit" name="update" value="Update Staff"></p> <p><?php echo $status_msg;?></p> </fieldset> </form> <footer> <p><a href="MainMenu.php">Main Menu</a></p> <p><a href="">Logout</a></p> </footer> </body> </html> <?php //displayemployeeinf.php session_start(); //Resume session if(isset($_SESSION['name'])){// If $_SESSION['name'] not set, force redirect to home page $name = $_SESSION['name']; }else{ header('Location: index.php'); } if(isset($_POST['logout'])){ session_destroy(); header('Location: index.php'); } ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Display Employee Information</title> </head> <body> <h1>Staff Profile</h1> <div class="display"> <fieldset> <legend>Employees Information</legend> <?php function connectDB(){//Function to connect to database $servername = "localhost"; $username = "root"; $password = ""; $db= "staff_db"; $conn = mysqli_connect($servername,$username,$password,$db); if(!$conn){ die('Connection Failed: '.mysqli_connect_error()); } return $conn; } $data = $_GET['data']; $sql = "SELECT * FROM staff_table WHERE name ='$data'"; $conn= connectDB(); $result = mysqli_query($conn,$sql); if($result){ $row = mysqli_fetch_assoc($result); } if(isset($_POST['update'])){ header('Location: UpdateEmployee.php?data='.$row['staff_id'].''); } ?> <form method="POST"> <table class="center"> <tr><td><p><strong>Name:</strong></p></td><td><p><?php echo $row['name'];?></p></td></tr> <tr><td><p><strong>Staff ID:</strong></p></td><td><p><?php echo $row['staff_id'];?></p></td></tr> <tr><td><p><strong>Email</strong></p></td><td><p><?php echo $row['email'];?></p></td></tr> <tr><td><p><strong>Gender</strong></p></td><td><p><?php echo $row['gender'];?></p></td></tr> <tr><td><p><strong>School</strong></p></td><td><p><?php echo $row['school'];?></p></td></tr> </table> <p><input type="submit" name="update" value="Update"></p> <p><input type="submit" name="delete" value="Delete"></p> </form> </fieldset> </div> <footer> <p><a href="MainMenu.php">Main Menu</a></p> <p><a href="">Logout</a></p> </footer> </body> </html>
  2. Hello yes I am required to store data in a text file. So from this I should try to use the get method instead of the post method. As I am retrieving data from the file and use the trim() function to display it in the proper format? Sorry if these are stupid questions I am new to php.
  3. Oh sorry my bad I think I may have worded my question wrong. The first code should be where the results are displayed and the second is the form I added it to help as a reference. Unfortunately I can't change the form to only search by faculty, the search has to be done by name. You said that the search is broken where in the code is it broken so I can try to fix it.
  4. The second page should display the specific name and correlating information related to that name. Example if I search "John Doe" the second page should display John Does information only such as his email address, gender etc. To which this information is received from a text file. The problem right now is that all entries in the text file are being displayed whether the name exists in the text file or not.
  5. Below is the contents of a text file: Name: John Doe| Email: johnd@gmail.com| Staff ID: SS1234| Gender: Male| School: FECS Name: Jane Doe| Email: doejane@gmail.com| Staff ID: SS3454| Gender: Female| School: SFS Name: Scott Doe| Email: does@gmail.com| Staff ID: SS9087| Gender: Male| School: FBDA Name: Kelly Doe| Email: kellydoe@gmail.com| Staff ID: SS2093| Gender: Female| School: SFS Below is the code for the display play <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> </head> <header> <nav> <ul> <li><a href="index.php">Main Page|</a></li> <li><a href="addEmployeeForm.php">Add Staff|</a></li> <li><a href="searchEmployeeForm.php">View Staff|</a></li> <li><a href="deleteEmployee.php">Delete Staff</a></li> </ul> </nav> </header> <body> <h1>Staff Profile</h1> <fieldset> <legend>Employees Information</legend> <?php $name=""; if(isset($_POST["name"])){ $name=$_POST["name"]; } $staffID=""; if(isset($_POST["sID"])){ $staffID=$_POST["sID"]; } $email=""; if(isset($_POST["email"])){ $email=$_POST["email"]; } $gender=""; if(isset($_POST["gender"])){ $gender=$_POST["gender"]; } $faculty=""; if(isset($_POST["faculty"])){ $faculty=$_POST["faculty"]; } //$searchthis = $_POST['name']; $file = "employees.txt"; $array= file($file); for($x=0; $x<count($array); $x++){ $record[]=explode("| ",$array[$x]); } for($x=0; $x<count($record); $x++){ $search = true; if($name==""){ $search=true; }elseif(strpos($record[$x][1],$name) !==false){ $search=true; }else{ $search=false; } if($email==""){ $search=true; }elseif(strpos($record[$x][2],$email) !==false){ $search=true; }else{ $search=false; } if($staffID==""){ $search=true; }elseif(strpos($record[$x][3],$staffID) !==false){ $search=true; }else{ $search=false; } if($gender==""){ $search=true; }elseif(strpos($record[$x][4],$gender) !==false){ $search=true; }else{ $search=false; } if($faculty==""){ $search=true; }elseif(strpos($record[$x][5],$faculty) !==false){ $search=true; }else{ $search=false; } if($search==true){ echo $record[$x][0],"<br>" .$record[$x][1],"<br>" .$record[$x][2],"<br>" .$record[$x][3],"<br>" .$record[$x][4],"<br>"; } } ?> </fieldset> </body> </html> Here is code for the form <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> </head> <header> <nav> <ul> <li><a href="index.php">Main Page|</a></li> <li><a href="addEmployeeForm.php">Add Staff|</a></li> <li><a href="searchEmployeeForm.php">View Staff|</a></li> <li><a href="deleteEmployee.php">Delete Staff</a></li> </ul> </nav> </header> <body> <h1>Staff Information</h1> <hr> <form action="displayEmployeeinfo.php" method="post"> <p>Staff Name:</p> <input type="text" id="name" name="name"> <p><input type="submit" name="confirm" value="Search"></p> </form> </body> </html> The basics are working but I want it to show a specific name that will display the info related to the searched name.
  6. Ok i have entered this code: $year = isset($_GET["leapyear"]) ? $_GET["leapyear"] : 0; To which it now returns the values I entered but now it showing all entered values as not being leap years. I used 2020 and 2021 as the test values.
  7. Basically I created a leapyear calculator, but to improve user experience i want it to display a message on the webpage confirming whether or not the year entered is a leapyear without changing the webpage. However I keep getting '1 is not leap year' regardless of the the year I enter. What am I doing wrong? <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title>Leap year form</title> </head> <body> <?php $year = isset($_GET["leapyear"]); function is_leapyear($year){ if(is_numeric($year)){ if($year%4 ==0) if($year%100 ==0) if($year%400 ==0){ return true; } return false; } } if (isset($_GET["confirm"])){ if (is_leapyear($year)){ echo'<span style="color:#008631;">'; echo"$year is a leap year</span>"; } else { echo'<span style="color:#FF0000;">'; echo "$year is not a leap year</span>"; } } ?> <h1>Leap Year</h1> <form action = "leapyear_selfcall.php" method = "get" > <label for="leapyear">Enter a year</label> <input type="text" name="leapyear"/> <p><input type="submit" name="confirm" value="Check For Leap Year"/></p> </form> </body> </html>
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.