Open new page with table Data from previous page

时间:2016-07-11 19:05:47

标签: php mysql

I'm building a website for the company I work for that is going to eventually replace the CRM we are using. Right now I have created a simple php file that creates a table of simple/basic values (I'm trying to test the concept before I scale it up to the read deal). I am using a WHILE loop to generate the table by pulling data from the server. I was able to make the first column in each row into a clickable link that will open to a new page. On this page, I want to post more detailed data that can be edited. For example, in the display.php file that shows the table created with the while loop I will have a property address, a city name and a person who is working to either buy or sell that property. When the user clicks on the first address, I want it to open into a page that will display information like bedrooms, bathrooms, square footage, subdivision, asking price, etc. It would be nice to have each of those fields editable too, but that's a different thing to tackle. Right now, I'm concerned with being able to click on one property and have it open up with the correct data in the next page. Right now, it successfully opens the page but it only shows the data from the first row no matter which row I click on in the table.

Here is the code that I have for the page that displays the table:

<?php
session_start();
if(isset($_SESSION['id'])) {
    $username = $_SESSION['username'];
    $userId = $_SESSION['id'];
} else {
    header('Location: index.php');
    die();
}
$dbCon = mysqli_connect("localhost", "##", "##", "##");
// Check connection
if (mysqli_connect_errno())
    {
        echo "Failed to connect: " . mysqli_connect_error();
    }
$sql="SELECT * FROM leads";
$records=mysqli_query($dbCon,$sql);
?>
<html>
<head>
<title>Display Data</title>
<body>
<table action="" method="post" class="table" id="example">
    <tr>
    <th>Address</th>
    <th>City</th>
    <tr>
    <?php
    while($leads=mysqli_fetch_assoc($records)){
        echo "<tr>";
        //I'm trying to figure out how to pass the record's ID as a way of keeping track of which record I want to look at when I open it in the next page. I don't know how to put the id in the link so that it carries through to the next page.
        echo "<td><a href='showstuff.php?leadid=$leadid'>".$leads['address']."</a></td>";
        echo "<td>".$leads['city']."</td>";
        echo "</tr>";   
    }
?>
</tr>
</table>
</body>
</head>
</html>

Here is my code for the page that should open up with a more detailed "profile view" of the data:

<?php
//connect to db
	$dbCon = mysqli_connect("localhost", "##", "##", "##");
// Check connection
if (mysqli_connect_errno())
{
	echo "Failed to connect: " . mysqli_connect_error();
}
//I tried to incorperate "WHERE leadid = '$_GET['leadid']'" in line 10 to define the lead id that is associated with the record that was selected in display.php and shoould be opened/shared to this page
	$sql="SELECT * FROM leads";
	$records=mysqli_query($dbCon,$sql);
	$leads=mysqli_fetch_assoc($records);
	//I tried to create a $_GET variable that calls on the record id that I am trying to pass from the display.php file to this page
	$leadid=$_GET['leadid'];
?>
<html>
	<head>
		<title>Show Stuff</title>
		<body>
			<h1>Show Stuff Here</h1><br>
			<?php
			echo "<p>Test</p><br>";
			//I only have one piece of information here to test if it works in the first place. Once it does, I'll add the rest of the fields to display
			echo "Is ".$leadid=$leads['leadid']." your ID number?";
			?>
		</body>
	</head>
</html>

Lastly, I am using sessions on here since eventually there will be various users with different levels of access to view and edit things. For now, it's really not too functional other than to log in and log out. It's also not very secure. But I'm more focused on figuring out the mechanics of one thing at a time as I build. It's no where near ready to be used. But I'm hopeful that I'll get there soon. You'll have to excuse my simple code, I'm just learning and teaching myself on my spare time since our company refuses to hire someone to actually do this...

2 个答案:

答案 0 :(得分:1)

You're putting $leadid in the URL parameters in the table, but you never set this variable.

<?php
while($leads=mysqli_fetch_assoc($records)){
    echo "<tr>";
    $leadid = $leads['leadid'];
    echo "<td><a href='showstuff.php?leadid=$leadid'>".$leads['address']."</a></td>";
    echo "<td>".$leads['city']."</td>";
    echo "</tr>";   
}
?>

Then you should be able to use $_GET['leadid'] in showstuff.php to show the information for the lead that they clicked on.

$leadid = intval($_GET['leadid']);
$sql="SELECT * FROM leads WHERE leadid = $leadid";

答案 1 :(得分:0)

First, in your table, you have to put like this:

 while($leads=mysqli_fetch_assoc($records)){
    echo '<tr>';
    echo '<td><a href="showstuff.php?leadid='.$leads['id'].'" >'.$leads['address'].'</a></td>';
    echo '<td>'.$leads['city'].'</td>';
    echo '</tr>';   
}

Like this your link leadid will have a value.

Use simple quote ' ' for html value, it's faster and better for PHP. ;)

In your second file, make like this:

$leadid=$_GET['leadid'];
$sql="SELECT * FROM `leads` WHERE `id`='$leadid' ";
$records=mysqli_query($dbCon,$sql);
$leads=mysqli_fetch_assoc($records);

It's better to put for the table and columns names and ' ' for values.

In $leads array, you 'll have all data. You can "print" it like this:

echo 'The ID is '.$leads['id'].' and the name is '$leads['name'];

I hope it's useful!