The most viewed items
<?php
$server = 'localhost';
$user = 'root';
$password = '';
$conn = mysqli_connect($server, $user, $password);
if($conn){
$query = "DROP DATABASE `TNW`";
$query_run = mysqli_query($conn, $query);
if($query_run){
echo 'Successfully deleted a Database.';
}else{
echo 'Deleting a database Error.';
}
}else{
echo 'Connection Error';
}
?>
<?php
$server = 'localhost';
$user = 'root';
$password = '';
$conn = mysqli_connect($server, $user, $password);
if($conn){
$query = "CREATE DATABASE `TNW`";
$query_run = mysqli_query($conn, $query);
if($query_run){
echo 'Successfully created a Database.';
}else{
echo 'Creating a database Error.';
}
}else{
echo 'Connection Error';
}
?>
The following is the example of connecting to database server in php.
<?php
$server = 'localhost';
$user = 'root';
$password = '';
$conn = mysqli_connect($server, $user, $password);
if($conn){
echo 'Successfull connected to the server.';
}else{
echo 'Connection Error.';
}
?>
We can set the limitations in uploading files. For example, we allow to upload only jpg or jpeg images and the maximum file size is 800KB.
<?php
@$name = $_FILES['file']['name'];
@$type = $_FILES['file']['type'];
@$size = $_FILES['file']['size'];
@$extension = substr($name, strpos($name, '.')+1);
@$max_size = 800000;
@$tmp_name = $_FILES['file']['tmp_name'];
@$location = 'tnw87/';
if(isset($name) && !empty($name)){
if($extension == 'jpg' || $extension == 'jpeg'){
if($size < $max_size){
if(move_uploaded_file($tmp_name, $location.$name)){
echo 'Successfully uploaded!';
}else{
echo 'Upload Errors!';
}
}else{
echo 'The maximum file size is 800KB';
}
}else{
echo 'Only jpg or jpeg image is allowed!';
}
}else{
echo 'Please choose a file to upload';
}
?>
<form action = "upload.php" method = "POST" enctype = "multipart/form-data">
<input type = "file" name="file"><br /><br />
<input type = "submit" value = "Submit">
</form>
In this example, we will upload file to the designated location. So, we will create a folder to store the files we've uploaded and give name to that folder. I give it as tnw87. Ok, lets start write the following codes and discover how it works.
<?php
$name = $_FILES['file']['name'];
$type = $_FILES['file']['type'];
$size = $_FILES['file']['size'];
$tmp_name = $_FILES['file']['tmp_name'];
if(@isset($name) && @!empty($name)){
$location = 'tnw87/';
if(move_uploaded_file($tmp_name, $location.$name)){
echo $name. ' is successfully uploaded.';
}else{
echo 'Upload Errors';
}
}else{
echo 'Please select file to upload';
}
?>
<style type = "text/css">
form{margin-top : 100px; margin-left : 50px;}
</style>
<form action = "upload.php" method = "POST" enctype = "multipart/form-data">
<input type = "file" name = "file"></br></br>
<input type = "submit" value = "Upload">
</form>
When we upload a file into one location, the file is firstly saved in temporary location as a temp file. Ok, lets discover how it works. We will write the following codes and test it.
<?php
$name = $_FILES['file']['name'];
$type = $_FILES['file']['type'];
$size = $_FILES['file']['size'];
$tmp_name = $_FILES['file']['tmp_name'];
if(@isset($name) && @!empty($name)){
$location = 'tnw87/';
if(move_uploaded_file($tmp_name, $location.$name)){
echo $name. ' is successfully uploaded.';
}else{
echo 'Upload Errors';
}
}else{
echo 'Please select file to upload';
}
?>
<style type = "text/css">
form{margin-top : 100px; margin-left : 50px;}
</style>
<form action = "upload.php" method = "POST" enctype = "multipart/form-data">
<input type = "file" name = "file"></br></br>
<input type = "submit" value = "Upload">
</form>
<?php
@$name = $_FILES['file']['name'];
@$type = $_FILES['file']['type'];
@$size = $_FILES['file']['size'];
if(@isset($name) && @!empty($name)){
echo 'File name is <strong> '.$name.'</strong>, File type is <strong>'.$type.'</strong> nad File size is <strong> '.$size.'</strong>';
}else{
echo 'Please select file to upload';
}
?>
<style type = "text/css">
form{margin-top : 100px; margin-left : 50px;}
</style>
<form action = "upload.php" method = "POST" enctype = "multipart/form-data">
<input type = "file" name = "file"></br></br>
<input type = "submit" value = "Upload">
</form>
<?php
$time = time();
$time_now = date('D M Y & H:i:s', $time);
$time_modified = date ('D M Y @ H:i:s', strtotime('+ 1 year') );
echo 'The current time is '.$time_now.'.</br>The time modified is '.$time_modified.'.' ;
?>
<?php
$redirect_page = 'https://www.tnw87.com';
$redirect = true;
if($redirect == true){
header('location:' .$redirect_page);
}
?>
<?php
$offset = 0;
if(isset($_POST['text']) && isset($_POST['search']) && isset($_POST['replace'])){
$text = $_POST['text'];
$search = $_POST['search'];
$replace = $_POST['replace'];
$search_length = strlen($search);
if(!empty($text) && !empty($search) && !empty($replace)){
while($strpos = strpos($text, $search, $offset)){
$offset = $strpos + $search_length;
$text = substr_replace($text, $replace, $strpos, $search_length);
echo $text;
}
}else{
echo 'Please fill all fields!';
}
}
?>
<hr />
<form action="searchandreplace.php" method="POST">
<textarea name="text" rows="15" cols="60"></textarea><br /><br />
Search : </br>
<input type="text" name="search"><br /><br />
Replace : </br>
<input type="text" name="replace"><br /><br />
<input type="submit" value="Submit">
</form>
<?php
$string = 'Hi, this is the php testing!';
$string1 = 'Hi, this is string testing!';
similar_text($string, $string1, $result);
echo $result;
?>
From the following example, you can learn the usage of form, textarea, isset, strtolower and str_replace. Every word 'love' you type in the text box, the codes will change with the word 'hate'.
<?php
$find = 'love';
$replace = 'hate';
if(isset($_POST['user_input']) && !empty($_POST['user_input'])){
$user_input = strtolower($_POST['user_input']);
$user_input_new = str_replace($find, $replace, $user_input);
echo $user_input_new.'</br>';
}else{
echo 'Please fill in the text box!';
}
?>
<form action = "wordcensoring.php" method = "POST">
<textarea name = "user_input" cols = "40" widths = "500"></textarea></br></br>
<input type = "submit" value = "Submit">
</form>