Using a GET Id from mysql query - php

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.

Related

wpdb->insert producing duplicates

I am trying to make a button on a page that prints out data from the database and then you can press 2 different buttons, one that deletes them from the database and the other one inserts it into another table in the database and deletes the data from the database, but it keeps inserting it twice into the new table and I have no clue why, this here prints out the data and session variables + buttons:
if(!isset($_POST['orderby'])) {
foreach ($requests as $row) {
echo "<div class='requests'>" . "<li class='refunds'>" . "Palauttajan nimi: ".
$row['customer_name'] . "</br>" ."Palautettavat tuotteet: ".$row['product_name']."<br> "."Määrä: ".
$row['product_qty'] . " "
. "<br>Kommentti: " . $row['comment'] . "<br> " . "Hinta: " . $row['refund_total'] . "€ " .
"<br>" . "Päivämäärä: " . $row['request_date'] . " " .
"<a class='right' href='admin-page?deleteid=" . $row['request_id'] . "'>Hylkää</a></li>" .
"<li class='refundaccepts'><a href='admin-page?acceptid=" . $row['request_id']
. "'>Hyväksy</a></li>" . "</div>";
$_SESSION['custname'] = $row['customer_name'];
$_SESSION['prodname'] = $row['product_name'];
}
} else {
foreach ($pergele as $row) {
echo "<div class='requests'>" . "<li class='refunds2'>" . "Palauttajan nimi: ".
$row['customer_name'] . "</br>" ."Palautettavat tuotteet: ".$row['product_name']."<br> "."Määrä: ".
$row['product_qty'] . " "
. "<br>Kommentti: " . $row['comment'] . "<br> " . "Hinta: " . $row['refund_total'] . "€ " .
"<br>" . "Päivämäärä: " . $row['request_date'] . " " .
"<a class='right' href='admin-page?deleteid=" . $row['request_id'] . "'>Hylkää</a></li>" .
"<li class='refundaccepts'><a href='admin-page?acceptid=" . $row['request_id']
. "'>Hyväksy</a></li>" . "</div>";
$_SESSION['custname'] = $row['customer_name'];
$_SESSION['prodname'] = $row['product_name'];
}
}
and this should insert it into the database once and delete the data from the old table:
if(isset($_GET['acceptid'])) {
$accept = $_GET['acceptid'];
$custname = $_SESSION['custname'];
$prodname = $_SESSION['prodname'];
/* Query to do whatever here */
$wpdb->insert("wp_acceptedrequests", [
"customer_name" => "$custname",
"name_product" => "$prodname",
"date" => date("Y/m/d/G:i:sa") ,
]);
$wpdb->query("DELETE FROM wp_refundrequests WHERE request_id = $accept");
}
What makes them insert twice and how do I prevent it from doing that?
I just ran into a similar situation where $wpdb inserts where being duplicated.
In my case it was happening if I was authenticated and browser inspector was open.

URL Link from HTML form to PHP page

I have read a lot of submissions, I haven't been able to find an answer to my exact question.
I am filling out an HTML form that is sent into mysql database - I am good there.
The results are displayed using php in a html table. I would like the website URL to show as a hyperlink. Everything I do results in an error.
Does anyone know how I can update my code to allow for the website url to show as a hyperlink?
[$dbname = "seller";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT website, url, price, name, email FROM listing";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table><tr><th>Website</th><th>URL</th><th>Price</th><th>Contact Name</th><th>Email</th></tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["website"]. "</td><td>" . $row["url"]. " </td><td>" . $row["price"]. "</td><td>" . $row["name"]. "</td><td> " . $row["email"]. "</td></tr>";
}
echo "</table>";
} else {
echo "0 results";
}
$conn->close();
?>]
Thanks
Have you tried this...
echo "<tr><td></td><td><a href='" . $row['url']. "'>" . $row['website']. "</a> </td><td>" . $row["price"]. "</td><td>" . $row["name"]. "</td><td> " . $row["email"]. "</td></tr>";
You could also just change 'Link to website' to " . $row["website"]. " for it to display the link
If you use an a tag it should work.
URL
echo "<tr><td><a href=\"{$row['url']}>{$row['website']}</a></td>...etc
You should also consider closing the tags to get a cleaner code:
<?php if ($condition) { ?>
<div>HTML code!</a>
<?php } ?>
Add an anchor tag <a href=""> inside the table data tag <td> inside your while loop.
Change this line:
echo "<tr><td>".$row["website"]."</td><td>".$row["url"]." </td><td>".$row["price"]."</td><td>".$row["name"]."</td><td> ".$row["email"]."</td></tr>";
Into this:
echo "<tr><td><a href='".$row["website"]."'>".$row["website"]."</a></td><td>".$row["url"]." </td><td>".$row["price"]."</td><td>".$row["name"]."</td><td> ".$row["email"]."</td></tr>";
That will be great if you can show me the error otherwise
Try this:
while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["website"]. "</td><td><a href='".$row["url"]."'>Link</a> </td><td>" . $row["price"]. "</td><td>" . $row["name"]. "</td><td> " . $row["email"]. "</td></tr>";
}
use this
echo '<tr><td>' . $row["website"]. '</td><td>'; echo $row["name"]; echo' </td><td>' . $row["price"]. '</td><td>' . '</td><td> ' . $row["email"]. '</td></tr>';
instead of
echo "<tr><td>" . $row["website"]. "</td><td>" . $row["url"]. " </td><td>" . $row["price"]. "</td><td>" . $row["name"]. "</td><td> " . $row["email"]. "</td></tr>";
use <a> tag, please see this link.
'' . $row['url'] . ''

PHP pull from two MySQL tables, where multiple rows in table two

I have two tables that looks like this:
Put it in images since i dont know how to draw a table here.
My problem is that i can't seem to make a query or anything in my php that will allow me to load the report and the 5 images, so that i can display the images where i want to on the page. As i do it now it loads the report five times and one image in each report.
The edited code after #Terminus suggestions
$sql = "
SELECT *
FROM fangstrapporter AS f, rapportbilleder AS r
WHERE f.id=".htmlspecialchars($_GET["id"])." AND r.id=f.id";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$row = $result->fetch_assoc();
echo "<b>Overskrift:</b> " . $row["overskrift"] . "<br><br>" .
"<b>Sted:</b> " . $row["sted"] . "<br><br>" .
"<b>Fangstdato:</b> " . $row["fangstdato"] . "<br><br>" .
"<b>Agn:</b> " . $row["agn"] . "<br><br>" .
"<b>Kategori:</b> " . $row["kategori"] . "<br><br>" .
"<b>Art:</b> " . $row["art"] . "<br><br>" .
"<b>Vægt:</b> " . $row["vaegt"] . "<br><br>" .
"<b>Længde:</b> " . $row["laengde"] . "<br><br>";
do {
echo "<a href='" . $row["image_path"] . "'><img src='" . $row["image_thumb_path"] . "'></a><br><br>";
} while($row = $result->fetch_assoc());
echo $row["beskrivelse"]."<br>";
} else {
echo "0 results";
}
Can anyone help me do this? I have been searching on google for four days now, without any success.
Do as #Kenney suggested and remove the part where you echo the report from the loop.
$sql = "
SELECT *
FROM fangstrapporter AS f, rapportbilleder AS r
WHERE f.id=".htmlspecialchars($_GET["id"])." AND r.id=f.id";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$row = $result->fetch_assoc();
echo "<b>Overskrift:</b> " . $row["overskrift"] . "<br><br>" .
"<b>Sted:</b> " . $row["sted"] . "<br><br>" .
"<b>Fangstdato:</b> " . $row["fangstdato"] . "<br><br>" .
"<b>Agn:</b> " . $row["agn"] . "<br><br>" .
"<b>Kategori:</b> " . $row["kategori"] . "<br><br>" .
"<b>Art:</b> " . $row["art"] . "<br><br>" .
"<b>Vægt:</b> " . $row["vaegt"] . "<br><br>" .
"<b>Længde:</b> " . $row["laengde"] . "<br><br>";
$beskrivelse = $row["beskrivelse"];
do {
echo "<a href='" . $row["image_path"] . "'><img src='" . $row["image_thumb_path"] . "'></a><br><br>";
} while($row = $result->fetch_assoc());
echo $beskrivelse . "<br>";
} else {
echo "0 results";
}

Really Easy PHP query. Displaying from separate PHP file

I'm a PHP n00b. I have this code, however it is on my main page. I assume like jQuery there must be a way to display this without having the actual script on the main document that the users access?
Or is there a way I can do this tidier without echo'ing everything?
include('talentsearch.php');
while($row = mysqli_fetch_array($result2))
{
echo "<div class='talent_result_wrapper' data-experience='" . $row['divTagExp'] . "' data-salary='" . $row['divTagExp'] . "'>";
echo "<ul>";
echo "<li><strong>Talent ID: </strong>" . $row['CandidateID'] . "</li>";
echo "<li><strong>Resides: </strong>" . $row['Town'] . "</li>";
echo "<li><strong>Salary Required: </strong>£" . $row['SalaryMin'] . "</li>";
echo "<li><strong>Experience: </strong>" . $row['CandidateExperience'] . " Years </li>";
echo "<li><strong>Industy: </strong>" . $row['PrimarySector'] . "</li>";
echo "<li><strong>Specialism: </strong>" . $row['PrimarySector'] . "</li>";
echo "</div>";
}
I have tried putting it in a function like this:
function getTalent() {
}
And then calling the function like this
<?php
include('talentsearch.php');
getTalent()
?>
But I get an error whereas I don't if I just run the code normally.
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in
So I assume I'm not doing it right.
is this what you are trying to achive?
talentsearch.php file
function getTalent($row) {
return "<div class='talent_result_wrapper' data-experience='" . $row['divTagExp'] . "' data-salary='" . $row['divTagExp'] . "'>
<ul>
<li><strong>Talent ID: </strong>" . $row['CandidateID'] . "</li>
<li><strong>Resides: </strong>" . $row['Town'] . "</li>
<li><strong>Salary Required: </strong>£" . $row['SalaryMin'] . "</li>
<li><strong>Experience: </strong>" . $row['CandidateExperience'] . " Years </li>
<li><strong>Industy: </strong>" . $row['PrimarySector'] . "</li>
<li><strong>Specialism: </strong>" . $row['PrimarySector'] . "</li>
</div>";
}
///////
main.php file
include('talentsearch.php');
while($row = mysqli_fetch_array($result2)) {
echo getTalent($row);
}

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