开发者

How to post a specific record from resultset to another page

开发者 https://www.devze.com 2023-03-07 13:19 出处:网络
I\'m a newbie in PHP and i\'ve got stucked into this... I have a database and a simple search form. I can search without problems using the criteria i want - for example, i fill in as name \"Alex\" a

I'm a newbie in PHP and i've got stucked into this...

I have a database and a simple search form. I can search without problems using the criteria i want - for example, i fill in as name "Alex" and i can see 5 records in my resultset with this criteria. So far so good...

Here's the problem : I need to create a Link / Button - whatever - and Post/Get the values for the specific record that i'll choose. When the resultset contains only one record found, i have no problems - everything works fine. But, whenever the resultset returns more than 1 records, the Post/Get method grabs the data for the last record.

Let me show you what i'm doing here... Here's the data i'm retrieving :

    while ($row= mysql_fetch_array($result)) {

    $my_id = $row["ID"];
    $my_name = $row["name"];
    $my_profession = $row["profession"];

    echo "<div align='center'><tr>
     <td><div align='center' style='color:white;'><font size='2' face='Arial'>$my_id</div></td></font>
     <td><div align='center' style='color:white;'><font size='2' face='Arial'>$my_name</div></td></font>
     <td><div align='center' style='color:white;'><font size='2' face='Arial'>$my_profession</div></td></font>
     <td><div align='c开发者_JAVA技巧enter' style='color:white;'><font size='2' face='Arial'><form action='person_info.php' method='POST'><input type='hidden' name='PersonID' value='$my_id' /><input type='hidden' name='PersonName' value='$my_name' /><input type='hidden' name='PersonProfession' value='$my_profession' /><input type='submit' value='Show' /></div></td></font>

    .....

Here's the code for person_info.php :

    $my_id = $_POST["PersonID"];
    $my_name = $_POST["PersonName"];
    $my_profession = $_POST["PersonProfession"];

    echo "
    <div align='center'><font size='4' face='Georgia' style='color:red';><b>$my_id, $my_name,  $my_profession</b></font></div>

If there's only one record involved, everything works great. If there's more than one, i get the last one's details. For example, from the resultset :

1 Alex Unemployed 2 Alex Carpenter 3 Alex Gardener

... the record that will be posted finally is the "3 Alex Gardener".

Any ideas ?

Thanks in advance!


If that is the exact code you are using, it doesn't look like you are closing your form. So when you click the submit button, it's grabbing all of the data and then overwriting it each time until it gets to the last one.

Throw a:

</form>

At the end there and it should clear it up.


is personID, personname and personProffesion always the name of the input? it looks like you base the value on the database entry that is currently pulled in but If you are not using a unique name that will cause an issue.

If you are using the same name for each set of hidden inputs there is no way for to tell them apart when you pass them with Post or Get, your name field should be unique, the last entry is the only one that is going through because as each one is added its essentially overwriting the past entries.

let me know if that works for you, I hope you get it figured out!

EDIT:

The easiest way to make each entry go to a unique page is using a GET (as mentioned by Paul Weber) multiple forms with post work but it gets messy quick:

        while ($row= mysql_fetch_array($result)) {

        $my_id = $row["ID"];
        $my_name = $row["name"];
        $my_profession = $row["profession"];

        echo'<a href="somepage.php?personid='.$my_id.'&personname='.$my_name.'&personprofession='.$my_profession.'">Name Your Link</a>';

        }

the code above would create a link that passes the required information as a get you can then pull it out on the somepage.php its also a lot cleaner then the forms.


If you can use get, you can pass the Parameters just by appending them to the Url.

So you could go

<a href="save.php?PersonName=$name&Profession=$profession...">Save</a>

If you cannot use Get, you could either seperate the Forms, creating a Form tag around all the Input elements, so only the ones in the current scope would be submitted. Which would mean you would create a new Form for every Line.

Alternatively use JQuery to do a post.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号