Posts

create and validate a registration form

Image
  PROGRAM:   <!DOCTYPE HTML>   <html> <head> <style> .error {color: #FF0000;} </style> </head> <body> <?php /* define variables and set to empty values */ $nameErr = $emailError = $mobileError =""; $name = $email = $mobile =   ""; if ($_SERVER["REQUEST_METHOD"] == "POST") {   if (empty($_POST["name"])) {     $nameErr = "Name is required";   }   else {     $name = test_input($_POST["name"]);     /* check if name only contains letters and whitespace */     if (!preg_match("/^[a-zA-Z ]*$/",$name)) {       $nameErr = "Only letters and white space allowed";     }   }   if (empty($_POST["name"])) {     $mobileError = "Name is required";   } else {     $mobile = test_input($_POST["mobile"]);     /* ch...

PHP - A Simple HTML Form

PHP - A Simple HTML Form   The example below displays a simple HTML form with two input fields and a submit button: Example < html > < body > < form  action ="welcome.php"  method ="post"> Name:  < input  type ="text"  name ="name">< br > E-mail:  < input  type ="text"  name ="email">< br > < input  type ="submit"> < /form > < /body > < /html > Run Example » When the user fills out the form above and clicks the submit button, the form data is sent for processing to a PHP file named "welcome.php". The form data is sent with the HTTP POST method. To display the submitted data you could simply echo all the variables. The "welcome.php" looks like this: <html> <body> Welcome <?php echo $_POST["name"]; ?><br> Your email address is: <?php echo $_POST["email"]; ?> ...

Prepared Statements and Bound Parameters

  Prepared Statements and Bound Parameters A prepared statement is a feature used to execute the same (or similar) SQL statements repeatedly with high efficiency. Prepared statements basically work like this: Prepare: An SQL statement template is created and sent to the database. Certain values are left unspecified, called parameters (labeled "?"). Example: INSERT INTO MyGuests VALUES(?, ?, ?) The database parses, compiles, and performs query optimization on the SQL statement template, and stores the result without executing it Execute: At a later time, the application binds the values to the parameters, and the database executes the statement. The application may execute the statement as many times as it wants with different values Compared to executing SQL statements directly, prepared statements have three main advantages: Prepared statements reduce parsing time as the preparation on the query is done only once ...

PHP MySQL Use The ORDER BY Clause

PHP MySQL Use The ORDER BY Clause Select and Order Data From a MySQL Database The ORDER BY clause is used to sort the result-set in ascending or descending order. The ORDER BY clause sorts the records in ascending order by default. To sort the records in descending order, use the DESC keyword. SELECT column_name(s) FROM table_name ORDER BY column_name(s) ASC|DESC  To learn more about SQL, please visit our  SQL tutorial . Select and Order Data With MySQLi The following example selects the id, firstname and lastname columns from the MyGuests table. The records will be ordered by the lastname column: Example (MySQLi Object-oriented) <?php $servername =  "localhost" ; $username =  "username" ; $password =  "password" ; $dbname =  "myDB" ; // Create connection $conn =  new  mysqli($servername, $username, $password, $dbname); // Check connection if  ($conn->connect_error) {    die ( "Connection f...