Tuesday, 23 April 2013

How to Add New State into the database of PHP + MySQL ???

For Add new state countries must fill from the database table of the country.
To add state information according to the selected country using PHP and MySQL, here's the process in simple terms:

Database Setup:

Create two tables: one for countries and one for states.
The countries table holds the list of countries.
The states table stores the states along with a reference (foreign key) to the country they belong to.
Insert Data:

Add some sample countries like "USA", "India", and "Canada" in the countries table.
Add related states like "California" and "New York" for the USA, and "Maharashtra" and "Gujarat" for India in the states table, with a link to the respective country.
User Interface:

Display a country dropdown in an HTML form.
When the user selects a country from this dropdown, an AJAX request is sent to the server to retrieve the corresponding states for that country.
Server-Side Processing:

On the server-side (PHP), receive the selected country ID from the AJAX request.
Query the states table in the database to fetch the states related to the chosen country.
Send back the list of states as options to be displayed in a second dropdown (the states dropdown).
User Experience:

The user first selects a country, and the states dropdown automatically updates with the relevant states.
This is done without needing to refresh the page, creating a smooth and dynamic experience.
In summary, the process involves linking states to countries in the database, dynamically fetching states based on the selected country using PHP, and updating the UI with the state data via AJAX.


Here is one example of it


<html>
<head>

<title>Title of Page</title>
<script language="javascript" type="text/javascript">
function showState(countryid)
{
 document.form1.submit();
}
</script>
<?php
include('myconnection.php');
if(isset($_POST['submit']))
{
$sname=trim($_POST['statename']); 
$cid=trim($_POST['countryid']); 
if(empty($sname))
{
echo "<center><p style=\"color:Red\" >Please Enter State name</p><center>";
}
else
{ $sql="insert into tblstate(`id`,`countryid`,`statename`) values(NULL,'$cid','$sname');";
if(mysqli_query($con,$sql))
{
echo "<center><p style=\"background-color:#000;width:450px;color:#fdd000\" >Statename is successfully inserted !!!</p><center>";
}
else
{
echo "error".mysqli_error();
}
}
}
?>
</head>
<body>
<form id="form1" name="form1" method="POST"  action="">
<center><fieldset style="width:450px">
<legend ><h1 style="background-color:#000fdd;color:#ffff00">Add New State</h1></legend>
<table>
<tr><td colspan="2"></td></tr>
<tr>
<td>Select Country</td>
<td>
<?php
$query = "SELECT * FROM tblcountry";
$result = mysqli_query($con,$query);
?>
<select name="countryid" id="countryid"  onChange="showState(this.value);">
<option value="">--- Select Country---</option>

<?php
while ($line = mysqli_fetch_array($result)) {
?>

<option value="<?php echo $line['id'];?>" > <?php echo $line['countryname'];?> </option>

<?php
}
?>
</td></tr>
<tr><td>StateName</td><td><input type="text" name="statename" value="" /></td></tr>
<tr><td></td><td><input type="submit" name="submit" value="submit"><input type="reset" name="reset" value="reset">
</td>
</tr>
</table>
</fieldset>

</center>
</form>
</body>

</html>

Create foreign key in mysql

How to create Foreign Key in MySQL

create table tblstate
(
id int primary key auto_increment not null,
countryid int ,
statename varchar(20),
foreign key(countryid) references tblcountry(id)
)


OR


ALTER TABLE tblstate ADD CONSTRAINT countryid_ref FOREIGN KEY (countryid) REFERENCES tblcountry (id);

Monday, 22 April 2013

How to insert records in table using PHP + MySQL ???

HTML and PHP code to insert new record 

HTML page having radio button list and checkbox list

Copy and paste this code into the html body part


<form id="form1" name="form1" method="POST" action="">
<table>
<tr><td colspan="2"><h1>REGISTRATION FORM</h1></td></tr>
<tr><td>Username</td><td><input type="text" name="username" value="" /></td></tr>
<tr><td>password</td><td><input type="text" name="password" value="" /></td></tr>
<tr><td>confirm password</td><td><input type="text" name="repassword" value="" /></td></tr>
<tr><td>Hobbies</td><td><input type="checkbox" name="hobbies[]" value="cricket" />Cricket<input type="checkbox" name="hobbies[]" value="football" />FootBall<input type="checkbox" name="hobbies[]" value="carrom" />Carrom</td></tr>
<tr><td>Gender</td><td><input type="radio" name="gender[]" value="male" />MALE<input type="radio" name="gender[]" value="FEMALE" />FEMALE</td></tr>
<tr><td>Emailid</td><td><input type="text" name="emailid" value="" /></td></tr>
<tr><td>Mobileno</td><td><input type="text" name="mobileno" value="" /></td></tr>
<tr><td></td><td><input type="submit" name="submit" value="submit"><input type="reset" name="reset" value="reset">
</td>
</tr>
</table>
</form>


php code 


<?php
if(isset($_POST['submit']))
{$s="";
 $g="";
$user=trim($_POST['username']); 
$pass=trim($_POST['password']);
$email=trim($_POST['emailid']);
$mobile=trim($_POST['mobileno']);

if(!isset($_POST['hobbies'])) 
{
echo("<p>please select your hobbies</p>\n");

else 

$ho = $_POST['hobbies'];
for($i=0; $i<sizeof($ho); $i++)
{
$s .= $ho[$i];
if (($i+1) < sizeof($ho))
$s .= ",";
}
echo "your hobbies are : "$s;echo("</p>");


if(!isset($_POST['gender'])) 
 {
echo("<p>please select your gender</p>\n");
  } 
else 
 { 
$ho = $_POST['gender'];
for($i=0; $i<sizeof($ho); $i++)
{
$g .= $ho[$i];
}
         echo "You are ".$g;echo("</p>");



$con=mysqli_connect(@"localhost","root","","phpdemo") or die("Unable to connect to the database of Mysql name :  phpdemo");


if(mysqli_query($con,"create table tblreg(id int primary key auto_increment not null,username varchar(20),password varchar(20),hobbies varchar(40),gender varchar(6),emailid varchar(30),mobileno decimal(10,0))"))
{
echo "<br>Table created successfully.<br>";
}

$sql="insert into tblreg(`id`,`username`,`password`,`hobbies`,`gender`,`emailid`,`mobileno`) values(NULL,'$user','$pass','$s','$g','$email','$mobile');";
if(mysqli_query($con,$sql))
{
echo "successfully inserted into  the registration table ";
}
else
{
   echo "error".mysqli_error();
}


Fill country state city using php mysql

For selecting country in drop down list
The states are also filled auto matically from the database table as shown in the above figure.

<html>
<head>
<title>Fill Dropdown list</title>
<script language="javascript" type="text/javascript">
function showState(Country_Id)
{
 document.frm.submit();
}

</script>
</head>
<body>
<form name="frm" id="frm">

<?php
$con=mysqli_connect(@"localhost", "root", "","demo") or die("Connection Failed");
$query = "SELECT * FROM tbl_country";
$result = mysqli_query($con,$query);
?>
<select name="select1"  onChange="showState(this.value);">
<option value="">--- Select Country---</option>
<?php
while ($line = mysqli_fetch_array($result)) {
?>
<option value="<?php echo $line['id'];?>" <?php if($line['id']==$_REQUEST["select1"]) { echo

"Selected"; } ?> > <?php echo $line['countryname'];?> </option>
<?php
}
?>
<?php
$con=mysqli_connect(@"localhost", "root", "","demo") or die("Connection Failed");
$query = "SELECT * FROM tbl_state where cid='$_REQUEST[select1]'";
$result = mysqli_query($con,$query);
?>
</select>
<select name="select2"  >
<option value="">--- Select State---</option>
<?php
while ($line = mysqli_fetch_array($result)) {
?>
<option value="<?php echo $line['sid'];?>"> <?php echo $line['statename'];?> </option>
<?php
}
?>
</select>
</form>
</body>

</html>

For Learn Project Training with PHP Technologies
Visit Website : PHP Project Training Institute Vadodara, Gujarat, India.

Friday, 19 April 2013

Login form in PHP + MySQL

Create login.php and home.php

If valid username and password is entered by the user then he/she will access his/her HomePage.

login.php

where code and html document is there

<html>
<head>
<title>Login form</title>
<?php
if(isset($_POST['submit']))
{
$con=mysqli_connect(@"localhost","root","","demo");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
$un=$_POST['username'];
$ps=$_POST['password'];

$result = mysqli_query($con,"SELECT * FROM tbladmin WHERE username='$un' and password='$ps'");
$row = mysqli_fetch_array($result);
$name = $row["username"];

$count=mysqli_num_rows($result);
  if($count==1)  {
 session_start();
 $_SESSION["user"]=$name;
 header("location:home.php");

  }
  else
  {
  echo "<div style=\"color:red\"><center>Invalid Username/Password</center></div>";
  }

mysqli_close($con);
}
?>
</head>

<body>
<form action="" method="post">
<center>
<h1>Admin LOGIN Form</h1>
<table >
<tr>
<td>
username:
</td>
<td>
 <input type="text" name="username">
</td>
</tr>
<tr>
<td>
password:
</td>
<td>
 <input type="password" name="password">
</td>
</tr>
<tr>
<td>
</td>
<td>
<input type="submit" name="submit" value="submit">
<input type="reset" name="reset" value="reset">
</td>
</tr>
</table>
</center>
</form>

</body>
</html>



home.php

Having the code to check session is started or Not.

<?php
session_start();
if(isset($_SESSION['user']))
{ $name=$_SESSION['user'];
 echo " welcome,$name";
}
else
{
 header("location:login.php");
}

?>

Thursday, 18 April 2013

Show Records using PHP + MySQL database connectivity in the website


PHPleqrn  Show Data of Database into the Tabular Format :

The Below Code will show the Columns
1.Username
2. Fullname 
3. Mobileno 
into the tabular format.


<?php
Echo "Show record of tbladmin";
$result = mysqli_query($con,"SELECT * FROM tbladmin");
echo "<table border=1>";
echo"<tr>
<th>Username</th>
<th>Fullname</th>
<th>Mobileno</th>
</tr>";
while($row = mysqli_fetch_array($result))
  {
  echo "<tr><td>".$row['username'] ."</td> <td> " . $row['fullname']." </td><td> ".$row['mobileno']."</td></tr>";

  }
 echo "</table>";
?>

For learning Project Training in Vadodara, Gujarat. Visit  website: www.vataliyatuitionclasses.com

Create New User Registration using PHP + MySQL Database connectivity


Write Insert Query to perform insert operation.

Direct Record fill by user itself <?php

// Create connection
$con=mysqli_connect(@"localhost","root","","Database1");


//Insert into table data
mysqli_query($con,"INSERT INTO tbladmin VALUES ('hitesh','hitesh','kumar','hiteshvataliya1@gmail.com','12391239123')");



?> 

By using Form user defined insert page


<html>
<head><title>Insert into Database</title>
<?php
if(isset($_POST['Submit'])){
$username=trim($_POST['username']);
$password=trim($_POST['password']);
$name=trim($_POST['name']);
$number=trim($_POST['number']);
$email=trim($_POST['email']);

// Create connection
$con=mysqli_connect(@"localhost","root","","Database1");


//Insert into table data
$sql="INSERT INTO `database1`.`tbladmin` (`username`, `password`, `fullname`, `emailid`, `mobileno`) VALUES ('$username', '$password', '$name', '$email', '$number');";

if (mysqli_query($con,$sql))
{
echo "Success";
//final code will execute here.
}
}

?> 

</head>
<body>
<form name= "info" id= "info" method= "post" action= "" >
<table width= "327" border= "0" align="center" cellpadding= "5" cellspacing= "1">

<tr>
<td>Username: </td>
<td><input name= "username" type= "text" id= "username" value="" ></td>
</tr>
<tr>
<td>Password: </td>
<td><input name= "password" type= "text" id= "password" value=""  ></td>
</tr>
<tr>
<td width= "82" >FullName: </td>
<td width= "238" ><input name= "name" type= "text" id="name" value="" ></td>
</tr>
<tr>
<td> Email: </td>
<td><input name= "email" type= "text" id= "email" value="" ></td>
</tr>
<tr>
<td>Number: </td>
<td><input name= "number" type= "text" id= "number" value="" ></td>
</tr>
<tr>
<td></td>
<td><input type= "submit" name= "Submit" value= "Submit" /></td>
</tr>
</table>
</form>
</body>
</html>

For learning PHP project - visit website www.vataliyatuitionclasses.com


Create New Table using Mysql And PHP ???


Create table for admin registration having fields

id
username
password
fullname
emailid
mobileno

<?php

// Create connection
$con=mysqli_connect(@"localhost","root","","Database1") or die("Unable to connect to the database of Mysql name :  Database1");

//CREATE NEW TABLE
$sql="CREATE TABLE tbladmin(
id INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY(id),
username VARCHAR(30),
password VARCHAR(30),
fullname VARCHAR(50),
emailid VARCHAR(50),
mobileno INT
)";

// Execute query
if (mysqli_query($con,$sql))
  {
  echo "Table tbladmin created successfully";
  }
else
  {
  echo "Error creating table: " . mysqli_error();
  }
?> 

How to connect PHP to MySQL ???


In web developement of php with mysql database it is must required to connect php form to the mysql database

$conn = mysqli_connect(“Server", “Username",“Password");



$conn is the varible in PHP.
mysqli_connect() :- is a method which is required to connect with the MySQL database
The attributes of the method are

Server – host name of server
Username - Username of Database
Password - Password of Database

Example :- 

if PC is works as a client and server then


$hostname='localhost';
$username='root';
$password='';
$conn = mysqli_connect($hostname,$username, $password) or die("Unable to connect to MySQL");
echo "<br>Now , Php for is connected to Mysql ";

              OR

$conn=mysqli_connect(@"localhost","root","“);


HTML and PHP code to Connect with MySQL using localhost and create new database using it.


<html>

<head>
<title>Demo</title>
</head>
<body>
Php form 
<?php
// Creating a connection to mysql

$hostname='localhost';
$username='root';
$password='';
$conn = mysqli_connect($hostname,$username, $password) 
  or die("Unable to connect to MySQL");
echo "<br>Now , Php for is connected to Mysql ";

$sql="CREATE DATABASE Database1";

if (mysqli_query($conn,$sql))
 {
 echo "<BR><BR>Database Database1 created successfully";
  }
else
  {
  echo "Error creating database: " . mysqli_error();
  }
?> 
</body>
</html>


What is PHP ???

PHP - Personnel Home Pages older name of php.

And Now it is Known as Hypertext PreProcessor.



PHP is the most widely used scripting language for web programming. 

PHP extends HTML pages by adding server-executed code segments to HTML pages.  

The output of the execution of the PHP code is merged into the HTML page.

PHP - Open Source Software. We don't have to pay any thing for using it. We can easily creates websites and web developement using PHP.

PHP - Which executes on the server.

The code is not shown in the view source of the page.

PHP is secure server side programming language.

PHP is easy to learn and use. we can easily embed it with HTML Page.

Example :-
<html>
<head><title>PHP with HTML</title>
</head>
<body>
<?php

//code of php


?>

</body>
</html>