0

I have three php pages the first page is to enter user details in the fields the second page is to insert user details in the database the third page is to show the user details that have been sent to the database,

everything work fine except in the third page the user details doest not appear and I get this error messages:

Notice: Undefined variable: user Notice: Undefined variable: email Notice: Undefined variable: pass

here is my code:

first_page.php

<form action="second_page.php" method="post" >
User Name:  
<input  type="text" name="username" >
User Email
<input  type="text" name="useremail" >
Password:  
<input  type="text" name="password" >
<input type="submit"  name="submit" >
</form>

second_page.php

if (isset($_POST['submit'])) 
{
$user= $_POST['username'];
$email = $_POST['useremail'];
$pass= $_POST['password']; 

mysql_query("INSERT INTO table (username, useremail, password) VALUES  ('$user','$emai','$pass');
header("location: third_page.php");
exit;

third_page.php

$user= $_POST['username'];
$email = $_POST['useremail'];
$pass= $_POST['password']; 

echo ' the user name: '.$user;
echo ' the user email:.'$email;
echo 'the user password:.'$pass;
Mj Jam
  • 173
  • 4
  • 6
  • 16
  • There's only one POST request to the second page, the POST data is only available there. You cannot forward or redirect a request including POST data. – deceze Aug 08 '13 at 10:55
  • You are just redirecting to `third_page.php` without sending any parameter value with it. – Yogesh Suthar Aug 08 '13 at 10:55

5 Answers5

1
$user_id = mysql_insert_id();
header("location: third_page.php?id=$user_id");

anf ftech data from datbase
Anil Gupta
  • 632
  • 4
  • 16
0

You're simply make some assignments in third_page.php but no actual input is being sent to that page. If you want to be able to access the variables in that page, you'll have query the database and get the results directly, or make use of sessions.

Method 1:

In second_page.php, add the following:

$_SESSION['username'] = $user;
$_SESSION['useremail'] = $email;
$_SESSION['password'] = $pass;

In third_page.php, add the following:

$user = $_SESSION['username'];
$email = $_SESSION['useremail'];
$pass = $_SESSION['password'];

Now you'll be able to access the variables in third_page.php.

Method 2:

$query = mysql_query("SELECT username, useremail, password FROM table") or die (mysql_error());

while ($row = mysql_fetch_array($query)) {
    $user = $row['username'];
    $email = $row['useremail'];
    $pass = $row['password'];
}

Important:

  • You're inserting unescaped variables into your database. Your code is vulnerable to SQL Injection. Use mysql_real_escape_string() to escape user inputs.
  • There is no more support for mysql_* functions, they are officially deprecated, no longer maintained and will be removed in the future. You should update your code with PDO or MySQLi to ensure the functionality of your project in the future.

Hope this helps!

Community
  • 1
  • 1
Amal Murali
  • 75,622
  • 18
  • 128
  • 150
0

In third_page.php your $_POST data is empty (you don't send a form in second_page.php).

Try fetching data from your database in third_page.php !

One more thing, in second_page.php, $username and $useremail are not defined (you define $user and $pass).

Antoine Augusti
  • 1,598
  • 11
  • 13
0

Replace below code:

mysql_query("INSERT INTO table (username, useremail, email) VALUES  ('$username','$useremail','$email');
header("location: third_page.php");

With the following code:

mysql_query("INSERT INTO table (username, useremail, email) VALUES  ('$user','$email','$email');
header("location: third_page.php?username=$user&useremail=$email&password=$pass");

And also change the $_POST with the $_REQUEST:

$user= $_REQUEST['username'];
$email = $_REQUEST['useremail'];
$pass= $_REQUEST['password'];

But sending the data by URL is not secure so send the id in the url and fetch the data on third.php page is good practice.

Code Lღver
  • 15,573
  • 16
  • 56
  • 75
0

This is my code , query the database and then print the results

<?php
$con=mysqli_connect("localhost","name","pass","db");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$result = mysqli_query($con,"SELECT * your where statement");
$job = array();
while($row = mysqli_fetch_array($result)){
    $job[] = $row;
}



<div id="content">
        <div class="priority">
        <?php foreach($job as $item): ?>
            <div class="jobpost">

            Job: <?php echo $item['job'] ?><br />
            Type: <?php echo $item['type'] ?><br />
            Comments: <?php echo $item['comments'] ?><br />
            Timescale: <?php echo $item['timescale'] ?><br />
            Date: <?php echo $item['date'] ?><br />
            <a href="<?php echo $item['id'] ?>">Go to this job</a>


        </div>
        <?php endforeach; ?>
    </div>
user2389087
  • 1,692
  • 3
  • 17
  • 39