passing a link title to the next page - php

I'm trying to make automatically generated pages for users based on the username.
Basically I'm creating a table in HTML which displays all the users currently registered. The names are hyperlinks which redirect to a page in which I will post a welcome message.
The problem is that I don't know how to pass the name of the user I click on to the next page.
<table border='1'>
<tr>
<th>Username</th>
<th>Email</th>
</tr>
<?php
while($currentRow = mysql_fetch_array($query))
{
echo "<tr>";
echo "<td>" . $currentRow['name'] . "</td>";
echo "<td>" . $currentRow['email'] . "</td>";
echo "</tr>";
}
?>
As you can see, I tried using get, but it won't work, I think it's because it doesn't know what I'm clicking on. The point is that I want the page with the welcome message say something like "welcome, $username". Any ideas?

You are closing the string with single quotes "'" that is why $_GET does not work. Replace profile.php?name='" with profile.php?name=" and also remove the single quote on the other end.

You have need remove single quote after ?name= and add it to before character >
Use
echo "<td><a href='/templates/profile.php?name=". $currentRow['name'] ."'>" .
$currentRow['name'] . "</a></td>";
Instead of
echo "<td><a href='/templates/profile.php?name='". $currentRow['name'] .">" .
$currentRow['name'] . "</a></td>";

try this in profile.php
Welcome <?php echo $_GET['name'];?>

In your profile.php
echo $_GET['name'];

echo "<td><a href='/templates/profile.php?name=". $currentRow['name'] ."'>" . $currentRow['name'] . "</a></td>";
close the single code after name value you are sending in a tag..
thats it.

Related

Dynamically creating buttons that remove a row from a table

I'm trying to create a button for each row in my database that, when pressed, will delete this particular row. I should also mention that the data from the database is displayed correctly and the table I'm using is also completely fine.The buttons appear at the side of each row, when the button is clicked, the row dissapears but the data is not deleted from the database, when the page is reloaded the rows that were previously "deleted", reappear. After pressing the button i also get this "Fatal error: Uncaught Error: Call to undefined function mysql_query() in C:\xampp\htdocs\INDUSTRIALPROJECT\records.php:56 Stack trace: #0 {main} thrown in C:\xampp\htdocs\INDUSTRIALPROJECT\records.php on line 56".
line 56 is : $del = mysql_query("DELETE FROM records WHERE id=" . $row['id']);. The same query works fine when placed directly into phpMyAdmin.
<?php
// Check connection
include_once 'config.php';
if ($link->connect_error) {
die("Connection failed: " . $link->connect_error);
}
$sql = "SELECT * FROM records";
$result = $link->query($sql);
function post($key){ return(isset($_POST[$key]) ? htmlspecialchars($_POST[$key]) : ""); }
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
if(post('rowButton'.$row['id']) =="Delete"){
$del = mysql_query("DELETE FROM records WHERE id=" . $row['id']);
$deleted = '<p>Entry ' . $row['id'] . ' was succesfully deleted</p>';
}
else {
echo '<form action ="' . $_SERVER['PHP_SELF'] . '" method="post">';
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row["visitingdate"]. "</td>";
echo "<td>" . $row["department"] . "</td>";
echo "<td>" . $row["visitingreason"]. "</td>";
echo "<td>" . $row["importance"]. "</td>";
echo "<td>" . $row["visitorname"]. "</td>";
echo "<td>" . $row["company"]. "</td>";
echo "<td>". $row["internalrecipientname"]. "</td>";
echo "<td>". $row["visitinglocation"]. "</td>";
echo "<td>". $row["ETA"]. "</td>";
echo "<td>". $row["ETD"]. "</td>";
echo "<td>". $row["HRverification"]. "</td>";
echo "<td>". $row["visitcompleted"]. "</td>";
echo '<td><input type="submit" name="rowButton'. $row['id'] .'" value="Delete"/> </td>';
echo "</tr>";
echo "</form>";
}
}
echo "</table>";
echo $deleted;
}
else { echo "0 results"; }
$link->close();
?>
First, this appears to be rather vulnerable to SQL injection attacks. StackOverflow is quite font of pointing this out up front, because it's really a solved problem that you should account for in the early stages of development. You're taking untrusted data (that was submitted by the user, without sanitizing it) and putting it directly in an SQL query. Bad things can happen when that occurs. Now with that aside, on to your actual question.
"Nothing happens" means the page doesn't change at all, right? So the browser doesn't know what to do when the button is clicked.
I think you haven't put any <form...> declaration here, which would be required for <input type="submit"> to do anything useful. You could use JavaScript with the stand alone submit button, but I don't see that in your code, either. You'll need something to tell the browser what to do when the submit button is pressed.
I haven't really tested the rest of your code, but based on what you've got already you might add the following:
else {
+ echo '<form action ="' . $_SERVER['PHP_SELF'] . '" method="post">';
echo "<tr>";
and
echo "</tr>";
+ echo "</form>";
}
(don't add the plus sign, that's just to show which line is added). I should add that I don't usually use submit buttons like this, so there's a chance I missed some additional details about how you're calling this, but putting the form in a <form> tag is at least a good start.
Edit
The mysql_query() function was removed in PHP 7; if you're using an older PHP you need to add support for the MySQL functions or if you're on PHP 7, you should use the MySQLi or PDO_MySQL functions instead. The warning box on the PHP manual page for mysql_query has some links for alternatives, how to select an alternative, and other supporting documentation that may help you. This StackOverflow answer may help, as well.

Give href to a php variable

I want to give href link to the variable below;
echo "<td>" . $row['userid'] . "</td>";
So that admin can click on that ID and in the other page admin can see all details about the user, using this id.
I tried;
echo "<td>" . $row['userid'] . "</td>";
and
echo "<td>" . $row['userid'] . "</td>";
But they didn't work. Thanks in advance.
edit:
Thanks to #Gautam the answer I'm looking for is below;
echo "<td><a href='http://www.google.com'>". $row['expiry'] . "</a></td>";
use this
echo "<td>Click Here</td>";
or else
?>
<td>Click Here</td>
Please use single quotas for your link. If you are using double quotas you will close your echo instead of opening quotas within other quotas:
echo "<td><a href='http://www.google.com'>" . $row['expiry'] . "</a></td>";
Otherwise you also could use \ in order to escape the double quotas:
echo "<td>" . $row['expiry'] . "</td>";
You are gettting error because of quotes. Change the quotes for href to single quotes like below and should work fine.
echo "<td><a href='http://www.google.com'>". $row['expiry'] . "</a></td>";

Sending MySQL data to page through link in a table

We are currently working on a game shop website and have hit a roadblock regarding the purchase link.
The link displays within a mysql table and each link sends the user to the same page.
This is necessary as we will be adding new games to the database and want to do using only a mysql command to make the site as efficient as possible.
This is the code of the table (ignore the fact that the purchase link displays the 'gameCodes'.
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['gameName'] . "</td>";
echo "<td>" . $row['pointsValue'] . "</td>";
echo "<td>" . ''. $row['gameCodes'] .'' . "</td>";
echo "</tr>";
}
echo "</table>";
What I am wanting to do is send the game code of the game that corresponds with the row the link is on to the Purchase.php page to then process the purchase.
Any help is appreciated greatly.
my answer deals with not only passing variables from url to your page....but passing it in clean way
First, make sure that url URL properly encode using "PHP urlencode"
echo "<td>" .
'<a href="Purchase.php?gameCodes='.urlencode($row['gameCodes']) .'">'.
$row['gameCodes'] .
'</a>' .
"</td>";
Then to fetch the data strip_tags from the url variable if any:
echo (strip_tags($_GET['item']));
why is this needed??
Since you are fetching the values from URL, assume i manually change the url to :
Purchase.php?gameCodes=<script>alert("hello")</script>
then without proper handling, gameCodes variable value will be fetched and it would alert "hello" on the page
Have you thought about sending it through the URL like follow:
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['gameName'] . "</td>";
echo "<td>" . $row['pointsValue'] . "</td>";
echo "<td>" . ''. $row['gameCodes'] .'' . "</td>";
echo "</tr>";
}
echo "</table>";
and then process to the purchase using the code sent in the URL
let me know if it corresponds to what you need.
I think you can put the gameCodes id directly in the link
echo "<td>" .
'<a href="Purchase.php?gameCodes='. $row['gameCodes'] .'">'.
$row['gameCodes'] .
'</a>' .
"</td>";
Now you can process the code from the purchase page and retrieve it with $_GET
$_GET['gameCodes'];

id not going through the url

my id is not going through the url. my code is as follows
<?php
include 'library/connect.php';
$result = mysql_query("SELECT * FROM meetings ");
echo "<table border='1'><tr><th>Title</th><th>Chairman</th><th>Secretary</th><th>Terms of Reference</th><th>Named membership</th><th>Occurences</th><th>Book Room</th></tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['title']. "</td>";
echo "<td>" . $row['chairman']. "</td>";
echo "<td>" . $row['secretary']. "</td>";
echo "<td>" . $row['termsOfReference']. "</td>";
echo "<td>" . $row['named_membership']. "</td>";
echo "<td>" . $row['occurences']. "</td>";
?>
<td><font color="#CC3300">Book: room/date/time</font></td>
<?php
}
echo "</tr>";
echo "</table>";
include 'library/closedb.php';
?>
have you got any idea of what the problem can be?
where is $meeting_id set? seems to me like it should be $row['meeting_id'].
Where $meeting_id is defined?
It seems like you did a select but forgot to retrieve the meeting id.
Try to change the link to:
<a href ="secretary_booksRoom.php?meeting_id=<?php echo $row['meeting_id']; ?>">
In case you have column named meeting_id in your table of course.
<tr> tag need to be closed in the while loop
You need to define the $meeting_id variable before adding it to the URL, otherwise your link will simply be "secretary_booksRoom.php?meeting_id=". I am going to assume that your meetings table has an id column as a primary key. In your while loop, try declaring "$meetind_id = $row['your meeting id column']". This should get the meeting id and pass it to the URL.
Hope this helps.

PHP How to pass variables through a link to the next php file

echo "<td>" .
"<a href='approve_mem.php?id=$row['member_id']'>Approve</a>" . " " .
'Disapprove' .
"</td>";
This is my code but the value of id does not get passed instead it gets passed like id=$row['member_id'] as it is, and when I echoed the id variable it got printed like $row['member_id']
Rather than building HTML strings in PHP, I recommend you only switch to the PHP context when required, eg
<td>
Approve
Disapprove
</td>
Try this:
echo "<td>" .
"<a href='approve_mem.php?id=".$row['member_id']."'>Approve</a>" . " " .
'Disapprove' ."</td>";
You need to change it to:-
echo "<td>" .
"<a href='approve_mem.php?id={$row['member_id']}'>Approve</a>" . " " .
"<a href='disapprove_mem.php?id={$row['member_id']}'>Disapprove</a>" .
"</td>";
Notice the use of curly braces and the change of quotes used in the second line. Also you hadn't quoted member_id in your second use of $row['member_id']
Also take note of Dagon's comment above.
I am guessing this is part of a database query. You have to seperate the data from the string in this instance. This can be written two ways:
<td>
Disapprove
</td>
------------- OR -------------
echo "<td><a href='approve_mem.php?id=".$row['member_id']."'>Approve</a> <a href='disapprove_mem.php?id=".$row['member_id']."'>Disapprove</a></td>";
change that to this:
echo "<td>Approve
Disapprove</td>";
or
<td>
<a href='approve_mem.php?id=<?=$row['member_id']?>'>Approve</a>
<a href='disapprove_mem.php?id=<?=$row['member_id']?>'>Disapprove</a>
</td>

Categories