hi i need to search a particular record where the user will enter the name or id .
this is my html form
<html>
<fieldset style=text-align:left>

 <form name="empdetails" id="empdetails" action="view.php">

 ID : <input type="text" id="id" name="id" ><br>
  <input type="submit" id="submit" name="view" value="view" ><br>
</form>

</body>

</html>

my php file :

<?php
error_reporting(E_ALL ^ E_DEPRECATED);
$connect=mysql_connect("localhost","root","")or die(mysql_error());
$db_select=mysql_select_db("employee",$connect) or die(mysql_error());

$id=isset($_POST['id']);
$qry="select * from emp where id='$id'";
$result=mysql_query($qry) or die(mysql_error()) ;
while($row=mysql_fetch_array($result))
{
echo "<div> ID ".$row['id'];
echo "<div> NAME" .$row['name'];
echo "<div>GENDER ".$row['gender'];
echo "<div> MOBILE" .$row['mobile'];
echo "<div>MAIL".$row['email'];

}
?>

Recommended Answers

All 7 Replies

So what seems to be the problem? :) Do you get any errors?

nothing is displaying

Well, have you checked the value of $id after the form has been submitted and if a record with that $id actually exists?

Checking your code, I notice that you use isset() where you shouldn't actually use it. I have made a couple of changes in your code. Hopefully you'll understand!

<html>
<fieldset style=text-align:left>
<form name="empdetails" id="empdetails" action="view.php">
ID : <input type="text" id="id" name="id" ><br>
<input type="submit" id="submit" name="view" value="view" ><br>
</form>
</body>
</html>
my php file :
<?php
error_reporting(E_ALL ^ E_DEPRECATED);
$connect = mysql_connect("localhost","root","") or die(mysql_error());
$db_select = mysql_select_db("employee",$connect) or die(mysql_error());

// You currently use isset() to set $id. isset() is a function that returns either true or false; not the actual value.
// To retrieve the value, you should omit isset(). Your line would become the following:
$id = $_POST['id'];

echo '<p>$id is now: ' . $id . '</p>';

// We should only execute the query if an id is actually given. Here, the isset() function may come in handy.
// We will also use the empty() function here, so that we do not only check if $id is set, but also if it has an actual readable value (in this case).
if(isset($id) && !empty($id)) {
    $qry = "select * from emp where id='$id'";
    $result = mysql_query($qry) or die(mysql_error());

    while($row=mysql_fetch_array($result))
    {
        echo "<div> ID ".$row['id'];
        echo "<div> NAME" .$row['name'];
        echo "<div>GENDER ".$row['gender'];
        echo "<div> MOBILE" .$row['mobile'];
        echo "<div>MAIL".$row['email'];
    }
}
else {
    echo '<p>Error: ID was nog given.</p>';
}
?>
im geting this error 
my php file : 
( ! ) Notice: Undefined index: id in G:\wamp\www\emp\view.php on line 16
Call Stack
#   Time    Memory  Function    Location
1   0.0003  247712  {main}( )   ..\view.php:0
$id is now:

Error: ID was nog given.
and plz explain me where we shoul use isset()

Well, maybe you should change it to this:

<html>
    <fieldset style=text-align:left>
        <form name="empdetails" id="empdetails" action="view.php">
            ID : <input type="text" id="id" name="id" ><br>
            <input type="submit" id="submit" name="view" value="view" ><br>
        </form>
    </body>
</html>

<?php
// Let's also turn off reporting notices.
error_reporting(E_ALL ^ E_DEPRECATED ^ E_NOTICE);

if($_POST) {
    $connect = mysql_connect("localhost","root","") or die(mysql_error());
    $db_select = mysql_select_db("employee",$connect) or die(mysql_error());

    echo '<pre><strong>$_POST:</strong>';
    print_r($_POST);
    echo '</pre>';

    // You currently use isset() to set $id. isset() is a function that returns either true or false; not the actual value.
    // To retrieve the value, you should omit isset(). Your line would become the following:
    $id = $_POST['id'];
    echo '<p>$id is now: ' . $id . '</p>';

    // We should only execute the query if an id is actually given. Here, the isset() function may come in handy.
    // We will also use the empty() function here, so that we do not only check if $id is set, but also if it has an actual readable value (in this case).
    if(isset($id) && !empty($id)) {
        $qry = "select * from emp where id='$id'";
        $result = mysql_query($qry) or die(mysql_error());

        while($row=mysql_fetch_array($result))
        {
            echo "<div> ID ".$row['id'];
            echo "<div> NAME" .$row['name'];
            echo "<div>GENDER ".$row['gender'];
            echo "<div> MOBILE" .$row['mobile'];
            echo "<div>MAIL".$row['email'];
        }
    }
    else {
        echo '<p>Error: ID was nog given.</p>';
    }
}
else {
    echo '<p>Error: no post data was submitted.</p>';
}
?>

isset() is used to check if a variable has been defined, while empty() checks if a value actually contains content.

Example:

<?php
$var = '';

var_dump(isset($var)); // Will return true.
var_dump(empty($var)); // Will also return true.

$var = false;

var_dump(isset($var)); // Will return true.
var_dump(empty($var)); // Will also return true.

$var = 'test';

var_dump(isset($var)); // Will return true.
var_dump(empty($var)); // Will return false.

im getting the result as no post data have been submitted

Well that means that something must be wrong with your form and/or PHP. Did you already try to find out what's in your $_POST var after having submitted the form? Because (obviously) the form must have been submitted in order for the PHP to be triggered.

Replace that echo '<p>Error: no post data was submitted.</p>'; line by:

echo '<p>Error: no post data was submitted.</p>';
echo '<pre><strong>$_POST:</strong>';
var_dump($_POST);

to see what's in $_POST.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.