$_GET not picking up the variable - php

What am i doing wrong. The variable will not get picked up. I have tried to echo it out just to check but the echo was blank. What did i do wrong?
if($result->num_rows){
while($row = $result->fetch_array(MYSQLI_ASSOC)){
//Displays them
echo '<h4> ' . $row['fname'] . '&nbsp' . $row['lname'] . ' <h4/>' .'<br/>';
}
request.php below
$requestedId = $_GET['account_id'];
$userId = $_SESSION['user_id'];
Thats the portions of the code i need help with. The account id is passed through the URL.

In the while loop you need to put the name account_id in it, not just the value
echo '<h4> <a href="request.php?account_id='.$row['account_id'].'">' .
$row['fname'] . '&nbsp' . $row['lname'] . '</a> <h4/>' .'<br/>';

Related

Trying to encapsulate an email in <>

I'm trying to pull a first name, last name, and email from a data base and trying to put into Firstname Lastname , with PHP. Every time I try to echo the data I only get Firstname Lastname. The email and <> doesn't show up? Anyone know how I can echo .
Here's the code:
while($row = mysqli_fetch_assoc($res)){
echo $row['firstname'] . " " . $row['lastname'] . " <" . $row['email'] . ">,";
}
Your code works fine, but < and > characters form a HTML tag, that is why you don't see the mail.
If you look at the source code of your echo, you will see the <mail> appearing.
You can also do this :
while($row = mysqli_fetch_assoc($res)){
echo htmlspecialchars($row['firstname'] . " " . $row['lastname'] . " <" . $row['email'] . ">,");
}
htmlspecialchars will transform < and > to their HTML code, so they will be visible even if you're not looking at the source.
A useful function to have is this one :
// Echoes the string in a HTML friendly way
function echoHTML($str)
{
echo htmlspecialchars($str);
}
while($row = mysqli_fetch_assoc($res)){
echoHTML($row['firstname'] . " " . $row['lastname'] . " <" . $row['email'] . ">,");
}
PS : be careful about the last comma if you want to concatenate email adresses, you will have a trailing , at the end of the list of mails. Here is how to avoid it :
$adresses = [];
while($row = mysqli_fetch_assoc($res))
{
// No more "," here
$adresses[] = $row['firstname'] . " " . $row['lastname'] . " <" . $row['email'] . ">";
}
echoHTML(implode(', ', $adresses));

name as email link if email !empty else name

A form sends name, topic, email, and blurb to MySQL db. Email is not a required field, but if it is !empty I want to include it as the name and echo the row data.
This is my idea so far:
if (!empty($_POST['email']))
{
$row['name'] = "<A href='mailto:' . $_POST['email'] . '>' . $_POST['name'] . '</A>";
}
else
{
$row['name'] = $_POST['name'];
}
I am having trouble sorting out the coding for the email/name any help please. And the rows will be:
while($row = mysql_fetch_array( $result )) {
echo "<tr>";
echo '<td><Posted by : ' . $row['name'] . ' on ' . $row['date'] . '<br />
Topic : ' . $row['topic'] . '<br />Thoughts : ' . $row['thoughts'] . '</td>';
echo "</tr>";
Not very well sorted yet, help please :-) thanks.
are you want that if the email is not empty then you want to replace it with name and echo it? if yes then try the below code..
if (!empty($_POST['email']))
{
$row['name'] =$_POST['email'];
$row['name']="<A href='mailto:' . $_POST['email'] . '>' . $_POST['name'] . '</A>";
}
else
{
$row['name'] = $_POST['name'];
}

Using a GET Id from mysql query

I am trying to run a mysql query within some php and have the results echoed as HTML. I got it working but now I would like to insert an id into the links so I can use a page to GET the id. Does anyone know how to do this. What I have tried (and is not working) is below. And at the bottom of the post is what was working originally, but without an id on the link...
<?
echo "<tr bgcolor=\"#CCCCCC\" style=\"border-bottom:1px solid gray;\"><td> Team </td><td>Correct Picks</td><td>Points</td></tr>";
while($row = mysql_fetch_array($memberslist)) {
if ($row['User_ID'] == $id) {
echo "<tr bgcolor=\"#F0F0F0\"><td>" . "$row[User_ID]" . "</td><td><b>" . $row['Correct_Picks'] . " </b> /" . $maxcorrectpicks . "</td><td>" . $row['Points'] . "</td></tr>";
} else {
echo "<tr><td>" . "$row[User_ID]" . "</td><td><b>" . $row['Correct_Picks'] . " </b> /" . $maxcorrectpicks . "</td><td>" . $row['Points'] . "</td></tr>";
}
}
?>
$uniqueid = $_GET["$row['User_ID']"];
echo $uniqueid;
This is from the first page that worked...
<?
echo "<tr bgcolor=\"#CCCCCC\" style=\"border-bottom:1px solid gray;\"><td> Team </td><td>Correct Picks</td><td>Points</td></tr>";
while($row = mysql_fetch_array($memberslist)) {
if ($row['User_ID'] == $id) {
echo "<tr bgcolor=\"#F0F0F0\"><td>" . "$row[User_ID]" . "</td><td><b>" . $row['Correct_Picks'] . " </b> /" . $maxcorrectpicks . "</td><td>" . $row['Points'] . "</td></tr>";
} else {
echo "<tr><td>" . "$row[User_ID]" . "</td><td><b>" . $row['Correct_Picks'] . " </b> /" . $maxcorrectpicks . "</td><td>" . $row['Points'] . "</td></tr>";
}
}
?>
The problem that you are having is that you are trying to access an array element from within double quotes. You could make this work by wrapping it in curly braces {$row['User_ID']}.
To make your code more readable, and to avoid this problem, just concatenate or use a list of values for echo. I also recommend the usage of htmlspecialchars() to ensure you are creating valid HTML.
echo '<tr><td>',
'<a href="2012week1.php?id=',
htmlspecialchars($row['User_ID']),
'">',
htmlspecialchars($row[User_ID]),
'</a>',
'</td><td>'
//etc.

PHP: Trouble using mysql_fetch_assoc

I am trying to return results from my database so that I can create an XML file for use in Adobe Flex, where I will then populate a Google Map. At the moment using Charles i'm getting the following error mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource
Here is the code for my function:
public function getBusiness ($item)
{
$stmt = mysqli_prepare($this->connection,
"SELECT * FROM businesses");
$this->throwExceptionOnError();
mysqli_stmt_execute($stmt);
$this->throwExceptionOnError();
$row = "";
echo "<?xml version=\"1.0\" ?><map>";
while(($row = mysql_fetch_assoc($stmt)) !== false)
{
echo "<business><businessid>" . $row["businessid"] . "</businessid>";
echo "<type>" . $row["type"] . "</type>";
echo "<name>" . $row["name"] . "</name>";
echo "<street>" . $row["street"] . "</street>";
echo "<city>" . $row["city"] . "</city>";
echo "<country>" . $row["country"] . "</country>";
echo "<postcode>" . $row["postcode"] . "</postcode>";
echo "<latitude>" . $row["latitude"] . "</latitude>";
echo "<longitude>" . $row["longitude"] . "</longitude>";
echo "<phonenumber>" . $row["phonenumber"] . "</phonenumber>";
echo "<email>" . $row["email"] . "</email>";
echo "<website>" . $row["website"] . "</website>";
echo "<logo>" . $row["logo"] . "</logo>";
echo "<description>" . $row["description"] . "</description>";
echo "<datesubmitted>" . $row["datesubmitted"] . "</datesubmitted></business>";
}
echo "</map>";
}
Can anybody help at all?
You are using mysqli for running the statement, then trying to process the result as mysql? Yeah, that's not going to work.
Use functions from the same extension! They are not cross-compatible.
You should use fetch_array() instead of mysql_fetch_assoc()

PHP While loop, echo Issues

There has to be a easier way...
I keep getting this for the second line.
Parse error: syntax error, unexpected ';'
while($row = mysql_fetch_array($result)){
echo ("'$MAP_OBJECT->addMarkerByCoords";
"(\"";
$row['longitude'];
",";
$row['latitude'];
",\"";
$row['routername'];
"-";
$row['desc'];
"\", \"";
$row['routername'];
"-";
$row['desc'];
"<br><a href=\"./div/";
$row['routername'];
"\">Site Info</a>'");
echo "<br />";
}
You have to combine with a . and not with ;
echo ("'$MAP_OBJECT->addMarkerByCoords" .
"(\"" .
$row['longitude'] .
....
Have a look into the manual: http://php.net/manual/en/language.operators.string.php
Most of the ; should be . if you are attempting to concatenate these strings:
while($row = mysql_fetch_array($result)){
echo ("'$MAP_OBJECT->addMarkerByCoords" .
"(\"" .
$row['longitude'] .
"," .
$row['latitude'] .
",\"" .
$row['routername'] .
"-" .
$row['desc'] .
"\", \"" .
$row['routername'] .
"-" .
$row['desc'] .
"<br><a href=\"./Ldiv/" .
$row['routername'].
"\">Site Info</a>'"); // Here's the actual end of the statement
echo "<br />";
}
This woudl be a lot tidier with a HEREDOC:
echo <<<ROW
$MAP_OBJECT->addMarkerByCoords(
{$row['longitude']},
{$row['latitude']},
"{$row['routername']}-{$row['desc']}",
"$row['routername']}-{$row['desc']}"<br>
Site Info
)
ROW;
Although , it looks like something is missing before the <br> since the previous quote doesn't get closed.
Your syntax is wrong for what you are trying to execute.
The ; in PHP is an end statement, essentially. You are telling PHP to stop executing the echo on the very first line echo ("'$MAP_OBJECT->addMarkerByCoords" ; which is NOT what you want.
Instead, replace all the ; with .'s except for the last echo statement
while($row = mysql_fetch_array($result)){
echo "'$MAP_OBJECT->addMarkerByCoords" .
"(\"" .
$row['longitude'] .
"," .
$row['latitude'] .
",\"" .
$row['routername'] .
"-" .
$row['desc'] .
"\", \"" .
$row['routername'] .
"-" .
$row['desc'] .
"<br><a href=\"./Ldiv/" .
$row['routername']; .
"\">Site Info</a>'";
echo "<br />";
}

Categories