passing parameter from php table row - php

Is it possible to pass a name_id parameter through the href via a row in a php table?
Sample Code:
<?php
$name = $_GET["name"];
$con = mysqli_connect("", "", "", "");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con, "SELECT * FROM table WHERE name LIKE '%$name%' ");
echo "<table class='table table-condensed'>
<tr>
<th> name id </th>
<th> name </th>
<th> link </th>
</tr>";
while ($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['name_id'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . "<a href='name.php?name_id=$name_id'>view</a>" . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
Eg. Multiple names with an auto incremented id is in the table and I would like it to open the name.php page where I can GET the name_id from the url?
Thanks in advance.

This worked for me.
echo "<td>" . "<a href='name.php?name_id=" . $row['name_id'] ."'>". $row['name_id'] . "</a>" . "</td>";
Hope this helps someone!

I think that this will be the simplest solution
echo "<td>" . "<a href='name.php?name_id=" . $row['name_id'] . "'>view</a>" . "</td>";

Related

How to compare values in php database

From the table image below:
Is there any ways that I used nodeID 101 as my indicator. Then I want to retrieve battery value of Node102 which is lower than the battery value of NodeID101... Same goes to node103, it will also compare with node101 whereby if its battery value lower than node101, result is displayed.
Thanks for your guides!
<?php
$username="root";
$password="";
$database="testing";
mysql_connect('localhost',$username,$password);
#mysql_select_db($database) or die("Unable to select database");
$query = "SELECT * FROM monitoring WHERE Battery < 2.7";
$result = mysql_query($query);
echo "<b><center>Database Output</center></b><br><br>";
echo "<table border='1'>
<tr>
<th>PC_Timestamp </th>
<th>NodeID </th>
<th>PacketID </th>
<th>RSSI </th>
<th>Node_Timestamp </th>
<th>Temperature </th>
<th>Battery </th>
</tr>";
while($row = mysql_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['PC_Timestamp'] . "</td>";
echo "<td>" . $row['NodeID'] . "</td>";
echo "<td>" . $row['PacketID'] . "</td>";
echo "<td>" . $row['RSSI'] . "</td>";
echo "<td>" . $row['Node_Timestamp'] . "</td>";
echo "<td>" . $row['Temperature'] . "</td>";
echo "<td>" . $row['Battery'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close();
?>

Displaying data from database in PHP, first and last name going into same column

I'm trying to display members from my database using PHP, however the last name goes in with the first name and I'm not quite sure why... Could anyone point me in the right direction?
<?php
$mysql_db_hostname = "localhost";
$mysql_db_user = "alex";
$mysql_db_password="";
$mysql_db_database="gym";
$con = mysql_connect($mysql_db_hostname, $mysql_db_user, $mysql_db_password) or die("Could not connect database");
mysql_select_db($mysql_db_database, $con) or die("Could not select database");
$query = mysql_query("select * from users WHERE Category='Member'");
echo "<table border=1>
<tr>
<th>Users ID</th>
<th>First Name</th>
<th>Last Name</th>
</tr>";
while($row =mysql_fetch_assoc($query))
{
echo "<tr>";
echo "<td>" . $row['user_id']."<br>" . "</td>";
echo "<td>" . $row['First_Name']."<br>" . "</td";
echo "<td>" . $row['Last_Name']."<br>" . "</td";
echo "</tr>";
}
echo "</table";
?>
You aren't closing the tags. Syntax highlighter would show the error.
while($row =mysql_fetch_assoc($query))
{
echo "<tr>";
echo "<td>" . $row['user_id']."<br>" . "</td>";
echo "<td>" . $row['First_Name']."<br>" . "</td"; <-----------
echo "<td>" . $row['Last_Name']."<br>" . "</td"; <------------
echo "</tr>";
}
You are not closing your <td> tags properly
echo "<td>" . $row['First_Name']."<br>" . "</td>";
//^here
echo "<td>" . $row['Last_Name']."<br>" . "</td>";
//^and here
So the output is all one cell with both variables printed inside
You forgot to close the td on your first name field.
Just change
</td
to
</td>

Table ID in PHP

I'm trying to assign anID to the table I'm generating with PHP but it keeps returning an error. Here is the full code that works fine so far. All I want is to add an 'id' to the table so I can apply css styles to it in the relevant sheet
<?php
$con=mysqli_connect("localhost","<un>","<pw>","monitor");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM presnationalresults ORDER BY Percentage DESC");
echo "<table border='1'>
<tr>
<th>President</th>
<th>Party</th>
<th>Votes</th>
<th>Percentage</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['PresidentName'] . "</td>";
echo "<td>" . $row['PartyCode'] . "</td>";
echo "<td>" . $row['Votes'] . "</td>";
echo "<td>" . $row['Percentage'] . "</td>";
}
echo "</table>";
mysqli_close($con);
Any help? maybe I'm going about it the wrong way?
Please try below block of code . I have added table id and also close </tr> inside while loop
<?php
$con = mysqli_connect("localhost", "<un>", "<pw>", "monitor");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con, "SELECT * FROM presnationalresults ORDER BY Percentage DESC");
echo "<table border='1' id='table-id'>
<tr>
<th>President</th>
<th>Party</th>
<th>Votes</th>
<th>Percentage</th>
</tr>";
while ($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['PresidentName'] . "</td>";
echo "<td>" . $row['PartyCode'] . "</td>";
echo "<td>" . $row['Votes'] . "</td>";
echo "<td>" . $row['Percentage'] . "</td>";
echo "</tr>"; // you forget close tr
}
echo "</table>";
mysqli_close($con);

Tables not showing in HTML database

I am creating a database and when creating the HTML script the table is coming back with no boarders = Firstname Lastname ";
while($row = mysqli_fetch_array($result)) {
echo "";
echo "" . $row['Firstname'] . "";
echo "" . $row['Lastname'] . "";
echo "";
}
echo "";
mysqli_close($con); ?>
Here is the code I created, could someone look at it and tell me what I have done wrong?
<?php
$con=mysqli_connect("localhost","root","","Franchise_Call_Log");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM caller_info");
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
th>Franchise</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['Firstname'] . "</td>";
echo "<td>" . $row['Lastname'] . "</td>";
echo "<td>" . $row['Franchise'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
Look at the tag
<th>Franchise</th>
Please change your file extension to .php it is not rendering your script. Html file not render your php, and also correct your markup as suggested by #suhail.

Add SQL data to URL with PHP

I have some basic code which selects data from a database and echo's it into a HTML table and also add's a link to the echo'd out data. This all works fine. What I want to do is add another piece of data from my database (product_id) to the URL. Here is the code:
<?php
$con=mysqli_connect("localhost","root","","db_test");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM tbl_products");
echo "<table border='1'>
<tr>
<th>Products:</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td><a href='product.php?id='>" . $row['product_name'] . "</a></td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
I want to add product_id from my database at the end of
<a href='product.php?id='
so the product_id becomes the ID of the page. How would I do this? I have experimented but it has resulted in numerous errors. I am sure this is a simple syntax thing but it' s bugging me.
You are closing the tag A before placing your data into href.
I think it should work:
echo "<table border='1'>
<tr>
<th>Products:</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['product_name'] . "</td>";
echo "</tr>";
}
echo "</table>";
Answer back if it doesn't work.
The problem is you should put the '> after " . $row['product_name'] . " so that the line would read:
echo "<td><a href='product.php?id=" . $row['product_name'] . "'>" . $row['product_name'] . "</a></td>";

Categories