GET method redirect error - php

I have a edit URL on my webpage, and want to redirect to the same page and execute the code.
Example: test.php: edit url clicked -> test.php.
code test.php:
while ($row = mysql_fetch_array($query)){
echo "<tr>";
echo "<td>".$row['name']."</td>";
echo "<td><a href='test.php&id=".$row['id']."'>Edit</a></td>";
echo "</tr>";
}
Now I have the following code also in test.php
if (isset($_GET['id'])){
echo "Test";
}
Output:
http://prntscr.com/2cauwi
Why does it not $_GET the ID and echo test?

The question mark is used as the separator. That's what separates your query string from the rest of the URL. In this case, the query string isn't separated properly (because you're using & as the separator) and it tries to access an incorrect page, causing a 404 error. The URL should be the following instead:
example.com/includes/test/test.php?id=14
^
You use & when you have multiple query parameters. For example:
example.com/includes/test/test.php?id=14&name=Foo

echo "<td><a href='test.php?id=".$row['id']."'>Edit</a></td>";

change this line
echo "<td><a href='test.php&id=".$row['id']."'>Edit</a></td>"
to
echo "<td><a href='test.php?id=".$row['id']."'>Edit</a></td>"
Note the ? instead of the & the & is only used for subsequent parameters.

Related

PHP create Url in mysqli_query while loop for every record

I would like to create a link within a while loop where the anchor link is a record from the query and the href passes a variable from the same query to another page so I only have to create one page that displays information based on the passed variable.
echo "<td>";
echo ''$row['result']''';
echo "</td></tr>";
This link kills my page and return an error.
echo "<td>";
echo ''.$row['result'].'';
echo "</td></tr>";
Should fix your code your quotes are not closed right
Your href string concatenation is incorrect.
It seems like you tried closing your strings and concatenating them halfway through and then stopped. Even with Stack Overflow you can see the string errors.
This is how your href echo should look:
echo '<a href="stats_game.php?idGame=' .$row['idGame'] . '>' . $row['result'] . '</a>';
It works fine for me
echo "<td><a href='stats_game.php?idGame=".$row['idGame']."'>".$row['result']."</a></td>";

Incorporating php variable into url with href attribute

I am trying to create the necessary url from this code however it is working and I am struggling to find out why.
$linkere = $row['message'];
echo '<a href="me.php?message=<?php echo rawurlencode($linkere); ?>">'
Currently this code is producing the url: me.php?message= . But, I would like it to create the url: me.php?message=hello for example.
Thanks for helping!
You are passing $linkere to rawurlencode(). The variable is actually named $linker.
$linker = $row['message'];
echo '<a href="me.php?message=<?php echo rawurlencode($linker); ?>">'
You have alot of syntax problems here.
first, you need to use Concatenation message='.rawurlencode($linker).'"
second your variable do not exist, it should be $linker.
Second close the tag and insert the text, in this case i used Test.
$linker = $row['message'];
echo 'Test';
Can you try this,
$linker = $row['message'];
echo 'YOUR LINK TEXT HERE';
You don't need the <? ?> and echo in your echo, it should just be:
$linkere = $row['message'];
echo 'Test';
Otherwise you are turning php on and off again to echo something within an already open instance of php in which you are already echoing.

space is neglected when passed a string by URL

I am using the following code to display a hyperlink inside a table:
echo "<td><a href=http://www.smstoneta.com/show.php?opcode=TCP Y".">".
$row['num_y']."</a></td>";
The hyperlink is displayed successfully but when I click on the hyperlink, the URL is
www.smstoneta.com/show.php?opcode=TCP
instead of
www.smstoneta.com/show.php?opcode=TCP Y
Why am I not getting the full URL?
Use urlencode()
$opCode = urlencode('TCP Y');
echo "<td>".$row['num_y']."</td>";
You need URL Encode spaces to make them working in links.
Here is manual for PHP function urlencode
$safe_url = urlencode('http://www.smstoneta.com/show.php?opcode=TCP Y');
echo "<td>" .$row['num_y']. "</td>";
BTW, more readable (no concatenation needed) version to echo such strings is:
echo "<td><a href='{$safe_url}'>{$row['num_y']}</a></td>";

$id is not going with the link

I am creating a hyperlink in foreach loop. It is working fine. When I am passing $id in URL parameter then it is not working. my link is showing http://****/test/index.php/test/view?id=**. i don't what i am doing wrong here.
foreach($list as $item)
{
$rs[]=$item['uname'];
$id=$item['uid'];
//var_dump($id); here it's printing $id value...
echo '<b> '.$item['uname'].'<br/>';
}
I want to pass $id value with hyperlink. Please suggest me.
It's of course getting printed -- your browser is just not displaying it to you since it's not being correctly parsed as HTML due to the extra " around the $id variable.
Set your header as follows:
header('Content-Type: text/plain');
and you'll see that it returns something like:
<b> FOOBAR<br/>
^ ? ^
As you can see, the issue is the extra double-quote before 55.
Change your code to:
echo '<b> <a href="/test/index.php/test/view?id=' . $id .'">'.
$item['uname'] . '</a><br/>';
Alternatively, you could also use double-quotes and enclose your variables inside {}, like so:
echo "<b> <a href=\"/test/index.php/test/view?id=$id\">{$item['uname']}
</a><br/>";
I'd use sprintf as it's cleaner.
echo sprintf('<b> %s<br/>', $id, $item['uname']);
You have another ".
Change this:
echo '<b> '.$item['uname'].'<br/>';
To this:
echo '<b> '.$item['uname'].'<br/>';
Try this:
echo "<b><a href='/test/index.php/test/view?id=$id'>$item</a></b><br/>";
This works!
And is the easiest and cleanest option. Inside of the double escaping, the simple is used for the html and through the double escaping all variables are written inside :) . Very simple.

Add PHP variable inside echo statement as href link address?

I'm trying to use a PHP variable to add a href value for a link in an echo statement.
Here's a simplified version of the code I want to use. I know that I can't just add the variable into the echo statement, but I can't seem to find an example anywhere that works.
$link_address = '#';
echo 'Link';
Try like
HTML in PHP :
echo "<a href='".$link_address."'>Link</a>";
Or even you can try like
echo "<a href='$link_address'>Link</a>";
Or you can use PHP in HTML like
PHP in HTML :
Link
you can either use
echo 'Link';
or
echo "Link';
if you use double quotes you can insert the variable into the string and it will be parsed.
Basically like this,
<?php
$link = ""; // Link goes here!
print "Link";
?>
as simple as that: echo 'Link';
You can use one and more echo statement inside href
Link
link : "/profile.php?usr=firstname&email=email"
This worked much better in my case.
HTML in PHP: Link
The safest way to generate links in PHP is to use the built-in function http_build_query(). This function is very easy to use and takes an array as an argument.
To create a dynamic link simply echo out the result of http_build_query() like so:
$data = [
'id' => $id,
'name' => $name
];
echo 'Link';
If you want to print in the tabular form with, then you can use this:
echo "<tr> <td><h3> ".$cat['id']."</h3></td><td><h3> ".$cat['title']."<h3></</td><td> <h3>".$cat['desc']."</h3></td><td><h3> ".$cat['process']."%"."<a href='taskUpdate.php' >Update</a>"."</h3></td></tr>" ;

Categories