Page 1 of 1

mysql created data with php; making dynamic link with &_POST

Posted: Sat Feb 06, 2010 7:22 am
by landobee
Hi all,

I am trying my best to make the following look good through use of the tags, so maybe I need to edit a couple of times. Bear with me please!

If you could help me out with the following, I would very much appreciate it. It's something that shouldn't be too difficult,
but I can't seem to get the question defined enough to find the answer though faqs/google etcetera.

I have a website running, with a database for teachers and students. What I am doing, is making a generated list of students that belong to a certain group (using mysql). This group is also matched to teachers.

My first 1.php contains the following, and gives the teachers that logs in an overview of the students that belong to the same group he or she belongs to. So far so good (that works!). But now, at the end of the table, I want to create a link that goes to 2.php.

And in 2.php, that specific student's information should be viewable. I tried this using sessions (but then the sessions gets
overwritten for the last created student), and through $_POST. I feel that the last method should be the way to go, but can't get it to work. I also want not to use browser url to send id's (to get the protection right), so I think GET is out of the question, right?


This is the code for 1.php (display of all the students's first name who are matching the group the logged in teacher belongs to):

Code: Select all

 
    <?php 
    $userid2 = $_SESSION['mamalou'];
$id2 = $userid2['mentorgroep'];
$studentenresult = mysql_query("SELECT * FROM studenten WHERE mentorgroep='$id2'");
$_SESSION['mijnstudenten'] = mysql_fetch_array($studentenresult);
$mijnstudenten = $_SESSION['mijnstudenten'];
while ($mijnstudenten = mysql_fetch_array($studentenresult)) {
 
 
    if ($mijnstudenten["voornaam"] != ""){
    echo "&nbsp;" . $mijnstudenten["voornaam"]; 
    } else {
    echo "&nbsp;" . "<strong>niet bekend</strong>";
    }
    ?>    
 
// trying through use of $S_SESSION
    <?php 
    $student = $mijnstudenten['id'];
    $_SESSION['student'] = $student;
    echo "&nbsp;" . "<a href='student.php'>volg deze link</a>";
    echo $student;
    echo "<br>";
 
// trying through use of method of POST
    $student2 = $mijnstudenten['id'];
    $_SESSION['student2'] = $student2;
    echo "<form action='2.php' method='POST'>";
    echo "<input type='hidden' name='$student2'>";
    echo "<input type='submit' id='button' value='klik hier'>";
    echo "</form>";
    ?>  
 
<?php } ?>
 

And the 2.php should present only the information of the student with has been clicked on the link in 1.php:

Code: Select all

 
    <?php 
    $student2 = $_SESSION['student'];
$id3 = $student2;
$studentenresult2 = mysql_query("SELECT * FROM studenten WHERE id='$id3'");
$_SESSION['mijnstudent'] = mysql_fetch_array($studentenresult2);
$mijnstudent = $_SESSION['mijnstudent'];
    if ($mijnstudent["voornaam"] != ""){
    echo "&nbsp;" . $mijnstudent["voornaam"]; 
    } else {
    echo "&nbsp;" . "<strong>niet bekend</strong>";
    }
    ?>  
 
I also tried 2.php with $_POST:

Code: Select all

 
    <?php 
    echo $_POST;
    $student3 = $_POST['$student2'];
$id3 = $student3;
$studentenresult2 = mysql_query("SELECT * FROM studenten WHERE id='$id3'");
$_SESSION['mijnstudent'] = mysql_fetch_array($studentenresult2);
$mijnstudent = $_SESSION['mijnstudent'];
 
    if ($mijnstudent["voornaam"] != ""){
    echo "&nbsp;" . $mijnstudent["voornaam"]; 
    } else {
    echo "&nbsp;" . "<strong>niet bekend</strong>";
    }
    ?>  
 
 
Could someone point me in the right direction for this? I have the feeling the answer is pretty simple,
but have been breaking my mind over this one far too long....

Thanks in advance,
landobee

edit 1: tried to clean up the code
edit 2: extra information; that I don't want to use url to add stuff (for protection)

Re: mysql created data with php; making dynamic link with &_POST

Posted: Sat Feb 06, 2010 9:37 am
by hnmxport
heres a suggestion from a newbie so keep that in mind. why don't you try using a form in your php2. have the session maintain the teachers info. on page two you could populate your form fields with the students info and then just submit the form. if you dont know how many field you will need you could always use the php count_rows or somethimg

Re: mysql created data with php; making dynamic link with &_POST

Posted: Sat Feb 06, 2010 9:59 am
by landobee
Thanks hnmxport for the reply :)

It's not a bad idea you posted, but I was asked specifically to make the pages work that way I described :( I feel there is some $_POST thing I am missing, so hopefully someone can shed a light on it :)

Re: mysql created data with php; making dynamic link with &_POST

Posted: Sat Feb 06, 2010 9:52 pm
by limitdesigns
Ha, it looks like you tried everything except what would work ;-)

You should pass the student's ID number to your student.php page via a GET method. For example, in your link, you would write:

Code: Select all

 
<a href="student.php?id=<?php echo $mijnstudenten['id']; ?">volg deze link</a>"
 
Then, on your student.php page, you could do the following:

Code: Select all

 
$id = $_GET['id'];
$studentenresult2 = mysql_query("SELECT * FROM studenten WHERE id='$id'");
$mijnstudent = mysql_fetch_array($studentenresult2);
if ($mijnstudent["voornaam"] != ""){
   echo "&nbsp;" . $mijnstudent["voornaam"];
 else {
   echo "&nbsp;" . "<strong>niet bekend</strong>";
} 
 
Hope this helps!

Re: mysql created data with php; making dynamic link with &_POST

Posted: Sun Feb 07, 2010 4:48 am
by landobee
limitdesigns wrote:Ha, it looks like you tried everything except what would work ;-)

You should pass the student's ID number to your student.php page via a GET method. For example, in your link, you would write:

Code: Select all

 
<a href="student.php?id=<?php echo $mijnstudenten['id']; ?">volg deze link</a>"
 
Then, on your student.php page, you could do the following:

Code: Select all

 
$id = $_GET['id'];
$studentenresult2 = mysql_query("SELECT * FROM studenten WHERE id='$id'");
$mijnstudent = mysql_fetch_array($studentenresult2);
if ($mijnstudent["voornaam"] != ""){
   echo "&nbsp;" . $mijnstudent["voornaam"];
 else {
   echo "&nbsp;" . "<strong>niet bekend</strong>";
} 
 
Hope this helps!
It does, thanks mate :)
I was looking for something with POST, because of the fact that with this GET method the id shows (and is changeable) within the URL of the browser. But, I do have protection on the page itself, so I think that will be enough :)

Re: mysql created data with php; making dynamic link with &_POST

Posted: Sun Feb 07, 2010 5:16 am
by landobee
Also got a response from someone else (thanks towildteen88!); posting that solution with POST here for others too :)

1.php

Code: Select all

 
    $mijnstudenten['id'];
    echo "<form action='student.php' method='POST'>";
    echo "<input type='hidden' name='id' value='".$mijnstudenten['id']."'>";
    echo "<input type='submit' id='button' value='klik hier'>";
    echo "</form>";
 
2.php

Code: Select all

 
<?php 
// get the student id, make sure it is set and that is numeric
if(isset($_POST['id'] && is_numeric($_POST['id']))
{
    $student_id = (int) $_POST['id'];
 
    $studentenresult2 = mysql_query("SELECT * FROM studenten WHERE id='$student_id'");
    $mijnstudent = mysql_fetch_assoc($studentenresult2);
     
   if ($mijnstudent["voornaam"] != ""){
   echo "&nbsp;" . $mijnstudent["voornaam"]; 
   } else {
   echo "&nbsp;" . "<strong>niet bekend</strong>";
   }
}
else
    echo 'Invalid student id';
 
   ?>  
 
Thanks all :)