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'] . ''
Related
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.
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";
}
I am trying to make a php script which will get clients data from database in a table. the script works fine and is getting database fields correctly. Now i want to add a link beside every client info. The link should be like www.domain.com/check?clientid=$id&number=$phonenumber
How should i do this?
its a table and clients details are arranged in order. Please help me?
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, firstname, lastname, phonenumber, city, country, email FROM clients ORDER BY ID ASC";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table><tr><th>ID</th><th>Name</th><th>Phone Number</th><th>Country</th><th>Email</th><th>Send SMS</th></tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["id"]. "</td><td>" . $row["firstname"]. " " . $row["lastname"]. "</td><td>" . $row["phonenumber"]. "</td><td>" . $row["city"]. " "
. $row["country"]. "</td><td>" . $row["email"]. "</td></tr>";
}
echo "</table>";
} else {
echo "0 results";
}
$conn->close();
?>
I agree with Marcosh (in the comments), so that would give you this code (between the if(...) { and } else):
echo "<table><tr><th>ID</th><th>Name</th><th>Phone Number</th><th>Country</th><th>Email</th><th>Send SMS</th><th>Link to page</th></tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["id"]. "</td><td>" . $row["firstname"]. " " . $row["lastname"]."</td><td>" . $row["phonenumber"]. "</td><td>" . $row["city"]. " ". $row["country"]. "</td><td>" . $row["email"]. "</td><td>Click me!</td></tr>";
}
echo "</table>";
Note 1: This will add a link with the text "Click me!". I think you may want to change that. You may also want to change the header (currently "Link to page")
Note 2: If you use php 5.5 or later, you can also use
echo "blablabla<td>{$row['id']}</td>etc..."
instead of
echo "blablabla<td>" . $row['id'] . "</td>etc..."
Thanks for help #ivo and i have learned how to get this thing done:
<td><form action='https://control.domain.com/api/sendhttp.php' method='GET'><input type='hidden' name='authkey' value='74685AiSz3dkBX5467773f'><input type='hidden' name='mobiles' value='" . $row['phonenumber'] . "'><input type='hidden' name='sender' value='hostbi'><input type='hidden' name='route' value='4'><textarea name='message' cols='55'>Leave a message</textarea><input type='submit' value='Click me!'></form></td>
I have read through several questions on stackoverflow before asking this. I have very little knowledge on php and mysql so much help is needed.
So, what I needed is to create a button for every table row so that when user hits the "copy" button, the data for that row will be copied into the database. How can I do this?
demotable.php (//EDIT//)
<?php
require('connect.php');
$query = "SELECT * FROM trade_history1 ";
$result = mysql_query($query);
echo "<table border = '1px'>"; // start a table tag in the HTML
echo "<tr><td>" . "ID" . "</td><td>" . "Date" . "</td><td>" . "Type" . "</td><td>" . "Size" . "</td><td>" . "Currency Pair" . "</td><td>" . "Entry" . "</td><td>" . "Stoploss" . "</td><td>". "Take Profit" . "</td><td>" . "Date Close" . "</td><td>" ."Close" . "</td><td>" ."Profit/Loss"."</td><td>" ."Copy"."</td></tr>" ; //$row['index'] the index here is a field name
while($row = mysql_fetch_array($result)){ //Creates a loop to loop through results
echo "<tr><td>" . $row['id'] . "</td><td>" . $row['date'] . "</td><td>" . $row['type'] . "</td><td>" . $row['size'] ."</td><td>" . $row['currency_pair'] ."</td><td>" . $row['entry'] ."</td><td>" . $row['stoploss'] ."</td><td>" . $row['takeprofit'] ."</td><td>" . $row['dateclose'] ."</td><td>" . $row['close'] ."</td><td>" . $row['profitloss'] . "</td></tr>"; //$row['index'] the index here is a field name
}
echo "</table>"; //Close the table in HTML
mysql_close(); //Make sure to close out the database connection
?>
<html>
View
</html>
copytrade.php
<?php
require ('connect.php');
$mysqli = new mysqli($database_hostname, $database_username, $database_password, $database_name) or exit("Error connecting to database");
$stmt = $mysqli->prepare("INSERT INTO trade_history1 (size, date, type, currency_pair, entry, stoploss, takeprofit, dateclose,close,profitloss)
SELECT size, date, type, currency_pair, entry, stoploss, takeprofit, dateclose,close,profitloss
FROM trade_history1
WHERE id = ?");
$stmt->bind_param("i", $id); //
$successfullyCopied = $stmt->execute();
$stmt->close();
$mysqli->close();
?>
By the way, whenever I click the "copy button" on demotable.php, the link will be this: http://localhost/1103242B/demo/copytrade.php?copy=copy. May I know which part of the code did I missed out or did I do wrongly? Thanks.
You have to put all your table design in the form tag. And then for each row you have to add a copy link with url something like http://localhost/1103242B/demo/copytrade.php?id=id. Here id is the record id from database. You are generating the table above and then in below form you don't have any id reference to copytrade.php page.
That way also you can do that but in each row you can put some checkbox and then when user clicks on checkbox set the id in a hidden field inside the form and then you can able to post that id to copytrade.php.
Both way it will work.
Try to edit your page like below.
<html>
<form method = "GET" action = "copytrade.php">
<?php
require('connect.php');
$query = "SELECT * FROM trade_history1 "; //You don't need a ; like you do in SQL
$result = mysql_query($query);
echo "<table border = '1px'>"; // start a table tag in the HTML
echo "<tr><td>" . "ID" . "</td><td>" . "Date" . "</td><td>" . "Type" . "</td><td>" . "Size" . "</td><td>" . "Currency Pair" . "</td><td>" . "Entry" . "</td><td>" . "Stoploss" . "</td><td>". "Take Profit" . "</td><td>" . "Date Close" . "</td><td>" ."Close" . "</td><td>" ."Profit/Loss"."</td><td>" ."Copy"."</td><td>Copy</td></tr>" ; //$row['index'] the index here is a field name
while($row = mysql_fetch_array($result)){ //Creates a loop to loop through results
echo "<tr><td>" . $row['id'] . "</td><td>" . $row['date'] . "</td><td>" . $row['type'] . "</td><td>" . $row['size'] ."</td><td>" . $row['currency_pair'] ."</td><td>" . $row['entry'] ."</td><td>" . $row['stoploss'] ."</td><td>" . $row['takeprofit'] ."</td><td>" . $row['dateclose'] ."</td><td>" . $row['close'] ."</td><td>" . $row['profitloss'] . "</td><td>a href='copytrade.php?id=" .$row['id'].'">copy</a></td></tr>"; //$row['index'] the index here is a field name
}
echo "</table>"; //Close the table in HTML
mysql_close(); //Make sure to close out the database connection
?>
<input type = "submit" name = "copy" value = "copy"/></form>
</html>
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.