Friday 23 August 2013

Query String In Php Mysql Example Step By Step

Query String In Php Mysql Example Step By Step



In php Passing variables value with a url is very nice thing for  programmers do that .We can access value of  method of passing variables as GET, the other method by  POST. It is one of those things which can be easily done in php. For example. You are to query a database and for that you need to send three variables via GET – city, id, paid The common way to pass them via GET is to construct a query string as below: query string in php mysql example.



display.php



-----------------------------------------------------
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Untitled 1</title>
</head>
<body>
<?php
mysql_connect("localhost","root","");
mysql_select_db("test");
$sql=mysql_query("select * from product");
?>

<table border="1">
<tr>
<td><b>Name</b></td>
<td><b>Price</b></td>
<td><b>Image</b></td>

</tr>
<?php
while($row=mysql_fetch_array($sql))
{
?>
<tr>
    <td><?php echo $row[1]; ?></td>
    <td><?php echo $row[2]; ?></td>
    <td><a href="cat.php?id=<?php echo $row['category']; ?>">Detail</a></td>
    <td><img src="<?php echo $row[3]; ?>" height="50px" width="50px"></td>
   
</tr>
<?php
}
?>

</table>
</body>

</html>
------------------------------------------------------


cat.php


<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Untitled 1</title>
</head>
<body>

<?php
mysql_connect("localhost","root","");
mysql_select_db("test");
$sql=mysql_query("select * from product where category='".$_GET['id']."'");
?>

<table border="1">
<tr>
<td><b>Name</b></td>
<td><b>Price</b></td>
<td><b>Image</b></td>

</tr>
<?php
while($row=mysql_fetch_array($sql))
{
?>
<tr>
    <td><?php echo $row[1]; ?></td>
    <td><?php echo $row[2]; ?></td>
    <td><img src="<?php echo $row[3]; ?>" height="50px" width="50px"></td>
  
</tr>
<?php
}
?>

</table>
</body>

</html>
----------------------------------------------------------------------------------------

Database
















above example is related to how to build query string in php mysql example



Friday 16 August 2013

Step By Step Insert Record Using Php Mysql


Step By Step Insert Record Using Php Mysql

we can insert record using php, mysql. mysql is used to store the data and php is used to send the query to mysql database. this below example give the full details about how to insert record in mysql using php.



registration.php


<?php

echo $pass=rand();
$epass=md5($pass);
$con=mysql_connect("localhost","root","");
mysql_select_db('project',$con);

$sql="insert into registration(fname,lname,emailid,password,status,role)values('".$_POST['fnm']."','".$_POST['lnm']."','".$_POST['emailid']."','$epass','a','u')";
mysql_query($sql);

?>

<body>
<form method="post">
<table align="center" border="1">

<tr>
    <td>First Name</td>
    <td><input type="text" name="fnm" />
</tr>


<tr>
    <td>Last Name</td>
    <td><input type="text" name="lnm" />
</tr>

<tr>
    <td>EmailId</td>
    <td><input type="text" name="emailid" />
</tr>




<tr>
    <td><input type="submit" name="submit" value="Submit" /></td>
    <td><input type="reset" name="reset" /></td>
</tr>

</table>

</form>
</body>
</html>

database


















***********************************************************************
New Example




















Insert.php


<html>
<head>
<title>Untitled Document</title>
</head>

<?php
$con=mysql_connect("localhost","root","");
mysql_select_db("demo");
echo $hb=$_POST['txtchk1']." ".$_POST['txtchk2']." ".$_POST['txtchk3'];

$sql="insert into stud(name,city,pincode,gender,hobby,branch,address)
values('".$_POST['fname']."','".$_POST['txtcity']."',".$_POST['pincode'].",'".$_POST['txtgen']."','".$hb."','".$_POST['txtbranch']."','".$_POST['txtadd']."')";
mysql_query($sql);
?>


<body>
<form method="post">
<table align="center" border="1">
    <tr>
   
        <td>First Name:</td>
        <td><input type="text" name="fname"></td>
    </tr>

<tr>

    <td>City:</td>
    <td><input type="text" name="txtcity"></td>

</tr>


<tr>
    <td>Pincode:</td>
    <td><input type="text" name="pincode"></td>

</tr>

<tr>

    <td>Gender:</td>
    <td>F:<input type="radio" name="txtgen" value="F">
   
        M:<input type="radio" name="txtgen" value="M">
    </td>

</tr>

<tr>
    <td>Hobby:</td>
    <td>
        Read:<input type="checkbox" name="txtchk1" value="Read">
        Playing:<input type="checkbox" name="txtchk2" value="Playing">
        Other:<input type="checkbox" name="txtchk3" value="Other">
    </td>

</tr>

<tr>
    <td>Branch:</td>
    <td>
        <select name="txtbranch">
        <option>BCA</option>
        <option>BIT</option>
        <option>PGDCA</option>
           
        </select>
   
    </td>

</tr>

<tr>
    <td>Address:</td>
    <td><textarea name="txtadd"></textarea> </td>

</tr>
<tr>
   
    <td><input type="submit" name="submit" value="Submit"></td>
    <td><input type="reset" name="reset" value="Cancle"></td>

</tr>
</table>

</form>
</body>
</html>







Friday 2 August 2013

Step By Step Login Example In Php Mysql Using Session And Logout

Step By Step Login Example In Php Mysql Using Session And Logout


In this post we have create login page using php and mysql , we have used mysql_num_rows() for count the match rows. with this example i have also explain the database. if rows values is 0 then login is not valid and if rows value is 1 then user is valid. how to implement login and logout code in our project. below example explain how to do login and logout using php script.login example in php mysql using session and logout
 --------------------------------------------------------------------------------

database
--------------------------------------------------

login.php
------------------------------------------------

<?php
session_start();

if(isset($_GET['msg']))
{

    echo $_GET['msg'];
}


if(isset($_POST["submit"]))
{
$con=mysql_connect("localhost","root","");
mysql_select_db("project");

$sql="select * from registration where emailid='".$_POST['txtemail']."' and password='".md5($_POST['txtpass'])."' and status='a'";
$qsql=mysql_query($sql);

$nsql=mysql_num_rows($qsql);

if($nsql>=1)
{
   echo $_SESSION['user']=$_POST['txtemail'];
   ?>
   <script language="javascript">
   window.location="logout.php";
   </script>
   <?php
}

if($nsql <=0)
{
    echo "Not Valid user";
}
}

?>
<html>
<head>
</head>
<body>
<form method="post">
<table border="1">
<tr>
        <td>EmailId</td>
        <td><input type="text" name="txtemail" /></td>
</tr>

<tr>
        <td>Password</td>
        <td><input type="password" name="txtpass" /></td>
</tr>

<tr>
    <td><input type="submit" name="submit" value="Login" /></td>
    <td><input type="reset" name="reset" value="Cancle" /></td>
</tr>

</table>

</form>

</body>
</html>

logout.php

-------------------------------------------------

<?php
session_start();


if(isset($_SESSION['user']))
{
    unset($_SESSION['user']);
    $_SESSION['user']="";
?>
<script language="javascript">
//window.location="login.php?msg=You have logout successfully";
</script>
<?php   
}

?>

<a href="login.php?msg=You have logout successfully">logout</a>