Getting mysql field data when a link is clicked?

2019-09-11 23:57发布

问题:

I'm trying to get data from a database if a link is clicked.

Basically i have a main.php that contains this:

$sql2="SELECT projectsid, projectname, description, 
    SUBSTRING(description,0,80) FROM projects";
$result2=mysql_query($sql2);    

while($row2 = mysql_fetch_assoc($result2)) {
    echo "<div id=\"links\">";
    echo "<ul>";
    echo "<li> <a href=\"fullproject.php\">" . $row2['projectname'];
    $_SESSION['projectname']= $row2['projectname'];
    echo "<em>" . $row2['description'] . "</em></a></li>";
    echo "</ul>";
    echo "</div>";
}

This displays a list of names and a brief description.

  • Proj1
    • description
  • Proj2
    • description

What i'd like to do is to display the full contents of a project if that one is clicked.

fullproject.php

<?php
session_start(); 
$projectname= $_SESSION['projectname'];

// Connect to server.
require ("connect.php");

$sql1="SELECT projectsid, projectname, programme, difficult, requirements,
    resources, description, contact, gsize, size 
    FROM projects WHERE projectname = '$projectname'";
$result1=mysql_query($sql1);    

while($row1 = mysql_fetch_assoc($result1)) {
echo "Project Name: " . $row1['projectname']. "<br />";
echo "Programme : " . $row1['programme'] . "<br />";
echo "Difficult : " . $row1['difficult'] . "<br />";
echo "Requirements : " . $row1['requirements'] . "<br />";
echo "Resources : " . $row1['resources'] . "<br />";
echo "Description : " . $row1['description'] . "<br />";
echo "Contact : " . $row1['contact'] . "<br />";
echo "Group size : " . $row1['gsize'] . " " . $row1['size'] . "<br />";
echo "<br /> ";
}

Whenever I click any of the project list items, it only displays the last itemin the list. I believe this is happening because when the while loop ends the session variable is set to the last one. Can someone tell me how to fix this? Thanks

回答1:

Try using GET variable instead of session

in main.php use

    echo "<li> <a href='fullproject.php?projectname=$row2[projectname]'>";

in fullproject.php declare a variable and initialize it to the get variable

   $projectname= $_GET['projectname'];


回答2:

There is a lot wrong with this code.

1) You are trying to send the primary key of the table via 2 different methods in main.php: in the session and via the path of the URL. This is just bad practice - the reason you have access to different conduits for passing data is that they all have different semantics, but even then, sending the same thing twice then there's always going to be a risk that the receiving end may see different values and must terefore have a way of reconciling the discrepancy.

2) You are using the session to pass a transactional data item. This is a bad idea - the data item relates to a specific transition - not to the session. $_SESSION should only be used for storing data relating to the sesion - not to navigation or transactions.

3) The other conduit you provided for passing the data item is by appending it to the path within the URL. While in some circumstances this approach has benefits it also has complications specific to the webserver the code is implemented on - you've not provided any explanation of why you are using this apporach instead of a conventional $_GET or $_POST var.

The reason your code is failing is because you can only store a single value in $_SESSION['projectname']. Even if you stored an array of values in the session, there is no mechanism for the webserver to know which of these values was selected when the code was delivered to the browser.

Your HTML is badly structured and poorly formatted too.

The code you've shown does not match the description you've given (it does not display the project name).

Your code is also wide open to SQL injection attacks.

Change it so that in main.php:

echo "<div id=\"links\">\n";
echo "<ul>\n";
while($row2 = mysql_fetch_assoc($result2)){
   echo "<li> <a href=\"fullproject.php?project=\"" 
      . urlencode($row2['projectname']) . "\">"
      . htmlentities($row2['projectname']) . "</a>\n";
   echo "<br /><em>" . $row2['description'] . "</em></li>";
}
echo "</ul>";
echo "</div>";

And in fullproject.php

<?php
session_start(); 
require ("connect.php");
$projectname= mysql_real_escape_string($_GET['projectname']);


回答3:

echo"<a href='fullproject.php?project_id='".$row2['project_id'].
"> $row2['projectName']</a>";

in fullproject.php

$project_id=$_GET['project_id'];

then you can use this id to display contents of that project id from database