0

I have this while loops and an embedded if statement which places a hyperlink in the appropriate table cell. When I press edit the URL reflects what I want to see (which is the current word to be edited) but the text field the word is suppose to populate is only ever the first entry in the database table.

I have tried Using PHP variables inside HTML tags? which I think does not work because mine is an internal link, not another website.

I have also tried to store $row['yourword'] in another var, so when the while loop encounters that if statement which puts the edit in the right cell, it also would save the correct yourword, but it just results in an error.

Here is the while loop

while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['uname'] . "</td>";
echo "<td>" . $row['clickrate'] . "</td>";
if($row['uname'] == $logedInUsername)
{
    echo "<td>" . $row['yourword'] . "<a href='edityourword.php?edit=$row[yourword]'> edit</a></td>";
}
else
{
    echo "<td>" . $row['yourword'] . "</td>";
}
echo "</tr>";
}
echo "</table>";
?>

And here is the destination

<form action="edityourword.php" method="POST">
    word: <input type="text" name="newName" value="<?php echo $row[yourword]; ?>"><br><br>
    <input type="submit" value="Update">    
</form>

The expected result should be the 2nd word showing up in the text field on the next page, instead of the first one each time, editing the word still works, but the text field is just populated with wrong one each time if it is not the user which is the first on the table.

PS. If visualising helps: table

4
  • 1
    Shouldn't you get the value in destination as : $_GET['edit'] ? Commented May 27, 2019 at 18:34
  • @emreozdemir, okay that worked! After reading this (php.net/manual/en/reserved.variables.get.php) I understand why. Although it works I do get a warning which does not seem to affect anything: Undefined index: edit on line 22 line 22 is the line of interest Commented May 27, 2019 at 18:43
  • Well it's another question but you should check when to use $_POST or $_GET and isset() in php. Commented May 27, 2019 at 18:50
  • @emreozdemir will do, can you write up that $_GET['edit'] comment as an answer so I can mark it as correct? Commented May 27, 2019 at 19:13

1 Answer 1

0

You should get the value of 'edit' in your destination with : $_GET['edit'] since you are sending it as parameter in the link

<a href='edityourword.php?edit=$row[yourword]'> edit</a>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.