PHP webservices advance level

Hi Guys,

It is our advance and final level of php webservices in this session we will learn about

  1. Form Introduction
  2. Connectivity with php admin using php.
  3. Insertion operation with php
  4. Selection Opertaion with php
  5. Insert Operation with form action using php
  6. Display data on page

So, now we are starting without wasting our time

Form Introduction

What is the Form?

A Document that containing black fields, that the user can fill the data or user can select the data. Casually the data will store in the data base.

Example:

Below example shows the form with some specific actions by using post method. In this example I am adding two numbers taken from user from Input box and after clicking on submit button it will be displayed into paragraph tag.


<html>
 <head> </head> 
<body> 
<h2>Adding two numbers Using Form Action </h2> 

<form method="post"> 

<label>Enter Your first number :: </label> 
<input type="number" name="num1" /> 

<label>Enter Your Second number ::</label> 

<input type="number" name="num2" /><br> 

<input type="submit" name="submit" value="add two numbers" /> 

</form>

 <?php if(isset($_POST['submit'])){

 $number1=$_POST['num1']; 

$number2=$_POST['num2']; 

$sum=$number1+$number2; 

echo "<h2>Number after Addition" .$sum. "</h2>"; } 

?> 

</body>

</html>
        

Assignment 

# Enter Your First name inside input text enter your last name and print inside paragraph.

Connectivity with php admin using php

In php database connectivity is very simple go through these few lines of code


<?php 

$servername = "localhost";

$username = "root"; //your user name for php my admin if in local most probaly it will be "root"

$password = "root"; //password probably it will be empty or root 

$databasename = "soha_org"; //Your db nane // Create connection 

$conn = new mysqli($servername, $username, 

$password,$databasename); // Check connection 

if ($conn->connect_error) { 

die("Connection failed: " . $conn->connect_error); } 

echo "Connected successfully"; 

?>
        

Note : object operator, -> , is used in object scope to access methods and properties of an object. It’s meaning is to say that what is on the right of the operator is a member of the object instantiated into the variable on the left side of the operator.

Insertion operation with php

Now, we have learnt connectivity with phpadmin now we learnt about insertion Operation using php.

 


<?php

$servername="localhost";//servername here it is localhost 

$username="root"; //username 

$password="root"; //password

$dbane="soha_org"; //databasename //

Create a connection $conn=new mysqli($servername,$username,$password,$dbane); 

if($conn -> connect_error)

{ die("connection failed ".$conn ->connect_error); } 

//Insert query 

$sql ="Insert into student_info(name,email,gender,contact_num,additional,comment) values('abc','abc@gmail.com','Male','12345','nothing','something')";

if ($conn->query($sql) === TRUE) { 

echo "New record created successfully"; }

else{ echo "Something going wrong"; }

 ?>
        

After running this code just go to your php admin panel and you will find that yeah your data has been inserted inside your database table.



<?php //Here we are dealing with selection operation using php 

//At first we establish a database connection 

$servername="localhost";//servername

$username="root";//usernaame

$password="root"; //password 

$dbname="soha_org";//database name 

//Establish connection 

$conn = new mysqli($servername,$username,$password,$dbname); 

if($conn ->connect_error){ 

echo "Something is going wrong"; } 

//Select query to fetch your data

$sql="Select * From student_info"; 

$result =$conn->query($sql); 

if($result->num_rows > 0){ 

while($row=$result -> fetch_assoc()){ 

//Show your data taken from database table 

echo "id: " . $row["name"]. " - Email: " . $row["email"]. " " . "-Gender :" .$row["gender"]." " . "-contact number :: " .$row["contact_num"]. " " . "-Additional ::" .$row["additional"]. " " ."-comment :: " .$row["comment"]. "<br>"; } }else{ echo "0 results"; 

}

$conn->close(); 

?>