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>
Related
Really stuck on this one. Spent a couple hours fiddling and no results so asking here...
Tried solutions from html/CSS to Javascript and jQuery, but couldn't get anything to work. Basically trying to accomplish freeze panes like in Excel with the below table. For this I am most interested in freezing the first 4 columns. I might be interested in freezing the first 'header' column too, but not as important.
This is an example, but I will likely use this is a situation where I have multiple tables on a single page (so thinking if we had a frozen header it would need to stop at the end of each table).
<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
border: 1px solid black;
}
</style>
</head>
<?php
$servername = "";
$username = "";
$password = "";
$dbname = "";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "
SELECT fac_id, abc_id, provider_id, fac_name, status, lob, service_level, rate, start_date, end_date, fiscal_month_end, month_start, region, area, bd, pd, phone_pd, ps, total_beds, licensed_beds, address, city, state, zip, phone_office, logo_1, logo_2
FROM table_db
WHERE status = 'ACTIVE' AND lob = '123' AND area = 'Smith'
ORDER BY region ASC, area ASC, fac_name ASC
";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table cellpadding=5><tr><th>Fac ID</th><th>ABC ID</th><th>Provider ID</th><th>Fac Name</th><th>Service Level</th><th>Rate</th><th>Start Date</th><th>Fiscal Month End</th><th>Month Start</th><th>Region</th><th>Area</th><th>BD</th><th>PS</th><th>PD</th><th>PD Phone</th><th>Total Beds</th><th>Licensed Beds</th><th>Address</th><th>City</th><th>State</th><th>Zip</th><th>Office Phone</th><th>Logo 1</th><th>Logo 2</th></tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["fac_id"]. "</td><td>" . $row["abc_id"]. "</td><td>" . $row["provider_id"]. "</td><td>" . $row["fac_name"]. "</td><td>" . $row["service_level"]. "</td><td>" . $row["rate"]. "</td><td>" . $row["start_date"]. "</td><td>" . $row["fiscal_month_end"]. "</td><td>" . $row["month_start"]. "</td><td>" . $row["region"]. "</td><td>" . $row["area"]. "</td><td>" . $row["bd"]. "</td><td>" . $row["ps"]. "</td><td>" . $row["pd"]. "</td><td>" . $row["phone_pd"]. "</td><td>" . $row["total_beds"]. "</td><td>" . $row["licensed_beds"]. "</td><td>" . $row["address"]. "</td><td>" . $row["city"]. "</td><td>" . $row["state"]. "</td><td>" . $row["zip"]. "</td><td>" . $row["phone_office"]. "</td><td><a href='/logo/" . $row["logo_1"]. "'>" . $row["logo_1"]. "</a></td><td><a href='/logo/" . $row["logo_2"]. "'>" . $row["logo_2"]. "</a></td></tr>";
}
echo "</table>";
} else {
echo "0 results";
}
$conn->close();
?>
</body>
</html>
I'm learning PHP right now and connecting to MySQL. I'm able to connect to a table called "info" and display the "date", "time", and "data" of each row, along with 2 extra columns that will allow the user to download the respective "data" into a txt and csv. This is where I'm completely lost on how to achieve that.
My understanding is that since it'll be a clickable link, I will have to use "href=" and will link that to a separate php that I create that will download that row's data, right? But then how does that new php file know which row to download from? I'm guessing I need to pass the row number it is to the other file?
Greatly appreciate it if anyone can lead me in the right direction or have examples. Thanks!
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT date, time, data FROM info";
$result = $conn->query($sql);
echo "<table border='1'>";
echo "<tr><td> Date </td><td>Time</td><td>Data</td><td> Download text </td><td> Download csv</td>";
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td>" . $row["date"]. "</td><td>" . $row["time"]. "</td><td>" .$row["data"] ."</td><td><a href='NEEDTXTFILE.php'>".$row["date"].'.txt'. "</a></td>"."<td><a href='NEEDCSVFILE.php'>".$row["date"].'.csv'. "</a></td> ";
echo "</tr>";
}
echo "</tr>";
echo "</table>";
} else {
echo "0 results";
}
Assuming you select also the id value
$sql = "SELECT id, date, time, data FROM info";
....
you could add the id to your href eg:
while($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td>" . $row["date"]. "</td><td>" .
$row["time"]. "</td><td>" .
$row["data"] .
"</td><td><a href='NEEDTXTFILE.php?id=" . $row["id"] . "'>".
$row["date"].'.txt'. "</a></td>"."<td><a href='NEEDCSVFILE.php'>".
$row["date"].'.csv'. "</a></td> ";
echo "</tr>";
}
then in your NEEDTXTFILE.php you can obtain the id in $_GET['id']
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'] . ''
How do you update only one row when you fetch for multiple rows? I am lost on how to do this. I can fetch for one row, update and it works. However, I'm stuck for multiple rows.
Down below is my code
FETCH PHP:
//loop through for results
while($row = mysqli_fetch_array($data)){
echo "<tr><td>"
. $row['title']."<input type='hidden' value='".$row['title']."' name='title'>"
. "</td><td>"
. $row['author']
. "</td><td>"
. $row['summary']
. "</td><td>"
. $row['isbn']
. "</td><td>"
. "<img src='books/".$row['image']."' width='100%'/>"
. "</td><td>"
. "$<input type='text' value='".$row['price']."' size='8' class='pricetoright' name='price'>"
. "</td><td>"
. "<button type='submit' class='btn btn-default btngreen' name='update'>Update</button>"
. "</td><td>"
. "<button type='submit' class='btn btn-default btnred' name='delete'>Delete</button>"
. "</td></tr>";
}
UPDATE PHP:
// Check Connection
if(!($db = mysqli_connect($server, $user, $password, $database))) {
die('SQL ERROR: Connection failed: '.mysqli_error($db));
}
$price = floatval($_POST["price"]);
$title = $_REQUEST["title"];
// SQL to update a record
$query = "UPDATE books SET price = '$price' WHERE title = '$title'";
if (mysqli_query($db, $query)) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . mysqli_error($db);
}
mysqli_close($db);
Use form tag for each row. Also its better to use unique id to update row instead of title. So take hidden input of id.
You'd want to add a foreach() statement into your code. While it's a little unclear on your code here's a possible example:
foreach($_POST as $k=>$v){
if($k === "title"){
$title = $_POST["title"];
}
if($k === "price"){
$price = floatval($_POST["price"]);
}
// SQL to update a record
$query = "UPDATE books SET price = '$price' WHERE title = '$title'";
if (mysqli_query($db, $query)) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . mysqli_error($db);
}
}
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>