<?php
include(“db.php”);
$id = $_REQUEST[‘id’];
$delete = “DELETE FROM employees where id=$id”;
if(mysqli_query($sql, $delete)){
echo “Record Deleted”;
}else{
echo “Getting error”;
}
?>
PHP
All posts in the PHP category
Update Table Record
Published November 17, 2017 by Anil Kumar Vishwakarma<?php
include(“db.php”);
$id = $_REQUEST[‘id’];
$first_name = $_REQUEST[‘first_name’];
$last_name = $_REQUEST[‘last_name’];
$f_name = $_REQUEST[‘f_name’];
$salary = $_REQUEST[‘salary’];
$update = “UPDATE employees set first_name=’$first_name’, last_name=’$last_name’, f_name=’$f_name’, salary=’$salary’ where id=$id”;
if(mysqli_query($sql, $update)){echo “Record updated”;}else{echo “Not updated”;}
?>
Edit Records from table
Published November 17, 2017 by Anil Kumar Vishwakarma<?php
include(“db.php”);
$id = $_REQUEST[‘id’];
$edit = “SELECT * FROM employees where id = $id”;
$employee = mysqli_query($sql, $edit);
?>
<!DOCTYPE html>
<html lang=”en”>
<head>
<title>Bootstrap Example</title>
<meta charset=”utf-8″>
<meta name=”viewport” content=”width=device-width, initial-scale=1″>
<link rel=”stylesheet” href=”https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css”>
https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js
https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js
</head>
<body>
Employee Updation Form
0){
$emp = mysqli_fetch_assoc($employee);?>
“>
” name=”first_name”>
” name=”last_name”>
” name=”f_name”>
” name=”salary”>
<button type=”submit” class=”btn btn-default” onclick=”return confirm(‘Are you sure you want to Update this entry?’)”>Update</button>
<?php } ?>
</form>
</div>
</body>
</html>
Displaying All Record from Table
Published November 17, 2017 by Anil Kumar Vishwakarma<?php
include(“db.php”);
$all_employees = “SELECT * FROM employees “;
$employee = mysqli_query($sql, $all_employees);
//print_r($employee);
?>
<!DOCTYPE html>
<html lang=”en”>
<head>
<title>All Employees Records</title>
<meta charset=”utf-8″>
<meta name=”viewport” content=”width=device-width, initial-scale=1″>
<link rel=”stylesheet” href=”https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css”>
https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js
https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js
</head>
<body>
All Employees Records
Employee ID | First Name | Last Name | Father Name | Salary | Edit | Delete |
---|---|---|---|---|---|---|
“>Edit | ” onclick=”return confirm(‘Do you realy want to delete this record?’)”>Delete |
</body>
</html>
Form for Entry of Employees
Published November 17, 2017 by Anil Kumar Vishwakarma<!DOCTYPE html>
<html lang=”en”>
<head>
<title>Bootstrap Example</title>
<meta charset=”utf-8″>
<meta name=”viewport” content=”width=device-width, initial-scale=1″>
<link rel=”stylesheet” href=”https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css”>
https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js
https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js
</head>
<body>
Employee Submission Form
<button type=”submit” class=”btn btn-default”>Submit</button>
</form>
</div>
</body>
</html>
Insert Data in Table
Published November 17, 2017 by Anil Kumar Vishwakarma<?php
include(“db.php”); //Database connection file name
$first_name = $_POST[“first_name”];
$last_name = $_POST[“last_name”];
$f_name = $_POST[“f_name”];
$salary = $_POST[“salary”];
$insert = “INSERT INTO employees(first_name, last_name, f_name, salary) values(‘$first_name’, ‘$last_name’, ‘$f_name’, ‘$salary’)”;
if(mysqli_query($sql, $insert)){
echo “Value Inserted”;
}else{
echo “value not inserted”;
}
?>
file_exists
Published June 12, 2017 by Anil Kumar Vishwakarmafile_exists — Checks whether a file or directory exists
Testing whether a file exists
<?php
$filename = '/path/to/foo.txt';
if (file_exists($filename)) {
echo “The file $filename exists”;
} else {
echo “The file $filename does not exist”;
}
?>
Laravel Setup
Published May 29, 2017 by Anil Kumar VishwakarmaServer Requirements
The Laravel framework has a few system requirements. Of course, all of these requirements are satisfied by the Laravel Homestead virtual machine, so it’s highly recommended that you use Homestead as your local Laravel development environment.
However, if you are not using Homestead, you will need to make sure your server meets the following requirements:
- PHP >= 5.6.4
- OpenSSL PHP Extension
- PDO PHP Extension
- Mbstring PHP Extension
- Tokenizer PHP Extension
- XML PHP Extension
Installing Laravel
Laravel utilizes Composer to manage its dependencies. So, before using Laravel, make sure you have Composer installed on your machine.
Via Laravel Installer
First, download the Laravel installer using Composer:
composer global require "laravel/installer"
Make sure to place the $HOME/.composer/vendor/bin
directory (or the equivalent directory for your OS) in your $PATH so the laravel
executable can be located by your system.
Once installed, the laravel new
command will create a fresh Laravel installation in the directory you specify. For instance, laravel new blog
will create a directory named blog
containing a fresh Laravel installation with all of Laravel’s dependencies already installed:
laravel new blog
Via Composer Create-Project
Alternatively, you may also install Laravel by issuing the Composer create-project
command in your terminal:
composer create-project --prefer-dist laravel/laravel blog
Local Development Server
If you have PHP installed locally and you would like to use PHP’s built-in development server to serve your application, you may use the serve
Artisan command. This command will start a development server at http://localhost:8000
:
php artisan serve
cURL Requests with PHP
Published February 6, 2017 by Anil Kumar VishwakarmaIntroduction
cURL allows transfer of data across a wide variety of protocols, and is a very powerful system. It’s widely used as a way to send data across websites, including things like API interaction and oAuth. cURL is unrestricted in what it can do, from the basic HTTP request, to the more complex FTP upload or interaction with an authentication enclosed HTTPS site. We’ll be looking at the simple difference between sending a GET
and POST
request and dealing with the returned response, as well as highlighting some useful parameters.
Basics
Before we can do anything with a cURL request, we need to first instantiate an instance of cURL – we can do this by calling the function curl_init();
, which returns a cURL resource. This function takes one parameter which is the URL that you want to send the request to, however, in our case, we’ll hold off doing that for now and set it an alternatively way later.
Settings
Once we’ve got a cURL resource, we can begin to assign some settings, below is a list of some of the core ones that I set
CURLOPT_RETURNTRANSFER
– Return the response as a string instead of outputting it to the screenCURLOPT_CONNECTTIMEOUT
– Number of seconds to spend attempting to connectCURLOPT_TIMEOUT
– Number of seconds to allow cURL to executeCURLOPT_USERAGENT
– Useragent string to use for requestCURLOPT_URL
– URL to send request toCURLOPT_POST
– Send request asPOST
CURLOPT_POSTFIELDS
– Array of data to POST in request
We can set a setting by using the curl_setopt()
method, which takes three parameters, the cURL resource, the setting and the value. So, to set the URL that we’re sending the request to as http://testcURL.com:
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'http://testcURL.com');
As mentioned, we can set the URL by sending a parameter through when getting the cURL resource:
$curl = curl_init('http://testcURL.com');
It is possible to set multiple settings at one time by passing through an array of settings and values to the function curl_setopt_array()
:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'http://testcURL.com'
));
Sending request
When all of the options are sent, and the request is ready to send, we can call the curl_exec()
method which will execute the cURL request. This function can return three different things:
false
– if there is an error executing the requesttrue
– if the request executed without error andCURLOPT_RETURNTRANSFER
is set tofalse
- The result – if the request executed without error and
CURLOPT_RETURNTRANSFER
is set totrue
Using the previous example, where we are wanting to get the result back, we would use the following:
$result = curl_exec($curl);
With $result
now containing the response from the page – which might be JSON, a string or a full blown site’s HTML.
Close Request
When you’ve sent a request and got the result back, you should look to close the cURL request so that you can free up some system resources, this is as simple as calling the curl_close()
method which as with all other functions takes the resource as its parameter.
GET Request
A GET
request is the default request method that is used, and is very straight forward to use, infact all of the examples so far have been GET
requests. If you want to send parameters along in the request you simply append them to the URL as a query string such as http://testcURL.com/?item1=value&item2=value2
.
So for example to send a GET
request to the above URL and return the result we would use:
// Get cURL resource
$curl = curl_init();
// Set some options - we are passing in a useragent too here
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'http://testcURL.com/?item1=value&item2=value2',
CURLOPT_USERAGENT => 'Codular Sample cURL Request'
));
// Send the request & save response to $resp
$resp = curl_exec($curl);
// Close request to clear up some resources
curl_close($curl);
POST Request
The sole difference between the POST
and GET
request syntax is the addition of one setting, two if you want to send some data. We’ll be setting CURLOPT_POST
to true
and sending an array of data through with the setting CURLOPT_POSTFIELDS
So for example switching the above GET
request to be a POST
request, we would use the following code:
// Get cURL resource
$curl = curl_init();
// Set some options - we are passing in a useragent too here
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'http://testcURL.com',
CURLOPT_USERAGENT => 'Codular Sample cURL Request',
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => array(
item1 => 'value',
item2 => 'value2'
)
));
// Send the request & save response to $resp
$resp = curl_exec($curl);
// Close request to clear up some resources
curl_close($curl);
There you have a POST
request that will work the same as our GET
request above and return the response back to the script so that you can use it as you want.
Errors
As much as we all hate errors, you really need to take care to account for any eventuality with cURL as ultimately you will not have control over the site(s) that you are sending your request to, you cannot guarantee that the response will be in the format that you want, or that the site will even be available.
There are a few functions that you can use to handle errors and these are:
curl_error()
– returns a string error message, will be blank''
if the request doesn’t fail.curl_errno()
– which will return the cURL error number which you can then look up on this page listing error codes.
An example usage would be:
if(!curl_exec($curl)){
die('Error: "' . curl_error($curl) . '" - Code: ' . curl_errno($curl));
}
You might want to look at using the setting CURLOPT_FAILONERROR
as true
if you want any HTTP response code greater than 400 to cause an error, instead of returning the page HTML.
curl_exec($theEnd);
cURL is a behemoth, and has many many possibilities. Some sites might only serve pages to some user agents, and when working with APIs, some might request you send a specfici user agent, this is something to be aware of. If you want to have a play with some cURL requests, why not have a go at playing with oAuth with Instagram.