I have Teams that I am assigning Employees to. The manager logs into his/her account which assigns the Team Number. They then click a link (which passes the Team ID as a variable) to go to the Assignment Page where they do an Employee Lookup; find the Employee and click on a link to assign this employee to their team.
When they first enter this Assignment Page, the Team Number exists. After they do the Search, however, the Team Number is blanked out. This is all on the same page. I don't know why the Search function wipes out the Session Variable value. Thank you.
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<?php
$_SESSION["teamid"] = $_REQUEST["tid"];
?>
<?php
$con = new mysqli($localhost, $username, $password, $dbname);
if( $con->connect_error){
die('Error: ' . $con->connect_error);
}
if( isset($_GET['search']) ){
$team = $_SESSION["teamid"];
$memberid = mysqli_real_escape_string($con, htmlspecialchars($_GET['search']));
$sql = "SELECT * FROM employees WHERE empid ='$memberid'";
}
$result = $con->query($sql);
?>
<label>Enter Employee You Wish To Add To Your Team (<?php echo $_SESSION["teamid"]; ?>)</label>
<form action="" method="GET">
<input type="text" placeholder="Enter Employee ID here" name="search">
<input type="submit" value="Search" name="btn" class="btn btn-sm btn-primary">
</form>
<br />
<table class="table table-striped table-responsive">
<tr>
<th>Employee ID</th>
<th>Name</th>
<th>Action</th>
</tr>
<?php
while($row = $result->fetch_assoc()){
?>
<tr>
<td><?php echo $row['empid']; ?></td>
<td><?php echo $row['firstname']; ?> <?php echo $row['lastname']; ?></td>
<td>Assign Employee</td>
</tr>
<?php
}
?>
</table>
</div>
</body>
</html>
It's because at the top of your code you have this part:
<?php
$_SESSION["teamid"] = $_REQUEST["tid"];
?>
So everytime the page reloads it sets the teamid to whatever is in the request. The second time you visit the page it's most likely empty which causes the session to be empty too.
An easy way to fix that is to check if $_REQUEST["tid"] is set:
<?php
if (isset($_REQUEST["tid"])) {
$_SESSION["teamid"] = $_REQUEST["tid"];
}
?>
Related
How do I add a few records to a database using the id’s I had listed in phpMyAdmin. The problem is once I entered the id , it display the database but when I entered another id , it took out and change the value of the first id database I entered and replace it with the second id. What I want is to add the database one by one through each click but instead of replacing it, I would like to add them below after the first ID I entered earlier. This is what I’m working on so far.
<html>
<head>
<title>Search data by its ID</title>
</head>
<body>
<center>
<h1>Search a single DATA</h1>
<h2>Retrieve data from database</h2>
<div class="container">
<form action="" method="POST">
<input type="text" name="id" placeholder="Student ID" />
<input type="submit" name="search" value="Search By ID" />
</form>
<table border="2" id="newton">
<tr>
<th>Product Name</th>
<th>Quantity</th>
<th>Returned Date</th>
</tr><br><br>
<?php
$connection = mysqli_connect("localhost","root", "");
$db = mysqli_select_db($connection,"myfirstdb");
if(isset($_POST['search']))
{
$id = $_POST['id'];
$query = "SELECT * FROM `table3` where id = '$id'";
$query_run = mysqli_query($connection, $query);
while($row = mysqli_fetch_array($query_run))
{
?>
<tr>
<td>
<?php echo $row ['product_name']; ?> </td>
<td>
<?php echo $row ['quantity']; ?> </td>
<td>
<?php echo $row ['returned_date']; ?> </td>
</tr>
<?php
}
}
?>
</table>
</form>
</div>
</center>
</body>
</html>
If I understand your problem correctly, the following code can solve your problem. Although the student ID has nothing to do with the name of the product !!
you can use a session for this problem.
<html>
<head>
<title>Search data by its ID</title>
</head>
<body>
<center>
<h1>Search a single DATA</h1>
<h2>Retrieve data from database</h2>
<div class="container">
<form action="" method="POST">
<input type="text" name="id" placeholder="Student ID" />
<input type="submit" name="search" value="Search By ID" />
</form>
<table border="2" id="newton">
<tr>
<th>Product Name</th>
<th>Quantity</th>
<th>Returned Date</th>
</tr><br><br>
<?php
$connection = mysqli_connect("localhost","root", "");
$db = mysqli_select_db($connection,"myfirstdb");
session_start();
if (!isset($_SESSION['id'])) {
$_SESSION['id'] = array();
}
if(isset($_POST['search']))
{
$id = $_POST['id'];
array_push($_SESSION['id'],$id);
$_SESSION['id'] = array_unique($_SESSION['id']);
$id = implode(',',$_SESSION['id']);
$query = "SELECT * FROM `table3` where id in ($id)";
$query_run = mysqli_query($connection, $query);
while($row = mysqli_fetch_array($query_run))
{
?>
<tr>
<td>
<?php echo $row ['product_name']; ?> </td>
<td>
<?php echo $row ['quantity']; ?> </td>
<td>
<?php echo $row ['returned_date']; ?> </td>
</tr>
<?php
}
}
?>
</table>
</form>
</div>
</center>
</body>
</html>
I'm working on a webpage where I allow users to edit their car information. In the mainlining, there is an edit button (input - type text with a hidden key value) where it takes the user to this "edit car info" page. Initially, once the page is opened for the first time, this hidden value is used to query the database, retrieve original information and and set them as placeholders for the field. The user can write information in the input field then press the "submit edit" button which then updates the row in the database table. However, I get an error that the name of the hidden value is undefined. I don't understand how it can be undefined for the update query when it was working just fine for the select query. Can anyone shed a light on this? What should I do? This is a picture of the errors:
This is the mainlanding code: (hidden value is set here)
<?php
$mysqli= new mysqli("localhost", "root","","Car_registration");
if(empty($_SESSION)) // if the session not yet started
session_start();
if(isset($_SESSION['username'])) { // if user already logged in
header("location: mainlanding_user.php"); //send to homepage
exit;
}
?>
<!DOCTYPE html>
<html>
<head>
<title> Car Registration: User's Mainlanding </title>
<link href="css/style3.css" rel="stylesheet">
</head>
<body>
<header>
<h1>Account Information</h1>
<img id="img1" src= "image/car.jpg" alt ="car image">
</header>
<nav id='nav'>
<form action="logout.php">
<input type="submit" value=" Logout " id="button">
</form>
</nav>
<h2>Profile </h2>
<div class='container1'>
<?php
$username="root";
$password="";
$database="Car_registration";
$mysqli= new mysqli("localhost",$username,$password,$database);
$query= "select * from driver where username='".$_SESSION['logged_username']."'";
$result = $mysqli->query($query);
while( $row = $result->fetch_assoc() ){
echo "<div id='container'>" ;
echo "<dl> <dt>First Name</dt> <dd>".$row['Fname'];
echo "</dd> <br> <dt>Last name</dt><dd>".$row['Lname'];
echo "</dd> <br> <dt>License Number</dt><dd>".$row['license_no'];
echo "</dd> <br> <dt>Age</dt><dd>".$row['Age'];
echo "</dd> <br> <dt>Birthday</dt><dd>".$row['bdate'];
echo "</dd> <br> <dt>City</dt><dd>".$row['City'];
echo "</dd></dl>";
echo "</div>";
$license_no = $row['license_no']; //used for finding cars
}
?>
<div class="align-me">
<div class="form-wrapper" action="search_plate_no.php">
<form class="center">
<input class="input-fields" name="search" type="text" placeholder="Search a plate number">
<input class="input-fields submit" name="find" type="submit" value="Search">
</form>
</div>
</div>
<h3> Registered Cars </h3>
<div class='container2'>
<?php
$username="root";
$password="";
$database="Car_registration";
$mysqli= new mysqli("localhost",$username,$password,$database);
$query= "select * from cars where license_no='".$license_no."'";
$result = $mysqli->query($query);
echo "<table border=1>
<tr>
<th>Plate No.</th>
<th>License No.</th>
<th>Car Type</th>
<th>Fines</th>
<th>City</th>
<th>Edit</th>
<th>Delete</th>
</tr>";
while ($temp = $result->fetch_assoc()){
?>
<tr>
<td><?php echo $temp['Plate_no']; ?></td>
<td><?php echo $temp['license_no']; ?></td>
<td><?php echo $temp['Car_type']; ?></td>
<td><?php echo $temp['Fines']; ?></td>
<td><?php echo $temp['city']; ?></td>
<td>
<form action = "edit_car.php" method="post">
<input type="hidden" name="id" value="<?php echo $temp['Plate_no']; ?>">
<input type="submit" name="edit" value="Edit">
</form>
</td>
<td>
<form action = "delete_car.php" method="post">
<input type="hidden" name="id" value="<?php echo $temp['Plate_no']; ?>">
<input type="submit" name="delete" value="Delete">
</form>
</td>
</tr>
<?php
}
?>
</table>
</div>
<form action="register_car.php">
<input type="submit" value=" Register Car " id="button2">
</form>
<footer>
<h4> All rights belong to Car Registration Inc. </h4>
<img id="img3" src= "image/license.png" alt ="license plates image">
</footer>
</body>
</html>
Edit car page: (Error is generated here)
<!DOCTYPE html>
<html>
<head>
<title> Edit Car Information Page </title>
<link href="css/style2.css" rel="stylesheet">
</head>
<body>
<div class="container">
<header>
<h1>Edit Car Information </h1>
<img id="img1" src= "image/register.png" alt ="Registration image">
</header>
<?php
$username="root";
$password="";
$database="Car_registration";
$mysqli= new mysqli("localhost",$username,$password,$database);
$plate_no= $_POST["id"]; //This line causes an error
$_SESSION['plateNo'] = $plate_no;
$query= "select * from cars where Plate_no='".$plate_no."'";
$result = $mysqli->query($query);
while( $row = $result->fetch_assoc()){
$plate_no = $row['Plate_no'];
$car_type = $row['Car_type'];
}
?>
<main>
<h2> You can only edit the following information: </h2>
<form action="" method="post">
<label for="car_type_input">Car Type:</label>
<input type="text" placeholder="<?php echo $car_type?>" id="car_type_input" name="car_type_input"><br><br>
<div class="vertical-center">
<input type="submit" value=" Submit Edit " name="button1" id="button1">
</div>
</form>
<?php
$username="root";
$password="";
$database="Car_registration";
$mysqli= new mysqli("localhost",$username,$password,$database);
if( isset($_POST['button1']) ){ //If user changed field, take value. If not, keep old value.
if( !empty($_POST['car_type_input']) ){ //If there is user input
$car_type_2 = $_POST['car_type_input'];
$query= "update cars set Car_type='".$car_type_2."' WHERE Plate_no='".$_SESSION['plateNo']."'";
}
if ($mysqli->query($query))
echo "Fields updated successfuly!";
else
echo "Update Fields Failed!";
}
?>
</main>
<footer>
<h3> All rights belong to Car Registration Inc. </h3>
<img id="img3" src= "image/license.png" alt ="license plates image">
</footer>
</div>
</body>
</html>
Use $plate_no= $_POST['id']; instead of $plate_no= $_POST["id"];
Here why you close the while loop ??
while ($temp = $result->fetch_assoc()){
?>
and here too
<?php
}
Try this:
print"<h3> Registered Cars </h3>
<div class='container2'>";
$username="root";
$password="";
$database="Car_registration";
$mysqli= new mysqli("localhost",$username,$password,$database);
$query= "select * from cars where license_no='".$license_no."'";
$result = $mysqli->query($query);
echo "<table border=1>
<tr>
<th>Plate No.</th>
<th>License No.</th>
<th>Car Type</th>
<th>Fines</th>
<th>City</th>
<th>Edit</th>
<th>Delete</th>
</tr>";
while ($temp = $result->fetch_assoc())
{
print"
<tr>
<td><?php echo $temp['Plate_no']; ?></td>
<td><?php echo $temp['license_no']; ?></td>
<td><?php echo $temp['Car_type']; ?></td>
<td><?php echo $temp['Fines']; ?></td>
<td><?php echo $temp['city']; ?></td>
<td>
<form action = "edit_car.php" method="post">
<input type="hidden" name="id" value="<?php echo $temp['Plate_no']; ?>">
<input type="submit" name="edit" value="Edit">
</form>
</td>
<td>
<form action = "delete_car.php" method="post">
<input type="hidden" name="id" value="<?php echo $temp['Plate_no']; ?>">
<input type="submit" name="delete" value="Delete">
</form>
</td>
</tr> ";
}
print"</table>
</div>";
you are not sending id that's because error appears use this code to check if id exists first:
$plate_no='';
$car_type = '';
if(isset($_POST["id"])){
$plate_no= $_POST["id"]; //This line causes an error
$_SESSION['plateNo'] = $plate_no;
$query= "select * from cars where Plate_no='".$plate_no."'";
$result = $mysqli->query($query);
while( $row = $result->fetch_assoc()){
$plate_no = $row['Plate_no'];
$car_type = $row['Car_type'];
}
}
I have 3 tables in my database (standards, courses, students). I want to display Fname and gender from students table enrolled in selected 'standard' and 'course' from dropdown - but the "submit" button doesn't seem to work. The code is as shown below. I am sure it is connected to the database as the dropdowns for 'courses' and 'standards' work:
<html>
<head>
<title>Courses</title>
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
<div id="container">
<div id="wrapper">
<h1> Students</h1>
<div id="data">
<form action="index.php" method="POST">
<select name="standards">
<option>Standard</option>
<?php
include 'includes/dbconnect.php';
$query1 = "SELECT * FROM standards";
$result1 = mysql_query($query1);
while($rows1 = mysql_fetch_array($result1)){
$standardID = $rows1['id'];
$rowsData1 = $rows1['standardName'];
?>
<option value="<?php echo $standardID; ?>">
<?php
echo $rowsData1; ?></option>
<?php
}
?>
</select>
</div>
<div id="data2">
<select name="courses">
<option>Courses</option>
<?php
$query2 = "SELECT * FROM courses";
$result2 = mysql_query($query2);
while($rows2 = mysql_fetch_array($result2)){
$coursesID = $rows2['id'];
$rowsData2 = $rows2['courseName'];
?>
<option value="<?php echo $courseID;?>">
<?php echo $rowsData2; ?></option> <?php }?>
</select>
</div>
<div id="submit">
<input type="submit" name="submit" id="submit" value="submit"/>
<table border="1" id="table1">
<tr>
<th>Student Name</th>
<th>Gender</th>
</tr>
<?php
if(isset($_POST['submit'])){
$standardName = $_POST['standards'];
$courseName = $_POST['courses'];
$query3 = "SELECT students.Fname, students.gender
FROM students
WHERE students.standardID = '$standardName'
AND students.courseID = '$courseName'";
$result3 = mysql_query($query3);
while($rows3 = mysql_fetch_array($result3)){
//$dataID = $rows3['id'];
$studentName = $rows3['FName'];
$gender = $rows3['gender'];
?>
<tr>
<td><?php echo $studentName; ?></td>
<td><?php echo $gender; ?></td>
<tr>
<?php
}
}
?>
</table>
</div>
</div>
</body>
</html>
Inscribe the input elements within a form tag with post as method. Input elements with type submit are used for submission of forms.
You only do anything with the submitted form data if that data includes a submit field.
if(isset($_POST['submit'])){
However you have:
<input type="submit" name="submit" id="submit"/>
Your submit button don't have a value, so nothing will be included in the form data for it.
Hi iam trying to fetch the record from database and need to update the query in database but if iam trying getting errors as below
Notice: Undefined variable: result in C:\xampp\htdocs\index\index.php on line 57
Warning: mysql_num_rows() expects parameter 1 to be resource, null given in C:\xampp\htdocs\index\index.php on line 57
here is my code:
index.php
<?php
session_start();
?>
<title> Dashboard </title>
</head>
<?php
$username = $_SESSION['username'];
if($username)
{
?>
<h3> Welcome <?php echo $username; ?></h3>
<?php
}
else
{
echo "no";
}
?>
<body>
<form method="post" action="personalinfo.php" id="myform">
<input type='hidden' value="<?php echo $username; ?>" name='email'>
<div class="box-body table-responsive no-padding">
<table class="table table-hover">
<tr>
<th>ID</th>
<th>Name</th>
</tr>
<tr>
<?php
if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_array($result)) {
?>
<td><?php echo $row['first_name']; ?></td>
</tr>
<?php
}
}
?>
</table>
</div>
<input type="button" value="Logout" id="logout" onClick="document.location.href='login.php'" />
</form>
</body>
Personalinfo.php
<?php
$connection = mysql_connect("localhost", "root", "") or die(mysql_error());
$db = mysql_select_db("accountant", $connection);
$email=$_POST['email'];
$firstname = $_POST['first_name'];
$res = "SELECT * FROM registered WHERE email ='$email' ";
$result=mysql_query($res);//this is for comparing ids
$res1 = "SELECT first_name FROM registered WHERE email ='$email' ";
$result=mysql_query($res1);//this is for getting first_name from database table
$query = mysql_query("UPDATE registered SET first_name='$firstname' WHERE email= '$email'");
// mysql_query("$query") OR die("Error:".mysql_error());
if($query)
{
echo "Successfully Registered";
}
else{
echo "Registration has not been completed.Please try again";
}
Can anyone help me regarding this.Thanks in advance
Maybe you should include Personalinfo.php into index.php so you have access to the $result variable, because it is undefined when you called mysql_num_rows
You cannot access variables from other files without including them, or parsing them.
Also you should use mysqli, pdo, etc. Because it's more secure with a example being prepared statements
<?php
session_start();
?>
<title> Dashboard </title>
</head>
<?php
$username = $_SESSION['username'];
if($username)
{
?>
<h3> Welcome <?php echo $username; ?></h3>
<?php
}
else
{
echo "no";
}
?>
<body>
<input type='hidden' value="<?php echo $username; ?>" name='email'>
<div class="box-body table-responsive no-padding">
<table class="table table-hover">
<tr>
<th>Name</th>
</tr>
<tr>
<?php include "personalinfo.php";?>
<td><?php echo $row['first_name']; ?></td>
</tr>
</table>
</div>
<input type="button" value="Logout" id="logout" onClick="document.location.href='login.php'" />
</form>
</body>
I am a new PHP coder and I started a project of cms panel with, for start, three options: new article, delete article and edit article. There isn't even one error in my project but, when im trying to edit a post, everything is going well, except the save. The post isn't saved!!! and there aren't any errors! I am trying to fix that problem from yesterday. That's annoying.
Here is part of my code:
editTreat.php:
<!DOCTYPE html>
<?php
include("includes/functions.php");
$ctreat = getTreat($_GET["id"]);
?>
<html lang="en">
<head>
<script type="text/javascript" src="ckeditor/ckeditor.js"></script>
<meta charset="utf-8" />
<title>Control panel</title>
</head>
<body>
<form action="doEditt.php" method="post">
<table>
<tr>
<td>
<label for="tName">:Title</label>
</td>
<td>
<input type="text" name="tName" value="<?php echo $ctreat["Title"]; ?>"></input>
</td>
</tr>
<tr>
<td>
<label for="tContent">:Content</label>
</td>
<td>
<textarea name="tContent"> <?php echo $ctreat["Content"]; ?> </textarea>
<script type="text/javascript">
CKEDITOR.replace('tContent');
</script>
</td>
</tr>
<tr>
<td colspan="2"><input type="submit" name="submit" value="Edit"></input></td>
<td> <input type="hidden" name="id" value="<?php echo $_GET["ID"]; ?>" /></td>
</tr>
</table>
</form>
</body>
</html>
doEditt.php:
<?php
include("includes/functions.php");
if(isset($_POST["submit"])) {
if(isset($_POST["tName"])) {
editTreat($_POST["tName"],$_POST["tContent"],$_POST["id"]);
header("Location: treatments.php");
} else {
echo "Please fill the title";
include("editTreat.php");
}
} else {
header("Location: editTreat.php");
}
?>
part of my includes/functions.php:
function editTreat($tName, $tContent, $id) {
$id = (int) $id;
$query = mysql_query("UPDATE `treatments` SET title = '$tName', content = '$tContent' WHERE ID = '$id'") or die(mysql_error());
header("Location: treatments.php");
}
function getTreat($id) {
$id = (int) $id;
$query = mysql_query("SELECT * FROM `treatments` WHERE ID = '$id'") or die(mysql_error());
return mysql_fetch_assoc($query);
}
So, if you can help me, please help me. Thanks!!
On line 4 you reference id lower case
$ctreat = getTreat($_GET["id"]);
Notice Lowercase id
Then in the form you reference ID Uppercase
<?php echo $_GET["ID"]; ?>
PHP is case sensitive... is id upper or lower case
The posting ID is not getting sent to the submit form, this looks to be your issue.
Some obvious issues:
I'm sure id column is not string so why quotes?
UPDATE `treatments` SET title = '$tName', content = '$tContent' WHERE ID = $id
You should escape any user input.
$tName = mysql_real_escape_string($tName);
PHP is case sensitive, and you use diferent case for $_GET["id"]. Im sure you want to use $_GET["id"]!
$ctreat = getTreat($_GET["id"]);
VERSUS
echo $_GET["ID"];