Monday 8 August 2016

Find out nth order salary in MySQL

To find out the nth highest salary from employee table :
SYNTAX:

SELECT * from employee emp WHERE (n) = (SELECT COUNT(emp2.salary) FROM employee emp2 WHERE emp2.salary >= emp.salary)
where n is the number of salary which you want.

OR

To Find the 2nd Highest Salary in following ways



SELECT name, salary FROM employee where salary NOT IN (SELECT MAX(salary) from employee)

SELECT MAX(salary) FROM employee where salary NOT IN (SELECT MAX(salary) from employee)
SELECT DISTINCT salary FROM employee ORDER BY salary DESC LIMIT 1,1;

Send Mail via PHP

Send an Email via PHP
Step 1: Create the form for an Email



<form action="mail_send.php" method="post" name="email">
<table width="354" height="279" >
<tr><td>To:</td><td><input type="email" name="to" value="" />
</td></tr>
<tr><td>From Name:</td><td><input type="text" name="name" value="" />
</td></tr>
<tr><td>Subject:</td><td><input type="subject" name="subject" value="" />
</td></tr>
<tr><td>From Email:</td><td><input type="email" name="femail" value="" />
</td></tr>
<tr><td colspan="2"><input type="submit" name="submit" value="SEND" />
</td></tr>
<tr><td>Message:</td><td><textarea cols="20" name="message" rows="8">
</textarea></td>
</tr>
</table>
</form>
Step 2: Write the php script for email


<?php 
if(isset($_POST[femail])){
    $to = $_POST["to"];
    $femail = $_POST["femail"];
    $name = $_POST["name"];
    $subject = $_POST["message"];
    $message = wordwrap ($message, 70);
    mail($to, $subject, $message, "From: $femail\n");
    echo "Thank you for sending a mail";
}else{
    echo 'mail not send..!!';
}
?>


Wednesday 3 August 2016

Make dropdown as a required field in HTML5

If you want to make <select> field as a required field then you have to do like this
<select name="" required x-moz-errormessage="Please Select the Field">
<option value="">Select the Field</option>
<option value="One">One</option>
<option value="Two">Two</option>
</select>

INNER JOIN of two tables

if you have two tables like product and product_description  and you want the inner join of these tables then the query will be like this:
select product.model, product_description.name, product.price, product.weight, 
product_description.description from product INNER JOIN product_description on
product.product_id=product_description.product_id

Tuesday 2 August 2016

ADD Foreign Key in existing Table

How to add Foreign Key in existing table
if you want to add any column as a foreign key in your existing table then you have to write following MySQL query for that
Syntax:
ALTER TABLE tablename ADD CONSTRAINT FOREIGN KEY column_name REFERENCES ref_table_name(column_name)
if you want to add multiple column as a foreign key in the same table then you have to write the following MySQL query:
Syntax:
ALTER TABLE tablename 
ADD CONSTRAINT FOREIGN KEY column_name REFERENCES ref_table_name(column_name)
ADD CONSTRAINT FOREIGN KEY column_name REFERENCES ref_table_name(column_name)
ADD CONSTRAINT FOREIGN KEY column_name REFERENCES ref_table_name(column_name)

Confirm before DELETE the record by javascript

<script type="text/javascript">
function ConfirmDelete() {
if (confirm("Delete Account?"))
location.href='linktoaccountdeletion';
}
</script> echo '<input type="button" onclick="ConfirmDelete()" value="DELETE ACCOUNT">';

File Upload Validation using javascript

Below are the script by which you can validate file when you upload
Make the form
<form>
<div class=”form-level“>Upload Resume
<input name=”image” type=”file” id=”upload_resume” placeholder=”Upload Resumeclass=”form-control” /></div>
</form>
var fup = document.getElementById('upload_resume'); 
var fileName = fup.value;
var ext = fileName.substring(fileName.lastIndexOf('.') + 1);
if(ext == "doc" || ext == "docx")
{
return true;
}
else
{
alert("Please select your Resume. Only pdf, doc and docx files are accepted");
fup.value="";
fup.focus();
return false;
}
}

3rd Highest Salary from employee table

SELECT * FROM employee one1 WHERE ( 3 ) = ( SELECT COUNT( one2.emp_salary ) FROM employee one2 WHERE one2.emp_salary >= one1.emp_salary )

Redirect to target page by Dropdown List

Redirect to other page by dropdown list :
Step 1: Write down the bit of php code
<?php
if (isset($_POST[‘nav’])) {
 header(“Location: $_POST[nav]”);
}
?>
Step 2: javascript code
<script type=”text/javascript”>
$(function() {
    $(“#submit”).hide();
    $(“#page-changer select”).change(function() {
        window.location = $(“#page-changer select option:selected”).val();
    })
});
</script>
Step 3: Make Html form & fields
<form name=”form1″ id=”page-changer” action=”” method=”post”>
    <div align=”center”><strong>Select Your Option  &nbsp;&nbsp;&nbsp;&nbsp;</strong>
      <select name=”nav”>
  <option value=””>Select Your Option…</option>
  <option value=”paidstp.php”>All Paid Students</option>
  <option value=”paidlocation.php”>Search Date & Location Wise</option>
</select>
      <input type=”submit” value=”Go” id=”submit” />
    </div>
</form>
Untitled

Email ID & mobile number validation in JS

Below are the script by which you can validate email id and mobile number field.
Step 1: Make the form
<div class="form-level">
     <input type="text" class="form-control" name="email" placeholder="Email Id" />
     <span class="form-icon fa fa-envelope"></span>
</div>
<div class="form-level">
      <input name="mobile" placeholder="Enter your Mobile Number" id="mobile" class="form-control" value="" type="text">
      <span class="form-icon fa fa-phone"></span>
</div>

Now apply the below script in your head section

var email = document.fromValidate.email;
var mobile = document.fromValidate.email;
if(email.value==''){
   window.alert('Pleaseer the email id');
   email.focus();
   return false;
}
if(email.value.indexOf('@',0 window.alert('Pleaseer the valid email');
  email.focus();
  return false;
}
if(email.value.indexOf('.',0 window.alert('Pleaseer the valid email');
  email.focus();
  return false;
}
if(mobile.value==''){
  window.alert('Pleaseer the mobile number')
  mobile.focus();
  return false;
}
if(mobile.value.length<10  window.alert('Pleaseer valid mobile number');
  mobile.focus();
}
if(mobile.value.length>10 window.alert('Pleaseer valid mobile number');
 mobile.focus();
}
if(!parseInt(mobile.value)){
  window.alert("Enterts only");
  mobile.focus();
  return false;
}
return true;
}

Fetch the month and year from date where date datatype is datetime

if you want to fetch the records from the below table
Order(varchar)
Order Date(datetime)
 1502014-09-26 10:36:18
 1500 2014-08-26 09:36:18
 5200 2014-06-26 01:36:18
 352 2014-09-26 22:36:18
 8654 2014-05-26 20:36:18
 2554 2014-09-26 14:36:18
 780 2014-03-26 18:36:18
 1200 2014-09-26 15:36:18
Ques: Find out Total amount and month name of order in the Sep 2015
QUERY: SELECT SUM( total ) , month( date_added ) FROM `order` WHERE month( date_added ) =9
OUTPUT: 
Order
month
 4256   9