I'm having trouble setting up some css code for a table I'm generating using php from a database. I want every even row to have a different background-color, so I tried using nth-child(even) for this, but it doesn't seem to work for some reason. Here's my code:
style.css:
#usertable {
border-collapse: collapse;
font-family: "Trebuchet MS", Arial;
}
#usertable td, #usertable th {
border: 1px solid black;
background-color: rgb(228,227,227);
padding: 8px;
}
#usertable tr:nth-child(odd){background-color: rgb(115,115,115);}
admin.php:
<table id="usertable">
<tr>
<th> Id: </th>
<th> Username: </th>
<th> Rights: </th>
</tr>
<?php
$userquery = "SELECT id, username, strength FROM users";
$result = $conn->query($userquery) or die($conn->error);
while ($row = $result->fetch_assoc()) { ?>
<tr>
<td> <?php echo $row['id']; ?> </td>
<td> <?php echo $row['username']; ?> </td>
<td>
<form method="post" action="">
<input type="number" min="0" max="255" name="newrights" value=" <?php echo $row['strength']; ?> ">
<input type="text" name="idnumber" hidden="true" value=" <?php echo $row['id']; ?> ">
<input type="text" name="usertochange" hidden="true" value=" <?php echo $row['username']; ?> ">
<input type="submit" value="Update">
</form>
</td>
</tr>
<?php
}
?>
</table>
There is some markup error with your code
<table id="usertable">
<tr> <!--Add this-->
<th> Id: </th>
<th> Username: </th>
<th> Rights: </th>
</tr> <!--Add this-->
<?php
$userquery = "SELECT id, username, strength FROM users";
$result = $conn->query($userquery) or die($conn->error);
while ($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row['id'] . "</td><td>" . $row['username'] . "</td><td>";
echo "<form method=\"post\" action=\"\">
<input type=\"number\" min=\"0\" max=\"255\" name=\"newrights\" value=\"" . $row['strength'] . "\">
<input type=\"text\" name=\"idnumber\" hidden=\"true\" value=\"" . $row['id'] . "\">
<input type=\"text\" name=\"usertochange\" hidden=\"true\" value=\"" . $row['username'] . "\">
<input type=\"submit\" value=\"Update\">
</form></td></tr>";
}
?>
Also looks like you have used many slaces so i suggest you to check table markup on browser
Updated: Add
#usertable td:nth-child(odd) { background-color: #efefef;}
Do not use tr as you have given background color to td and th initially
Why don't you sue some CSS class in each alternate table row?
Ex:
HTML
<tr class="bg-red">
<td>Jill</td>
<td>Smith</td>
<td>50</td>
</tr>
<tr class="bg-blue">
<td>Eve</td>
<td>Jackson</td>
<td>94</td>
</tr>
CSS
<style type="text/css">
.bg-blue{
background-color: blue;
}
.bg-red{
background-color: red;
}
</style>
I think this will simplify your code & make it easy to read.
Example for Dynamic Data
<?php
$users[] = array('fname'=>'Jill', 'lname'=>'Smith', 'age'=>50);
$users[] = array('fname'=>'Eve', 'lname'=>'Jackson', 'age'=>94);
$rowColClass = 'bg-blue';
foreach ($users as $key => $value)
{
echo "<tr class='$rowColClass'>";
echo "<td>$value[fname]</td>";
echo "<td>$value[lname]</td>";
echo "<td>$value[age]</td>";
echo "</tr>";
if($rowColClass == 'bg-blue')
$rowColClass = 'bg-red';
else
$rowColClass = 'bg-blue';
}
?>
Related
Here I have HTML, PHP and Jquery code with where I can edit all my record using the modal, the problem is that it keeps disappearing after I click on my update button, the modal only shows for about half a second. Can you help me? I'm actually new and just learning Jquery
this is the front view
<?php
include 'php/header.php';
require_once 'php/connect.php';
?>
<script>
$(document).ready(function(){
$("#input").on("keyup", function() {
var value = $(this).val().toLowerCase();
$("#body tr").filter(function() {
$(this).toggle($(this).text().toLowerCase().indexOf(value) > -1)
});
});
});
</script>
<style>
table, th, td {
border: 1px solid black;
color: black;
width: auto;
margin: auto;
}
td {
text-align: center;
}
.edit {
background-color: blue;
font-family: algerian;
color: lime;
border-radius: 30px;
}
.edit:hover {
cursor: pointer;
background-color: yellow;
color: black;
}
.delete {
color: white;
background: black;
font-family: algerian;
}
.delete:hover {
color: black;
background: yellow;
}
</style>
<h1 style=" font-family: algerian; color: lime" align="center">Assets</h1>
<div class="filter">
<input style="height: 23px; float: right;" id="input" type="text" placeholder="Search.." autocomplete="off"></div>
<br>
<br>
<table align="center">
<tr>
<a onclick="" href=""></a>
<thead>
<th>Asset Num</th>
<th>FA num</th>
<th>Employee</th>
<th>Team</th>
<th>Contractor</th>
<th>Status</th>
<th>Category</th>
<th>Disposed</th>
<th colspan="2">Actions</th>
</thead>
<tbody id="body">
<?php
$sql = " SELECT * FROM assets
LEFT JOIN team
ON assets.team_id = team.team_id
LEFT JOIN contractor
ON assets.contractor_id = contractor.contractor_id
LEFT JOIN employee
ON assets.employee_id = employee.employee_id
LEFT JOIN item_status
ON assets.status_id = item_status.status_id
LEFT JOIN category
ON assets.category_id = category.category_id
ORDER BY category ";
$result = mysqli_query($conn, $sql);
if ($result->num_rows> 0) {
while($row = $result->fetch_assoc())
{
if (empty($row["disposal_status_id"])) {
$row["disposal_status_id"] = "NO"; }
else {
$row["disposal_status_id"] = "YES";
}
echo "<tr>";
echo "<td>". $row["asset_num"] ."</td>" ;
echo "<td>". $row["fa_num"]."</td>";
echo "<td>". $row["first_name"]. " " .$row["last_name"]. "</td>";
echo "<td>". $row["team"]."</td>";
echo "<td>". $row["F_name"]."</td>";
echo "<td>". $row["item_status"]."</td>";
echo "<td>". $row["category"]."</td>";
echo "<td>". $row["disposal_status_id"] ."</td>";
echo "<td><form method='POST' id='form'><button name='edit' class='edit' value=". $row['assets_id'] . " >Update</button></form>";
echo "<td><a class='delete' onclick=\"return confirm('Are you sure you want to delete this record?')\"
href='php/delete_asset.php?id=".$row['assets_id']."'>Delete</a>";
echo "</tr>";
}
}
else {
echo "<p style='font-family: algerian; font-size: 30px; margin-left: 45%; color: yellow ' >No Record</p>";
}
?>
</tbody>
</table>
and this is the modal view
<script>
$(document).ready(function() {
$(".edit").click(function(){
$("#modal").css('display', 'block');
});
});
</script>
<script>
$(document).ready(function() {
$("#cancel").click(function(){
$("#modal").css("display", "none");
});
});
</script>
<script>
$(document).ready(function(){
$("#form").submit(function() {
var edit = $(this).val();
$.ajax({
type: "POST",
data: {edit:edit},
});
});
});
</script>
<?php
include 'php/connect.php';
$id = $_POST['edit'];
$d = mysqli_query($conn, " SELECT * FROM assets
LEFT JOIN employee
ON assets.employee_id = employee.employee_id
LEFT JOIN team
ON assets.team_id = team.team_id
LEFT JOIN contractor
ON assets.contractor_id = contractor.contractor_id
LEFT JOIN item_status
ON assets.status_id = item_status.status_id
LEFT JOIN category
ON assets.category_id = category.category_id
LEFT JOIN disposal_status
ON assets.disposal_status_id = disposal_status.disposal_status_id
WHERE assets_id = '$id'");
$check = mysqli_fetch_array($d);
function load_employee()
{
include 'php/connect.php';
$output = '';
$sql = "SELECT * FROM employee ORDER BY first_name";
$result = mysqli_query($conn, $sql);
while ($row = mysqli_fetch_array($result)) {
$output .= "<option value='" . $row['employee_id'] ."'>"
. $row['first_name'] . " "
. $row['last_name'] . " </option>";
}
return $output;
}
?>
<div id="modal">
<script>
$(document).ready(function(){
$('#employee').change(function(){
var employee_id = $(this).val();
$.ajax({
url:"php/get_employee_team.php",
method: "POST",
data: {employee_id:employee_id},
dataType:"text",
success:function(data)
{
$('#team').html(data);
}
});
});
});
</script>
<link rel="stylesheet" type="text/css" href="../mystyle.css">
<style>
#modal {
position: fixed;
display: none;
width: 100%;
height: 100%;
top: 0%;
left: 0;
right: 0;
bottom: 0;
background-color: rgba(0,0,0,0.5);
z-index: 2;
cursor: pointer;
background-color: rgba(0,0,0,0.8); /* Black w/ opacity */
}
</style>
<form method="POST" action="php/update_assets.php" >
<table align="center">
<th colspan="4" >Update Asset # <?php echo $check['assets_id']; ?></th>
<tr></tr>
<input style="display: none" type="text" name="assets_id" value="<?php echo $check['assets_id'] ?>">
<td>Asset Num:</td>
<td><input type="text" name="asset_num" value="<?php echo $check['asset_num'] ?>" autocomplete="off" required></td>
<td>Unit Condition:</td>
<td><input type="text" name="condition" value="<?php echo $check['condition'] ?>" autocomplete="off" ></td>
<tr></tr>
<td>FA Num:</td>
<td><input type="text" name="fa_num" value="<?php echo $check['fa_num'] ?>" autocomplete="off" required></td>
<td>Audit Date:</td>
<td><input type="date" name="audit" value="<?php echo $check['audit_date'] ?>" autocomplete="off"></td>
<tr></tr>
<tr>
<td>Employee:</td>
<td>
<select id="employee" name="employee" >
<?php
echo "<option value='" . $check['employee_id'] ."'>"
. $check['first_name'] . " "
. $check['last_name'] . " </option>";
?>
<option><?php echo load_employee(); ?></option>
</select>
</select></td>
<td>Location:</td>
<td><input type="text" name="location" value="<?php echo $check['location'] ?>" autocomplete="off"></td>
</tr>
<tr>
<td>Team:</td>
<td>
<select style="color: black" id="team" name="team" readonly>
<option value= "<?php echo $check['team_id'] ?>" ><?php echo $check['team'] ?></option>
</select>
</td>
<td>Insurance Date:</td>
<td><input type="date" name="insurance" value="<?php echo $check['audit_date'] ?>"></td>
</tr>
<td>Contractor:</td>
<td><select name="contractor">
<?php
echo "<option value='" . $check['contractor_id'] ."'>"
. $check['F_name'] . " "
. $check['L_name'] . " </option>";
?>
<option>
<?php
$sql = "SELECT * FROM contractor" ;
$result = mysqli_query($conn, $sql);
while ($row =mysqli_fetch_assoc($result)) {
echo "<option value='" . $row['contractor_id'] ."'>"
. $row['F_name'] . " "
. $row['L_name'] . " </option>";
}
?>
</option>
</select> </td>
<td>Comments:</td>
<td><input style="overflow: scroll;" type="text" name="comments" value="<?php echo $check['comments'] ?>" autocomplete="off"></td>
<tr></tr>
<td>Status:</td>
<td><select name="status">
<?php
echo "<option value='" . $check['status_id'] ."'>"
. $check['item_status'] . " </option>";
?>
<option>
<?php
$sql = "SELECT * FROM item_status " ;
$result = mysqli_query($conn, $sql);
while ($row =mysqli_fetch_assoc($result)) {
echo "<option value='" . $row['status_id'] ."'>"
. $row['item_status'] . " </option>";
}
?>
</option>
</select>
</td>
<td colspan="2"> <br></td>
<tr></tr>
<td>Asset Category:</td>
<td>
<select name="category">
<?php
echo "<option value='" . $check['category_id'] ."'>"
. $check['category'] . " </option>";
?>
<option>
<?php
$sql = "SELECT * FROM category" ;
$result = mysqli_query($conn, $sql);
while ($row =mysqli_fetch_assoc($result)) {
echo "<option value='" . $row['category_id'] ."'>"
. $row['category'] . " </option>";
}
?>
</option>
</select>
</td>
<td colspan="2"> <br></td>
<tr></tr>
<td>Description:</td>
<td><input type="text" name="description" value="<?php echo $check['description'] ?>" autocomplete="off">
<td colspan="2" style="text-align: center; color: blue; font-family: algerian" >DISPOSAL DETAILS</td>
<tr></tr>
<td>Serial Number:</td>
<td><input type="text" name="serial" value="<?php echo $check['serial_num'] ?>" autocomplete="off"></td>
<td>Disposal Location</td>
<td><input type="text" name="disposal_loc" value="<?php echo $check['disposal_location'] ?>" autocomplete="off"></td>
<tr></tr>
<td>Date Aquired:</td>
<td><input type="date" name="acquired" value="<?php echo $check['date_get'] ?>" autocomplete="off"></td>
<td>Date Disposed</td>
<td><input type="date" name="disposal_date" value="<?php echo $check['disposal_date'] ?>" autocomplete="off"></td>
<tr></tr>
<td>Purchase Price:</td>
<td><input type="text" name="price" value="<?php echo $check['purchase_price'] ?>" autocomplete="off"></td>
<td>Disposed Status</td>
<td>
<select id="dept" name="disposal_status">
<?php
if ($check['disposal_status_id'] == 0) {
echo "<option value='". $checl['disposal_status_id'] ."' >";
echo "NOT YET";
echo "</option>";
}
?>
<?php
$sql = "SELECT * FROM disposal_status " ;
$result = mysqli_query($conn, $sql);
while ($row =mysqli_fetch_assoc($result)) {
echo "<option value='" . $row['disposal_status_id'] ."'>"
. $row['status'] . " </option>";
}
?>
</select>
</td>
<tr></tr>
<br>
<td style="text-align: center;" colspan="4"><button type="submit" name="btn" class="submit" >Submit</button>
<p class="submit" id="cancel">Cancel</p></form>
</td>
</table>
</div>
I try to send the data in the same page and try to change the CSS code when I click on the Update button, sending the id and fetching the data required using ajax Jquery, Hope someone can help me, thanks in advance
I think you need to use preventDefault() function because the nature of submiting form is to refresh the page that's also the reason why the modal is appearing just a couple of seconds only or sometimes it is not appearing.
Please try these line of code.
$("#form").on("submit",function(event) {
event.preventDefault();
var edit = $(this).val();
$.ajax({
type: "POST",
data: {edit:edit},
});
});
});
I want to display the data loaded from database after it is inserted using the function add_data() but it's not displaying the data on the table.
if(isset($_POST['btn_add_details'])) {
add_data();
include("db_PSIS.php");
$sql2="SELECT * FROM sample_barcode WHERE IDRec='".$row['IDRec']."'";
$result2=mysql_query($sql2);
if(!$result2) {
echo "<h1>Could not process query this time. Please try again later!</h1>";
}
else {
while($row2=mysql_fetch_array($result2)) {
echo "<form name='form2' method='POST'>";
echo "<table class='output' border=2 align=center>";
echo "<tr class='thcolor'>";
echo "<th>Parent</th>";
echo "<th>LOT Traveller No.</th>";
echo "<th>Datecode</th>";
echo "</tr>";
echo "<tr>";
echo "<td>".$row2['LotTraveller']."</td>";
echo "<td>".$row2['LotTraveller']."</td>";
echo "<td>".$row2['Datecode']."</td>";
echo "</tr>";
echo "</table>";
echo "</form>";
}
echo "<br><h1>Data successfully loaded!</h1>";
}
mysql_close($link);
}
?>
Here's the function for adding data on the database:
function add_data() {
include("db_PSIS.php");
$sql="INSERT INTO sample_barcode (LotTraveller, ShipmentLotNumber) VALUES ('".$_POST['traveller']."', '".$_POST['datecode']."')";
$result=mysql_query($sql);
$row=mysql_fetch_array($result);
mysql_close($link);
}
Here's the code for database connection:
<?php
$username="****";
$password="****";
$database="****";
$hostname="****";
$link=#mysql_connect($hostname,$username,$password)
or die ("...cannot connect database, using $username for $hostname");
mysql_query("set names 'utf8'"); //指定数据库字符集
mysql_select_db($database,$link);
?>
Here's the form input:
<table width="500" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<tr>
<th align="right">Parent Traveller: </th>
<td><input type="text" name="traveller" size="20" value="<?php if(empty($_POST['traveller'])) {echo ''; } else { echo $row2['LotTraveller']; } ?>"/></td>
</tr>
<tr>
<th align="right">Date Code: </th>
<td><input type="text" name="datecode" size="20" value="<?php if(empty($_POST['traveller'])) {echo ''; } else { echo $row2['ShipmentLotNumber']; } ?>" /></td>
</tr>
<tr>
<th align="right">Traveller 1: </th>
<td><input type="text" name="traveller1" size="20" /></td>
</tr>
<tr>
<th align="right">Date Code: </th>
<td><input type="text" name="datecode1" size="20" /></td>
</tr>
<tr>
<th align="right">Traveller 2: </th>
<td><input type="text" name="traveller2" size="20" /></td>
</tr>
<tr>
<th align="right">Date Code: </th>
<td><input type="text" name="datecode2" size="20" /></td>
</tr>
<tr>
<th align="right"> </th>
<td><input type="hidden" name="id" size="20" value="<?php if(empty($_POST['traveller'])) {echo ''; } else {echo $row2['IDRec'];} ?>"/></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" name="btn_add_details" id="btn_add_details" onClick="return check_submit();" value="Add the Detail(s)" onfocus="setStyle(this.id)"
style="color: #000000;
padding: 2px 5px;
border: 2px solid;
border-color: #7bf #07c #07c #4AA02C;
background-color: #09f;
font-family: Georgia, ..., serif;
font-size: 18px;
display: block;
height: 30px;
width: 200px;" /> </td>
</tr>
</table>
Since you stated that IDRec is your PK and is set to auto-increment, AND you are pulling records based on a single number... you will never get more than one result in your resultset.
Change this code block...
$sql2="SELECT * FROM sample_barcode WHERE IDRec='".$row['IDRec']."'";
$result2=mysql_query($sql2);
if(!$result2) {
echo "<h1>Could not process query this time. Please try again later!</h1>";
}
else {
while($row2=mysql_fetch_array($result2)) {
echo "<form name='form2' method='POST'>";
echo "<table class='output' border=2 align=center>";
echo "<tr class='thcolor'>";
echo "<th>Parent</th>";
echo "<th>LOT Traveller No.</th>";
echo "<th>Datecode</th>";
echo "</tr>";
echo "<tr>";
echo "<td>".$row2['LotTraveller']."</td>";
echo "<td>".$row2['LotTraveller']."</td>";
echo "<td>".$row2['Datecode']."</td>";
echo "</tr>";
echo "</table>";
echo "</form>";
}
echo "<br><h1>Data successfully loaded!</h1>";
}
mysql_close($link);
}
To this...
$sql2="SELECT * FROM sample_barcode WHERE IDRec=".$row['IDRec']."";
$result2=mysql_query($sql2);
$row2=mysql_fetch_assoc($result2); // added this line
if(!$result2) {
echo "<h1>Could not process query this time. Please try again later!</h1>";
}
else {
echo "<form name='form2' method='POST'>";
echo "<table class='output' border=2 align=center>";
echo "<tr class='thcolor'>";
echo "<th>Parent</th>";
echo "<th>LOT Traveller No.</th>";
echo "<th>Datecode</th>";
echo "</tr>";
echo "<tr>";
echo "<td>".$row2['LotTraveller']."</td>";
echo "<td>".$row2['LotTraveller']."</td>";
echo "<td>".$row2['Datecode']."</td>";
echo "</tr>";
echo "</table>";
echo "</form>";
echo "<br><h1>Data successfully loaded!</h1>";
}
mysql_close($link);
}
In the first line I removed the single quotes around your IDRec - this is an integer so single quotes are not needed.
Removed your while loop - you will only ever get one result so a loop is not needed.
Also, this doesn't appear to actually be a form so you can also remove your <form> </form> tags.
Finally, start learning pdo_mysql. my_sql has been deprecated. Anyone still using this code will wake up one day to find out that their website has magically stopped functioning.
EDIT
In your add_data function, change it to this...
function add_data() {
include("db_PSIS.php");
$sql="INSERT INTO sample_barcode (LotTraveller, ShipmentLotNumber) VALUES ('".$_POST['traveller']."', '".$_POST['datecode']."')";
$result=mysql_query($sql);
$sql="SELECT * FROM sample_barcode ORDER BY IDRec DESC"; // added this line
$result=mysql_query($sql); // added this line
$row=mysql_fetch_assoc($result); // changed this line from array to assoc
mysql_close($link);
}
This will get the result of the data you just entered. I added a query to get the data you need to display.
I have a login page which sets a _SESSION parameter of 'id' upon login. After logging in the user is redirected to the index.php page and at this point the session parameter is still set, I know this because I can print it with PHP. However, when navigating to the account.php page the parameter is lost and I am not sure why.
Most of the other questions asked on here regarding this are because of the session_start not being used but I have used this on the account.php page.
Any help in resolving this would be grateful.
The code for the account.php page is below:
<?php session_start(); ?>
<?php
$con=mysqli_connect("localhost","tkernick96","Tylerkernick1996","users");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$id = $_SESSSION['id'];
$query = mysqli_query($con,"SELECT * FROM users WHERE id = $id");
$row = mysqli_fetch_array($query,MYSQLI_ASSOC);
$firstname = $row ['firstname'];
$lastname = $row ['lastname'];
$email = $row ['email'];
$age = $row ['age'];
$nationality = $row ['nationality'];
$language1 = $row ['language1'];
$language2 = $row ['language2'];
$language3 = $row ['language3'];
$language4 = $row ['language4'];
$language5 = $row ['language5'];
$form = "<div id='header'>
<img src='logo.png' id='logo'>
<div id='headerdiv'>
<table id='login'>
<tr>
<td style='padding-right: 20px;'>
<h2 style='margin-top: 0px'>No account?</h2>
<h3 style='margin-top: 0px'><a href='signup.php'>Sign Up Now!</a></h3>
</td>
<td style='padding-left: 20px; border-left: 1px solid #838383'>
<h3>Login:</h3>
<form method='post' action='login.php'>
<input id='logininput' type='email' name='email' placeholder='Email'>
<br>
<input id='logininput' type='password' name='password' placeholder='Password'>
<input type='submit' name='login' value='Login'>
</form>
<p id='p1'><a href='forgotdetails.php'>Forgotten Details?</a></p>
</td>
</tr>
</table>
</div>
</div>";
$account = "<div id='header'>
<img src='logo.png' id='logo'>
<div id='headerdiv'>
<table id='accounttable'>
<tr>
<td id='account'>
<p id='p2'><a href='account.php'>My Account</a></p>
<p id='p2'><a href='account/companions.php'>My Companions</a></p>
<p id='p2'><a href='account/messages.php'>Messages(".$messages.")</a></p>
<p id='p2'><a href='logout.php'>Sign Out</a></p>
</td>
</tr>
</table>
</div>
</div>
<table id='profiletable'>
<tr>
<td id='profiletabletd1' rowspan='2'>
<img src='images/avatar.png' id='profileimage'>
</td
<td id='profiletabletd1'><p id='profilename'>".$firstname." ".$lastname.", ".$age."</p></td>
</tr>
<tr>
<td id='profiletabletd3'><p id='profileinfo'>Nationality: <b>".$nationality."</b>     Languages Spoken: <b>".$language1." ".$language2." ".$language3." ".$language4." ".$language5."</b></p></td>
</tr>
</table>
";
if ($id == "") {
$output = $form;
}
else {
$output = $account;
}
mysqli_close($con);
?>
index.php:
<?php session_start(); ?>
<?php
$con=mysqli_connect("localhost","tkernick96","Tylerkernick1996","users");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$id = $_SESSION['id'];
$form = "<table id='login'>
<tr>
<td style='padding-right: 20px;'>
<h2 style='margin-top: 0px'>No account?</h2>
<h3 style='margin-top: 0px'><a href='signup.php'>Sign Up Now!</a></h3>
</td>
<td style='padding-left: 20px; border-left: 1px solid #838383'>
<h3>Login:</h3>
<form method='post' action='login.php'>
<input id='logininput' type='email' name='email' placeholder='Email'>
<br>
<input id='logininput' type='password' name='password' placeholder='Password'>
<input type='submit' name='login' value='Login'>
</form>
<p id='p1'><a href='forgotdetails.php'>Forgotten Details?</a></p>
</td>
</tr>
</table>";
$account = "<table id='accounttable'>
<tr>
<td id='account'>
<p id='p2'><a href='account.php'>My Account</a></p>
<p id='p2'><a href='account/companions.php'>My Companions</a></p>
<p id='p2'><a href='account/messages.php'>Messages($messages) </a></p>
<p id='p2'><a href='logout.php'>Sign Out</a></p>
</td>
</tr>
</table>";
if ($id == "") {
$output = $form;
}
else {
$output = $account;
}
mysqli_close($con);
?>
it should be $_SESSION not $_SESSSION!
you should see a warning with the right error reporting level, e.g.
error_reporting(E_ALL);
Are you using IIS?
If it is, you should check the php.ini file (under c:\php or in c:\windows), find the session.save_path parameter, and be sure that points to a folder that exists.
Then edit the rights of that folder and give read/write permissions to the IUSR user.
If you're using Apache, you should find the user that is starting the daemon (you can find this in /etc/apache2/apache.conf), then be sure that that user have r/w permissions in the folder that php.ini session.save_path points.
Restart the IIS or Apache after that.
Hope it helps.
I have a page to view and delete data from database. The delete is a link that clicked and pass the variable in url to another page for delete query. For example i have 3 rows in total in my database, i have no problem in deleting the last two rows but i was unable to delete the first row because the delete is deleting the record based on the "description" column, not the "ID" column. Below is the submission page and delete query page
viewreportdb.php
<!doctype html>
<head>
<meta charset="utf-8">
<title>Report Database </title>
<link href="style/fg_membersite.css" rel='stylesheet' type='text/css'>
</head>
<?php
include('config.php');
include('adminconfig.php');
$sql = "SELECT * FROM report ORDER BY ID";
$result = mysql_query($sql);
?>
<body>
<form method="POST" action="reportsave.php" onSubmit="return validate(this);">
<table id='fcolor2' class="viewdb3">
<tr>
<td style='border:2px solid black; background-color:#FF9933;' align=center> ID </td>
<td style='border:2px solid black; background-color:#FF9933;' align=center> Name </td>
<td style='border:2px solid black; background-color:#FF9933; width:auto;' align=center> Department </td>
<td style='border:2px solid black; background-color:#FF9933; width:auto;' align=center> Description </td>
<td style='border:2px solid black; background-color:#FF9933; width:auto;' align=center> Date </td>
</tr><?!-- missing close tr -->
<?php
while($row = mysql_fetch_array($result))
{
?>
<tr>
<td style='border:2px solid black; background-color:#33FF00; width:auto' align=center><?php echo $row['ID'] ?></td>
<td style='border:2px solid black; background-color:#33FF00; width:auto' align=center><?php echo $row['name'] ?></td>
<td style='border:2px solid black; background-color:#33FF00; width:auto' align=center><?php echo $row['department'] ?></td>
<td><textarea rows="5" cols="50" name="description[]"><?php echo $row['description']; ?></textarea></td>
<td style='border:2px solid black; background-color:#33FF00; width:auto' align=center><?php echo $row['date'] ?></td>
</tr><!-- missing close tr -->
<tr>
<td><input class='edit' type = 'text' name="ID[]" value='<?php echo $row['ID'] ?>' maxlength="50"/></td>
<td><input class='edit' type = 'text' name="name[]" value='<?php echo $row['name'] ?>' maxlength="50"></td>
<td><input class='edit' type = 'text' name="department[]" value='<?php echo $row['department'] ?>' maxlength="50"></td>
<td> </td>
<td><input class='edit' type = 'text' name="date[]" value='<?php echo $row['date'] ?>' maxlength="50"></td>
<?php echo "<td><a href='http://localhost/kinhock/deletereport.php?description=".$row['description']."'>Delete</a></td>";?>
</tr><!-- missing close tr -->
<?php
} ?>
<tr class='btn2'><td><input type='submit' name='submit' value='Submit' /></td></tr>
<tr class='btn2'><td> Back to Description </td></tr><!-- missing open & close tr-->
<tr class='btn2'><td> Logout</td></tr>
</table>
</form>
<?php
$ID=$row['ID'];
$name=$row['name'];
$department=$row['department'];
$date=$row['date'];
$description=$row['description'];
?>
</body>
</html>
deletereport.php
<?php
include('adminconfig.php');
include('config.php');
$description = $_GET['description'];
$sql="DELETE FROM `report` WHERE `description`='$description'";
$result=mysql_query($sql);
if(!$result){
die('invalid query:'.mysql_error());
}else
?>
<p style="font-family:arial;color:#0066CC;font-size:30px;">One row deleted...</p>
<?php
header('Refresh:3; url=viewreportdb.php');
die;
?>
Do not delete using description field because urlencoding and many more factors can break this code, even duplicate descriptions are quite possible, isn't it? Also your code is
liable to easiest SQL Injection methods!
Change your code here:
<?php echo "<td><a href='http://localhost/kinhock/
deletereport.php?id=".$row['ID']."'>Delete</a></td>";?>
And your SQL query:
$id = intval($_GET['id']);
$sql="DELETE FROM `report` WHERE `id`='$id'";
STEP 1
Send the ID,not the description, for deletion.
<?php echo "<td><a href='http://localhost/kinhock
/deletereport.php?id=".$row['ID']."'>Delete</a></td>";?>
STEP 2
Delete using the ID, not the description.
$id = intval($_GET['id']);
$sql="DELETE FROM `report` WHERE `id`=$id";
You should probably use the ID instead of the description within your where clause and in this line:
(change)
<?php echo "<td><a href='http://localhost/kinhock
/deletereport.php?description=".$row['description']."'>Delete</a></td>";?>
(to)
<?php echo "<td><a href='http://localhost/kinhock
/deletereport.php?id=".$row['ID']."'>Delete</a></td>";?>
and finally change:
$description = $_GET['description'];
$sql="DELETE FROM `report` WHERE `description`='$description'";
to:
$id = $_GET['id'];
$sql="DELETE FROM `report` WHERE `ID`='$id'";
Using the ID will be faster and more accurate assuming it is a primary or unique ID.
You may also want to use:
$id = mysql_real_escape_string($_GET['id']);
to prevent SQL injection.
I believe your report table's ids are based on the users IDs right? Why don't you make it such that each report had an ID(report ID), User_ID(eg. admin001) and a description. In this way if admin001 makes report A it will have ID 1, if admin001 makes report B it will have ID 2. si when you want to delete Report A you use
$sql = "delete from {tablename} where ID={report ID}";
I've been having a really annoying "error"...
My page have a header, left sidebar and a footer. After i submit the form everything goes really good EXCEPT now the footer is in the middle of the page!! I tried everything i knew to and searched but i still can't solve that.
BEFORE SUBMIT with footer on the right place : http://i48.tinypic.com/ly1dl.jpg
AFTER SUBMIT with footer on the middle of the page : http://i47.tinypic.com/168a3uq.jpg
Here is The code :
Header:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="stylesheets/stylesheet.css" type="text/css" rel="stylesheet" />
<title>Wilson Electric & Alarm Company</title>
</head>
<body>
<div id="header">
<h1 id="toptitle"> Wilson Electric & Alarm Company </h1>
</div>
<div id="navigation_top">
</div>
<div id="column_left">
<ul id="left_menu">
<li> Insert Page </li>
<li> Search Page </li>
<li> Menu 4 </li>
<li> Menu 5 </li>
<li> Menu 6 </li>
<li> User </li>
<li> Admin </li>
<li> Login </li>
<li> Create Username </li>
</ul>
</div>
<div id="content">
Footer :
</div>
<div id="footer">
This is a Footer.
</div>
</body>
</html>
Stylesheet : (there is also a default reset in my stylesheet before the #container)
#container {
width:100%;
height:100%;
}
#header {
width: 100%;
height:120px;
}
#column_left {
float:left;
width:15%;
height:100%;
border-right: 3px solid #06F;
border-left: 3px solid #06F;
}
#content {
width:100%;
height:100%;
}
#footer {
width:100%;
height: 30px;
border-top:3px solid #06F;
}
#navigation_top {
width:100%;
height:15px;
border-bottom:3PX solid #06F;
}
#toptitle {
padding:30px;
font-size:300%;
font-weight:700;
}
.formtitle {
font-weight:800;
font-size:150%;
}
#form {
margin-left:230px;
padding-top:30px;
}
table,th, td {
border: 1px solid black;
}
table {
width:1024px;
}
th {
font-weight:800;
background-color:#0FF;
}
And here is the page code that is giving me those problems!!
<?php require_once("includes/connection.php"); ?>
<?php require_once("includes/functions.php"); ?>
<?php
if (isset($_POST['search'])) {
$date1 = mysql_prep($_POST['date1']);
$date2 = mysql_prep($_POST['date2']);
$latte = mysql_prep($_POST['latte']);
$coffee = mysql_prep($_POST['paid']);
$query = "SELECT *
FROM payroll
WHERE (day BETWEEN '{$date1}' AND '{$date2}')
AND (company = '{$latte}')
AND (paid = '{$coffee}')
ORDER BY day ASC ";
$result = mysql_query($query, $connection);
$woof = "SELECT SUM(p.hours) AS sum_hours
, COUNT(DISTINCT p.day) AS cnt_days
FROM PAYROLL p
WHERE p.day BETWEEN '{$date1}' AND '{$date2}'
AND company = '{$latte}'
AND paid = '{$coffee}' ";
$raw = mysql_query($woof, $connection);
if(!$raw) { die(mysql_error());}
$meow = mysql_result($raw, 0, 0);
$days = mysql_result($raw, 0, 1);
if(!$result) {
echo "FAIL";
} else {
$message = "<table>
<tr>
<th> Date </th>
<th> Hours </th>
<th> Job Title </th>
<th> Job Description </th>
<th> For </th>
<th> Paid </th>
</tr>";
while($row = mysql_fetch_array($result)) {
$company = $row['company'];
if($company == 0) {
$company = "Wilson Electric";
} if($company == 1) {
$company = "Wilson Rental";
} if ($company == 2) {
$company = "Church of Christ";
}
$paid = $row['paid'];
if($paid == 0) {
$paid = "Yes";
} else {
$paid = "<form action=\"update.php\" method=\"post\" ><input type=\"checkbox\" name=\"paid\" value=\"0\"> NO ";
}
$message .= "<tr>";
$message .= "<td class=\"center\">" . $row['day'] . "</td>";
$message .= "<td class=\"center\">" . $row['hours'] . "</td>";
$message .= "<td style=\"padding:5px;\">" . $row['job_title'] . "</td>";
$message .= "<td style=\"padding:5px;\">" . $row['job_description'] . "</td>";
$message .= "<td style=\"padding:5px;\">" . $company . "</td>";
$message .= "<td class=\"center\">" . $paid . "</td>";
$message .= "</tr>";
}
$message .= "<tr>";
$message .= "<td class=\"center\"> Total Days: " . $days . "</td>";
$message .= "<td class=\"center\"> Total Hours: " . $meow . "</td>";
$message .= "<td class=\"center\"> </td>";
$message .= "<td class=\"center\"> </td>";
$message .= "<td class=\"center\"> </td>";
$message .= "<td class=\"center\"> <input type=\"submit\" name=\"gamind\" value=\"Update\"> </form> </td>";
$message .= "</tr>";
}
}
?>
<?php include("includes/header.php"); ?>
<form action="" method="post" id="form">
<h1 class="formtitle"> Search </h1>
<br />
<table id="table1">
<th> From </th>
<th> To </th>
<th> For </th>
<th> Paid </th>
<tr>
<td>
<input type="date" name="date1" value="" >
</td>
<td>
<input type="date" name="date2" value="" >
</td>
<td>
<select name="latte" >
<option value="0"> Wilson Electric </option>
<option value="1"> Wilson Rental </option>
<option value="2"> Church of Christ</option>
</select>
</td>
<td>
<input type="radio" name="paid" value="0"> Yes <br />
<input type="radio" name="paid" value="1"> No <br />
</tr>
<tr>
<td colspan="4" align="right"> <input type="submit" name="search" value="Search">
</td>
</tr>
</table>
<br />
<br />
<p>
<?php
if(!empty($message)) {
echo "<h1 class=\"formtitle\"> Payroll Result </h1>";
echo $message;
}
?>
</p>
</form>
<?php include("includes/footer.php"); ?>
<?php mysql_close($connection); ?>
Sorry for the loooong post!! I hope someone knows the solution for my problem!
Thanks! :)
There's a lot of code here to take in, but I think your $message builds <tr>s, but when you echo $message, you're not inside a table element..?