0

I have created register form using php. I have index.php,submit.php,functions.php and script.

Now it works fine, and i need to retrieve the data from database and display in front end.

I am new to php, and i am learning. please help me to do this.

This is my data.php:

    <?php 
    include ('condb.php');
    $query = mysql_query("select * from test where id='".$_GET['id']."'");
    $row = mysql_fetch_array($query);
?>
    <body>
        <h3>Employee Detail</h3>
        <p>Emp Name:</p> <?php echo $row['fname'];?>
        <p>LastName:</p> <?php echo $row['lname'];?>
    </body>
<?php ?>

and this is action.php:

           <?php
include('condb.php');

extract($_POST);
$que=mysql_query("INSERT INTO test (id, fname, lname) VALUES ('$id', '$fname', '$lname')") or die("Error Msg!");
if($que)
{
    header('Location: data.php?id='.$id.'');
}

?>

index.html:

<body bgcolor="#2E2E2E">
        <table align="center" cellpadding="5" style="background:#A4A4A4; border-radius:4px; margin-top:40px; padding:15px;">
            <h1 style="text-align:center; color:white; margin-top:50px">Employee Registration Form</h1>
            <form method="post" action="action.php">
                <tr><td>First Name:</td><td> <input type="text" id="fname" name="fname" required /></td></tr>
                <tr><td>Last Name:</td><td> <input type="text" id="lname" name="lname"/></td></tr>
                <tr><td></td><td><input type="submit" value="Register Me!"/></td></tr>
            </form>
        </table>
    </body>

and condb.php:

<?php 
$con=mysql_connect("localhost","root","")or die("Error!");
mysql_select_db("test") or die(mysql_error());
?>
6
  • which data, you want to display Commented Oct 8, 2014 at 10:44
  • all entered input details. Commented Oct 8, 2014 at 10:45
  • after successfull input Commented Oct 8, 2014 at 10:46
  • check this link php.net/manual/en/function.mysql-fetch-array.php Commented Oct 8, 2014 at 10:47
  • You should read some tutorials on PHP/MySQL. There are a plethora of books out there. ANd don't use MySQL Functions (mysql_*), they're deprecated as of PHP 5.5.0. Instead use mysqli class or functions. Commented Oct 8, 2014 at 10:48

4 Answers 4

2

Here I have shown code for the 3 field and you can do similarly for other fields.You can make newfile and paste that code their.

<?php
    $dbhost = 'localhost';
    $dbuser = 'root';
    $dbpass = '';
    $conn = mysql_connect($dbhost, $dbuser, $dbpass);
    if(! $conn )
    {
      die('Could not connect: ' . mysql_error());
    }
    $sql = 'SELECT fname, lname, email FROM crop';

    mysql_select_db('crop');
    $retval = mysql_query( $sql, $conn );
    if(! $retval )
    {
      die('Could not get data: ' . mysql_error());
    }
    while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
    {
        echo "First Name :{$row['fname']}  <br> ".
             "Last Name : {$row['lname']} <br> ".
             "Email Address : {$row['email']} <br> ".
             "--------------------------------<br>";
    } 
    echo "Fetched data successfully\n";
    mysql_close($conn);
    ?>
Sign up to request clarification or add additional context in comments.

24 Comments

this is what i have already seen buddy, i just where to i add to my existing code..
You have done for insert where as my code is for retrieval. What exactly do you want ? Be clear ..
Just make one file data.php in the same folder and do some CSS.
In the same folder that you have included all that above mentioned file.
Paste that code in submit.php after query if($result){header('Location: data.php');} So that it will directly redirect you to page where all the user are listed . NOTE:Make sure that path to data.php is correct.
|
1

You have to use the select statement to retrieve data from the database. After you select the data from the database, you have to fetch the data to make it usefull.

For example:

$queryResult = mysql_query("SELECT * FROM crop");
$result = mysql_fetch_assoc($queryResult);

When you are going to print the result , you are going to see all the selected data from the database

print_r($result);

1 Comment

you can put this code into the file where you are going to show the data from the database.
1
 <?php
       include 'config.php';
        error_reporting(E_ERROR);
        session_start();
        $fname=$_POST['fname'];
        $lname=$_POST['lname'];
        $email=$_POST['email'];
        $pass=$_POST['pass'];
        $phone=$_POST['phone'];
        $sex_select=$_POST['sex_select'];
        $month=$_POST['month'];
        $day=$_POST['day'];
        $year=$_POST['year'];

        $result = mysql_query("INSERT INTO crop(fname, lname, email, pass, phone,`sex_select`, month,day,year)  
    VALUES ('$fname', '$lname', '$email', '$pass','$phone','$sex_select', '$month','$day','$year')");
    if(mysql_affected_rows()>1)
        {

        //it will redeirect you to. your any file. 
        //you can give any file name here. 
           $_SESSION['fname']=$fname;//you are adding value to the session
         $_SESSION['lanme']=$lname; //you are adding value to the session
    //Do this for rest of your value
    header('Location:Your_File_name.php');
        }



    if (!$result) {
        die(msg(0,"wrong query"));
    }
        ?>

In the file where you are Redirecting too. You dont have to redirect. if You dont want to redirect dont redirect. but understand the concept you can acess these $_SESSION any where after start_start(); at any file

    session_start();

//now you have access to all those value. which you stored in $_SESSION
echo $_SESSION['fname'];
echo $_SESSION['lanme'];
//In short which ever value you store in $_SESSSION you can echo them 
//out on any page.

In That file

<?php

//lets say there is one more file you want to do 
//that 

//do the same thing 

 session_start();

 echo $_SESSION['fname'];
echo $_SESSION['lanme'];

3 Comments

within a minutes, i have to move dr, can you help me as soon as possible?
what is this, header('Location:Your_File_name.php');
i can't understand dr, can you send zip?
-1

Markkh answered you but also make sure that you escape the input from $_POST otherwise you'll have SQL Injection problems. php.net/mysql_real_escape_string

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.