Echo value from MYSQL Database to url [duplicate] - php

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 3 years ago.
I have a table that draws information from my MYSQL database. I have a button that I want to echo the id of the row of the information I draw. I can echo all the information to the table but for some reason no idea how to get the information into the URL
I want to use it in the next page in the GET function for additional data being drawn
echo "<td> <a href='dashboard.php?id="<php echo[id], ?>"'><button>Next</button></a> </td>";
I am receiving this error:
Parse error: syntax error, unexpected 'echo' (T_ECHO), expecting ',' or ';' in
The unexpected Echo they referring to I think is the second one as the first echo on previous rows in my table I have no issues
Update:
I have this sql string:
$sql = "SELECT * FROM new_case WHERE status = 'Active'";
In the table I have one row as below and it shows the id of that row.
echo "<td>" . $row['id'] . "</td>";

You made mistake here <php echo[id], ?>
you are already inside PHP, so dont use <?php ?>
also echo[id] is not valid, PHP variable start with $
So replace your line with below line.
echo "<td> <a href='dashboard.php?id=".$row['id']."'><button>Next</button></a> </td>";

It seems there is a typo at the echo function, the syntax of the echo function is echo <string> you can see it in the documentation.
So calling echo[id] is invalid syntax, it expects a string of some sorts (this can be a variable too, as long as it implements a __toString function).
A right way to call it would be like echo "<td><a href='dashboard.php?id=$id'"... when you use double quotes PHP will try to interpit the quote and parse any variables written inside of it.
However, it is important to know that is is a security risk to directly output database information into an URL.

Please use below code
echo "<td> <a href='dashboard.php?id=".$id."'><button>Next</button></a> </td>";
instead of
echo "<td> <a href='dashboard.php?id="<php echo[id], ?>"'><button>Next</button></a> </td>";

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>";

How do I echo a <a> tag which has an href that is using php echo base_url() [duplicate]

This question already has answers here:
How can I combine two strings together in PHP?
(19 answers)
Closed 7 years ago.
Hi I am trying to produce a <td> which also contains an <a> link which will redirect to a function in my controller which also echos the id of my data. Here is my code so far:
<?php
if ($this->session->userdata("username")==$info->U_username) {
echo '<td>EDIT</td>';
}
?>
This code produces an error Disallowed Key characters. Any help or comment is highly appreciated.
For concatenating strings PHP has . operator.
echo '<td>EDIT</td>';
You need to add the result of base_url() to the string you want to output, e.g.:
echo '<td>EDIT</td>';
Either you need to concatinate instead to use php multiple time .use like this
<?php
if ($this->session->userdata("username")==$info->U_username){
echo "<td><a href='".base_url()."'/gamestalker/edit_content/'".$info->C_id."'>EDIT</a></td>";
}
?>
or don't include html into php tags like this
<?php
if ($this->session->userdata("username")==$info->U_username){ ?
<td>EDIT</td>
<? }
?>
You need string concatenation. In PHP you use . for that.
Also codeigniter's base_url can take an argument:
<?php
if ($this->session->userdata("username")==$info->U_username){
echo '<td>EDIT</td>';
}
?>

How can i display form data in echo php? [duplicate]

This question already has answers here:
How to echo php code inside html which is inside php code in this situation? [duplicate]
(2 answers)
Closed 7 years ago.
How can i display form data in echo php ?
My code is:
<?php
echo "<td align='center' style='vertical-align:middle;'> Bust:
<?php
echo $_POST["bust"];
?>
<br> Waist:
<?php
echo $_POST["waist"];
?>
<br> </td>";
?>
But this doesn't work, please guide me with correct steps.
There are a lot of problems with your code.
You're using " in $_POST["bust"] but you opened echo with " as well, causing a conflict.
You're using <?php instead an echo... which is already PHP. There's no need to do this.
It is best to isolate variable printing from your code. Use string concatenation with . as such:
echo "<td align='center' style='vertical-align:middle;'> Bust: ".$_POST["bust"]."<br> Waist: ".$_POST["waist"]."<br> </td>";

Display link as hyperlink enabled output from mysql in php page but I am getting error [duplicate]

This question already has answers here:
Print string with a php variable in it
(4 answers)
Closed 11 months ago.
If I execute this I am getting output properly:
echo "'.$elink.'";
but when I want to display my output in a table column format I am not able to insert:
echo "<td width='200'>" '.$elink.' "</td>";
or
echo "<td width='200'>" "'.$elink.'" "</td>";
or
echo "<td width='200'>" ''.$elink.' "</td>";
Please correct the syntax errors.
echo '<td width="200">' . ''.$elink.'</td>';
Looks like you have mismatched quotes. But I would use sprintf.
echo sprintf("<td width='200'><a href='%1$s'>%1$s</a></td>", $elink);
First off you quotes are messed up. They should look like this:
echo "<td width='200'> <a href='".$elink."'>".$elink."</a></td>";
Correct your string format
echo '<td width="200">' . $elink . '</td>';
I suggest you to use the attribute style (style="width:200px;") instead the width attribute.
Remember to Url Encode the parameters contained in the href attribute.

using ' and " in php syntax [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to get useful error messages in PHP?
Ive started on part of my new year resolution and decided to learn php, as part of it im trying to parse in an xml feed, and echo out the name of the events wrapped in <a> tags linking them back to the events page on the xml feed's site.
I think ive got it all in but i cant seem to see why this isnt working im just getting a blank page, if some one could point me in the right direction it would be much appreciated, cheers
<?php
// F1 W/H xml feed
$xml = simplexml_load_file('http://whdn.williamhill.com/pricefeed/openbet_cdn?action=template&template=getHierarchyByMarketType&classId=5&marketSort=HH&filterBIR=N');
foreach ($xml->response->williamhill->class->type as $type) {
$type_attrib = $type->attributes();
echo "<h2>".$type_attrib['name']."</h2>"; //Title - in this case f1 championship
} ?>
<ul>
<?php
foreach($type->market as $event) {
echo "<li>";
echo "<a href="$event_attributes['url']">";
echo $event_attributes['name'];
echo "</a>";
echo "</li>";
}
?>
</ul>
echo "<a href="$event_attributes['url']">";
try changing that line to
echo "<a href=\"".$event_attributes['url']."\">";
The Php parser is pretty funny about this. Usually you pick one and just stick to it, or use both single quotes and double quotes as you please. Just remember that strings with double quotes are parsed for variables.
$hello = "Hello";
echo "$hello master";
is the same as
$hello ="Hello";
echo $hello.' master';
When you are testing your PHP scripts, you'll find it useful to switch on errors - then PHP will actually tell you why it isn't showing you anything:
error_reporting(E_ALL);
Normally you will have missed a ; or mis-typed a variable name.
in your case the error is here:
echo "<a href="$event_attributes['url']">";
You have accidentally ended the string with a double quote, so PHP thinks the string ends here:
echo "<a href="
This is where using single-quotes can be very handy because your double quotes won't then close the string.
echo '<a href="' . $event_attributes['url'] . '">';
The main difference between single and double quotes in PHP is that double quotes has special clever parsing rules and single quotes doesn't. For example:
$myVar = "BLAH";
echo "Example $myVar"; // Example BLAH
echo 'Example $myVar'; // Example $myVar
In your unordered list, you should use a dot to concatenate your string, and escape your double quotes like this:
echo "<a href=\"".$event_attributes['url']."\">";
Instead of
echo "<a href="$event_attributes['url']">";
Your example throws and error because you haven't used proper string concatenation. However, even with correct concat, it would render as <a href=http://someurl>, and you'd need to add the double quotes according to html standard. Hence you have to double quote.
if you want to not be troubled by having to switch between using a ' or a " then i suggest using the php alternative syntax php alternative syntax
with the given code it would look like
<?php
// F1 W/H xml feed
$xml = simplexml_load_file('http://whdn.williamhill.com/pricefeed/openbet_cdn?action=template&template=getHierarchyByMarketType&classId=5&marketSort=HH&filterBIR=N');
foreach ($xml->response->williamhill->class->type as $type) {
$type_attrib = $type->attributes();
echo "<h2>".$type_attrib['name']."</h2>"; //Title - in this case f1 championship
} ?>
<ul>
<?php foreach($type->market as $event):?>
<li>
<a href="<?php echo $event_attributes['url']; ?>">
<?php echo $event_attributes['name']; ?>
</a>
</li>
<? endforeach;?>
</ul>
one advantage this would bring is that it would produce cleaner code since you can clearly distiguish your php code from your html which is the presentational part at the price writing all those other <?php ?> and as what others would claim a performance degradation. the choice is yours
Change
echo "<a href="$event_attributes['url']">";
for
echo "<a href=".$event_attributes['url'].">";
You are missing the periods in your second echo, where you have your $event_attributes['url']
<?php
foreach($type->market as $event) {
echo "<li>";
echo "<a href=".$event_attributes['url'].">";
echo $event_attributes['name'];
echo "</a>";
echo "</li>";
}
?>
I would recommend you to enable your error log, it would allow you to know the line with problems in any of your scripts.

Categories