How To delete Records From PHP & (MySql)



Share

The main way that website users interact with PHP and MySQL is through the use of HTML forms. These were introduced very early on in the development of the World Wide Web—in 1993, even before the advent of ecommerce—and have remained a mainstay ever since, due to their simplicity and ease of use.

Of course, enhancements have been made over the years to add extra functionality to HTML form handling; this chapter will bring you up to speed on the state of the art and show you the best ways to implement forms for good usability and security.



Building Forms


Handling forms is a multipart process. First a form is created, into which a user can enter the required details. This data is then sent to the web server, where it is interpreted, often with some error checking. If the PHP code identifies one or more fields that require reentering, the form may be redisplayed with an error message. When the code is satisfied with the accuracy of the input, it takes some action that usually involves the database, such as entering details about a purchase.

To build a form, you must have at least the following elements:
1. An opening <form> and closing </form> tag
2. A submission type specifying either a GET or POST method
3. One or more input fields
4. The destination URL to which the form data is to be submitted
In below shows a very simple form created using PHP. Type it in and save it as formtest.php.


<?php
<html>
<head>
<title>
</title>
</head>
<body> 
<form method="post" action="formtest.php">
What is your name?    
<input type="text" name="name" />
<input type="submit" />
</form> 
</body>
</html>
?>
 

Default Values


Sometimes it’s convenient to offer your site visitors a default value in a web form. For example, suppose you put up a loan repayment calculator widget on a real estate website. It could make sense to enter default values of, say, 25 years and 6 percent interest, so that the user can simply type in either the principal sum to borrow or the amount that she can afford to pay each month.

     <form method="post" action="calc.php">
     Loan Amount <input type="text" name="principle" /> Monthly Repayment
     <input type="text" name="monthly" />  
     Number of Years <input type="text" name="years" value="25" />   Interest Rate <input type="text" name="rate" value="6" />



Input Types


HTML forms are very versatile and allow you to submit a wide range of different types of inputs, ranging from text boxes and text areas to checkboxes, radio buttons, and more.

Text boxes

Probably the type of input you will use most often is the text box. It accepts a wide range of alphanumeric text and other characters in a single-line box. The general format of a text box input is:


       <input type="text" name="name" size="size" maxlength="length" value="value" / >



Text areas


When you need to accept input of more than a single line of text, use a text area. This is similar to a text box but, because it allows multiple lines, it has some different parameters. Its general format looks like this:


        <textarea name="name" cols="width" rows="height" wrap="type">
        </textarea> 
    



Checkboxes


When you want to offer a number of different options to a user, from which he can select one or more items, checkboxes are the way to go. The format to use is :


    <input type="checkbox" name="name" value="value" checked="checked" />


Radio buttons


Radio buttons are named after the push-in preset buttons found on many older radios, where any previously depressed button pops back up when another is pressed. They are used when you want only a single value to be returned from a selection of two or more options. All the buttons in a group must use the same name and, because only a single value is returned, you do not have to pass an array.


    8am-Noon <input type="radio" name="time" value="1" / >
    Noon-4pm <input type="radio" name="time" value="2" checked="checked"/ >
    4pm-8pm <input type="radio" name="time" value="3" / >



Hidden fields


Sometime it is convenient to have hidden form fields so that you can keep track of the state of form entry. For example, you might wish to know whether a form has already been submitted. You can achieve this by adding some HTML such as the following to your PHP code:


     echo '<input type="hidden" name="submitted" value="yes" />'