Finding Duplicate Records in MySQL using PHP


<html>
<head>
<title>Record Confirmation</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body bgcolor="#99FFFF">
<?php
$id = $_REQUEST['id'];
$first_name = strtoupper($_REQUEST['first_name']);
$last_name = strtoupper($_REQUEST['last_name']);
 
$connet=mysql_connect("localhost","root","") or die(mysql_error());
$db=mysql_select_db("student") or die(mysql_error());
 
// Code for checking if the text field is empty using
// different conditions
 
$id_no = (empty($id));
$first_name2 = (empty($first_name)) ;
$last_name2 = (empty($last_name));
 
if ($id_no == true && $first_name2 == true
&& $last_name2 == true)
{
echo "<h1> Please filled out the form correctly!</h1>";
}
else if ($first_name2 == true && $last_name2 == true)
{
echo "<h3> Both first name and last name is not filled out correctly!</h1>";
}
else if ($id_no == true && $first_name2 == true)
{
echo "<h3> Both ID No. and First Name is not filled out correctly!</h1>";
}
else if ($id_no == true && $last_name2 == true)
{
echo "<h3> Both ID No. and Last Name is not filled out correctly!</h1>";
}
 
else if ($id_no == true)
{
echo "<h1> The student ID No. is not filled out correctly!</h1>";
}
else if ($first_name2 == true)
{
echo "<h1> The student first name is not filled out correctly!</h1>";
}
else if ($last_name2 == true)
{
echo "<h1> The student last name is not filled out correctly!</h1>";
}
 
// Code for checking for duplicate record in the database
// for the first name and the last name of the student.
 
else if (mysql_num_rows(mysql_query("SELECT first_name,last_name
FROM info WHERE first_name = '$first_name' AND
last_name = '$last_name'"))) {
echo "<center> <br> <br> <h2> Sorry Duplicate Record for "
.$first_name. " " .$last_name. ". ";
echo "<center> <h3> Please Try Again. ";
 
}
else {
$sql = "INSERT INTO info(id,first_name,last_name)
VALUES ('$id','$first_name','$last_name')";
$query = mysql_query($sql) or die(mysql_error());
echo "<center> <br> <br> <h2> The record of "
.$first_name. " " .$last_name.
" has been saved in the database.";
}
?>
</body>
</html>

Best practices for speeding up your website


1) Minify JavaScript and CSS
Minification is the practice of removing unnecessary characters from code to reduce its size thereby improving load times. When code is minified all comments are removed, as well as unneeded white space characters (space, newline, and tab). In the case of JavaScript, this improves response time performance because the size of the downloaded file is reduced. Two popular tools for minifying JavaScript code are JSMin and YUI Compressor. The YUI compressor can also minify CSS.
Obfuscation is an alternative optimization that can be applied to source code. It’s more complex than minification and thus more likely to generate bugs as a result of the obfuscation step itself. In a survey of ten top U.S. web sites, minification achieved a 21% size reduction versus 25% for obfuscation. Although obfuscation has a higher size reduction, minifying JavaScript is less risky.
In addition to minifying external scripts and styles, inlined
An alternative in PHP would be to create a function called insertScript.
In addition to preventing the same script from being inserted multiple times, this function could handle other issues with scripts, such as dependency checking and adding version numbers to script filenames to support far future Expires headers.
3)Minimize the Number of iframes
Iframes allow an HTML document to be inserted in the parent document. It's important to understand how iframes work so they can be used effectively.
iframe pros:
Helps with slow third-party content like badges and ads
Security sandbox
Download scripts in parallel
iframe cons:
Costly even if blank
Blocks page onload
Non-semantic
4)Reduce Cookie Size
HTTP cookies are used for a variety of reasons such as authentication and personalization. Information about cookies is exchanged in the HTTP headers between web servers and browsers. It's important to keep the size of cookies as low as possible to minimize the impact on the user's response time.
For more information check "When the Cookie Crumbles" by Tenni Theurer and Patty Chi. The take-home of this research:
Eliminate unnecessary cookies
Keep cookie sizes as low as possible to minimize the impact on the user response time
Be mindful of setting cookies at the appropriate domain level so other sub-domains are not affected
Set an Expires date appropriately. An earlier Expires date or none removes the cookie sooner, improving the user response time
5) Use Cookie-free Domains for Components
When the browser makes a request for a static image and sends cookies together with the request, the server doesn't have any use for those cookies. So they only create network traffic for no good reason. You should make sure static components are requested with cookie-free requests. Create a subdomain and host all your static components there.
If your domain is www.example.org, you can host your static components on static.example.org. However, if you've already set cookies on the top-level domain example.org as opposed to www.example.org, then all the requests to static.example.org will include those cookies. In this case, you can buy a whole new domain, host your static components there, and keep this domain cookie-free. Yahoo! uses yimg.com, YouTube uses ytimg.com, Amazon uses images-amazon.com and so on.
Another benefit of hosting static components on a cookie-free domain is that some proxies might refuse to cache the components that are requested with cookies. On a related note, if you wonder if you should use example.org or www.example.org for your home page, consider the cookie impact. Omitting www leaves you no choice but to write cookies to *.example.org, so for performance reasons it's best to use the www subdomain and write the cookies to that subdomain.
6) Minimize DOM Access
Accessing DOM elements with JavaScript is slow so in order to have a more responsive page, you should:
Cache references to accessed elements
Update nodes "offline" and then add them to the tree
Avoid fixing layout with JavaScript
7) Choose over @import
One of the previous best practices states that CSS should be at the top in order to allow for progressive rendering.
In IE @import behaves the same as using at the bottom of the page, so it's best not to use it.
8) Don't Scale Images in HTML
Don't use a bigger image than you need just because you can set the width and height in HTML. If you need
My Cat
then your image (mycat.jpg) should be 100x100px rather than a scaled down 500x500px image.
9) optimize Images
After a designer is done with creating the images for your web page, there are still some things you can try before you FTP those images to your web server.
You can check the GIFs and see if they are using a palette size corresponding to the number of colors in the image. Using imagemagick it's easy to check using identify -verbose image.gif When you see an image using 4 colors and a 256 color "slots" in the palette, there is room for improvement. Try converting GIFs to PNGs and see if there is a saving. More often than not, there is. Developers often hesitate to use PNGs due to the limited support in browsers, but this is now a thing of the past. The only real problem is alpha-transparency in true color PNGs, but then again, GIFs are not true color and don't support variable transparency either. So anything a GIF can do, a palette PNG (PNG8) can do too (except for animations). This simple imagemagick command results in totally safe-to-use PNGs: convert image.gif image.png
10) Avoid Empty Image src
Image with empty string src attribute occurs more than one will expect. It appears in two form:
straight HTML
JavaScript
var img = new Image();
img.src = "";
Both forms cause the same effect: browser makes another request to your server.
1. Internet Explorer makes a request to the directory in which the page is located.
2. Safari and Chrome make a request to the actual page itself.
3. Firefox 3 and earlier versions behave the same as Safari and Chrome, but version 3.5 addressed this issue[bug 444931] and no longer sends a request.
4. Opera does not do anything when an empty image src is encountered.

PHP to get html & metadata for website


Following script can be used to get the first part of a remote file to parse the html elements in local script. eregi() function is used here to parse the meta keywords, the meta description and title element.
Extra rules and regex patterns can be added for more meta elements. We can use this script for adding new links in to a linklist.
Php script is as below:


  <?php   $page_title = "NA";   $meta_descr = "NA";   $meta_keywd = "NA";   if ($handle = @fopen("http://www.your_url.com", "r")) {   $content = "";   while (!feof($handle)) {   $part = fread($handle, 1024);   $content .= $part;   if (eregi("</head>", $part)) break;   }   fclose($handle);   $lines = preg_split("/\r?\n|\r/", $content); // turn the content in rows   $is_title = false;   $is_descr = false;   $is_keywd = false;   $close_tag = ($xhtml) ? " />" : ">"; // new in ver. 1.01   foreach ($lines as $val) {   if (eregi("<title>(.*)</title>", $val, $title)) {   $page_title = $title[1];   $is_title = true;   }   if (eregi("<meta name=\"keywords\" content=\"(.*)\"([[:space:]]?/)?>", $val, $keywd)) {   $meta_keywd = $keywd[1];   $is_keywd = true;   }   if (eregi("<meta name=\"description\" content=\"(.*)\"([[:space:]]?/)?>", $val, $descr)) {   $meta_descr = $descr[1];   $is_descr = true;   }   if ($is_title && $is_descr && $is_keywd) break;   }   }   ?>