I'm having trouble selecting data from inside one of the tables in the database.
I'm trying to display a user's profile when their name is clicked on.
<?php
$view = mysql_fetch_array(mysql_query("select * from users where id=$view"));
if (empty ($view[id])) {
print "No such user.";
exit;
}
print "<center><b><u>$view[user]</b></u> ($view[id])</center><br>";
print "Rank: $view[rank]<br>";
?>
The link they will click is:
viewpage.php?page_id=4&view=....
I can't get this to work.
<?php
$id=$_GET["view"];
$query = mysql_query("SELECT * FROM users WHERE id=$id");
if($query)
{
echo '<table>';
echo '<tr><th>Name</th><th>ID</th><th>Something Else</th></tr>';
while($view = mysql_fetch_assoc($query))
{
echo '<tr>';
echo '<td>'.$view['Name'].'</td>';
echo '<td>'.$view['id'].'</td>';
echo '<td>'.$view['some other variable in your data'].'</td>';
echo '</tr>'; //This one goes after you've echoed all the data for one user.
}
echo '</table>';
} else {
echo 'no user found';
}
?>
Related
For a school project, I have to make a webshop with PHP and use a database to search for your products, I have the code to display the results, however, I want to make a link, so that when you click on one of the search results, you go to that product's page.
I've tried looking online but I couldn't seem to find it anywhere, that's why I'm posting this question.
$sql = "SELECT ProductID, ProductTags, ProductName FROM producttabel";
$result = $mysqli->query($sql);
if ($result->num_rows > 0) {
// output data of each row
echo '<div class="allepccskop">';
echo "Onze producten: " . "<br>";
echo '</div>';
while($row = $result->fetch_assoc()) {
echo '<div class="allepccs">';
echo $row["ProductName"]. "<br>";
echo '</div>';
}
} else {
echo "0 results";
}
I want to make a link when you click on one of the search results, you go to that product's page.
To make a link to a product page, you need to use html a tag, just wrap it around your product name like this:
echo '' . $row["ProductName"]. "<br>";
The href attribute contains your php file name (e.g. index.php) and a GET parameter id to send the product id to the php page.
Full example:
Assuming you want to extend your code to display a single product when a product id (ProductID) is provided or all products otherwise.
This is a simple example how you could extend your code, look at the comments:
<?php
// take id from the request, otherwise set default to null
$productId = isset($_GET['id']) ? intval($_GET['id']) : null;
// when we have an id in the url, then we display a product page
if (!is_null($productId)) {
$sql = 'SELECT ProductID, ProductTags, ProductName FROM producttabel WHERE ProductID = ' . $productId;
$result = $mysqli->query($sql);
if ($result && $result->num_rows == 1) {
$row = $result->fetch_assoc();
$result->free(); // free result set
// output data of each row
echo '<div class="allepccskop">';
echo "Product page: #" . $productId . "<br>";
echo '</div>';
echo '<div class="allepccs">';
echo $row["ProductName"]. "<br>";
echo '</div>';
} else {
echo "Product not found";
}
// otherwise we show All products page
} else {
// your code
$sql = 'SELECT ProductID, ProductTags, ProductName FROM producttabel';
$result = $mysqli->query($sql);
if ($result && $result->num_rows > 0) {
// output data of each row
echo '<div class="allepccskop">';
echo "Onze producten: " . "<br>";
echo '</div>';
while ($row = $result->fetch_assoc()) {
echo '<div class="allepccs">';
echo '' . $row["ProductName"]. "<br>";
echo '</div>';
}
$result->free(); // free result set
} else {
echo "0 results";
}
}
I am using a class that fetches information from a database and shows that information in a table, along with an edit and delete button. When I click the edit or delete button, I want to add $_GET['id'] to the link, but I am confused on how to accomplish this.
public function select($tName,$from,$where=1){
$key=[];
$con=$this->con;
$sql="SELECT $tName FROM $from WHERE $where";
$data = $con->query($sql);
$fullRec=[];
foreach($data as $k=>$rows){
$fullRec[]=$rows;
}
foreach($rows as $k=>$v){
$key[] = $k;
}
//create table
echo "<table border='1'>";
//create one row with col name selected!!
echo "<tr>";
for($z=0;$z<count($key);$z+=2){
echo "<td>".$key[$z]."</td>";
}
echo "<td>actions</td>";
echo "</tr>";
//create one row for each record comes!!
for($i=0;$i<count($fullRec);$i++){
echo "<tr>";
//create one table data for each record comes!!
for($j=0;$j<count($fullRec[$i])/2;$j++){
echo "<td>".$fullRec[$i][$j]."</td>";
}
echo "<td>EditDelete</td>";
echo "</tr>";
}
echo "</table>";
}
use like this to pass the id of the record in get parameter
echo "<a href='your_route?del=".$rows['id']."'>DELETE</a>";
PHP form is not displaying data from MySQL. I have a simple 4 text area form that I would like the data in the specific table rows to display in their respective area.
The first display area does display the data from table and specific row
ex: ( I have attempted to use the endwhile: but I get an error
<?php
//Create Select Query
$query = "SELECT * FROM worksheet";
$data = mysqli_query($con, $query);
?>
Area 1 - Does display data in area 1 of form
<?php while($row1 = mysqli_fetch_assoc($data)) {
echo "\r";
echo $row1['u_i'] ;
echo "\r";
}
?>
Area 2 does not display data from MySQL
<?php while($row2 = mysqli_fetch_assoc($data)) {
echo "\r";
echo $row2['u_ni'] ;
echo "\r";
}
?>
Area3 does not display data from MySQL
<?php while($row3 = mysqli_fetch_assoc($data)) {
echo "\r";
echo $row3['nu_i'];
echo "\r";
}
?>
Area4 does not display data from MySQL
<?php while($row4 = mysqli_fetch_assoc($data)) {
echo "\r";
echo $row4['nu_i'];
echo "\r";
}
?>
Someone please show me the way. Thanks
1st you have to push result into array.Then you can display the anywhere as you wish.
$query = "SELECT * FROM FROM worksheet";
$data_arr=array();
if ($result = $mysqli->query($query)) {
/* fetch object array */
while ($obj = $result->fetch_object()) {
//push elements into array
array_push($data_arr,$obj->field_name)
}
/* free result set */
$result->close();
}
Display the result.
echo "\r";
echo $data_arr[0]['nu_i'];
echo "\r";
..
..
..
echo "\r";
echo $data_arr[3]['nu_i'];
echo "\r";
I have this search php code in which i made the formnumber into a hyperlink. All I wanna do is once i click a formnumber it will retrieve all ta data of tha click formnumber into the database and echos it.
$dbname = "vianney300";
$SRCHDATA = $_POST["srch"];
if($connection===FALSE)
echo "<p> Connection Failed. ". mysql_error()."</p>";
else{
if(mysql_select_db( $dbname)===FALSE)
echo "<p>Could not select database.</p>";
}
$query = "SELECT * FROM profile WHERE LastName='$SRCHDATA'";
$result = #mysql_query($query);
if ($result===FALSE){
echo "<p>Unable to execute query.</p>";
}
else {
while (($row = mysql_fetch_assoc($result))!==FALSE){
echo "Form no: ";
echo "<a href='individual.php'>{$row['FormNo']}</a></br>";
echo "Last name: ";
echo "{$row['LastName']}</br>";
echo "First name: ";
echo "{$row['FirstName']}</br></br>";
If you want to send a form number to individual page then you have to do like this inside your while function :
echo "<a href='individual.php?form_no={$row['FormNo']}'>{$row['FormNo']}</a></br>";
And on individual.php you will retrieve form number using $_GET['form_no']
individual.php
<?php
echo $_GET['form_no'];
// Using this form number you can fetch the data from database
?>
You have made a small mistake on this line
echo "<a href='individual.php'>{$row['FormNo']}</a></br>";
it should be
echo "<a href='individual.php?formnumber={$row['FormNo']}'>{$row['FormNo']}</a></br>";
ok.then what should i put to echo in the individual.php
Thats another question really, but
code individual.php
<?php
if ( isset($_GET['formnumber']) ) {
echo 'Recieved parameter "formnumber" in the GET array = ' . $_GET['formnumber'];
} else {
echo 'No parameter passed';
}
?>
This is the code I am trying to use to display information from multiple MySQL tables in one table. I'm also trying to create an else clause if the table information doesn't exist. This is my failed attempt at it.
I keep editing this as I peck away at it but at this point I still can't get it to display the final else when the row doesn't exist.
<?
include"db.inc.php";//database connection
$item = "SELECT * FROM Item";
$result = mysql_query($item);
while ($row=mysql_fetch_array($result)) // Display information from Item Table.
{
echo ("<tr><td>Edit</td>");
echo ("<td>$row[ItemID]</td>");
echo ("<td>$row[Category]</td>");
echo ("<td>$row[Cost]</td>");
echo ("<td>$row[Condition]</td>");
echo ("<td>$row[PurchaseLot_PurchaseLotID]</td>");
echo ("<td>$row[Location]</td>");
echo ("<td>$row[Date]</td>");
echo ("<td>$row[Desc]</td>");
echo ("<td>$row[Notes]</td>");
echo ("<td>Pics</td>");
echo ("<td>Info</td></tr>");
$info = "SELECT * FROM Info WHERE Item_ItemID = $row[ItemID] ";
$info_results = mysql_query($info);
while ($info_row = mysql_fetch_array($info_results))
{
if ($info_row['Item_ItemID'] = $row['ItemID']) // Display Information from Info table If column exists with same ItemID.
{
echo ("<tr><td colspan=\"12\">");
echo ("Manufacturer:$info_row[Manufacturer]<br>");
echo ("Model:$info_row[Model]<br>");
echo ("Model Number:$info_row[ModelNumber]<br>");
echo ("Serial Number:$info_row[SerialNumber]<br>");
echo ("Service Number:$info_row[ServiceNumber]<br>");
echo ("</td></tr>");
} else //else dispaly Add information to info link.
{
echo ("<tr><td colspan=\"12\">Add Extra Information</td></tr>");
}
}
}
?>
you could re-write the SQL statement in your $info "SELECT * FROM Info" to "SELECT * FROM Info wHERE Item_ItemID=$row['ItemID'] ... and then delete the
if ($info_row['Item_ItemID'] = $row['ItemID'])
Plus you new a while ind your code after $info_results = mysql_query($info);
* EDITED *
then you do like this, instead of
while ($info_row = mysql_fetch_array($info_results))
{
if ($info_row['Item_ItemID'] = $row['ItemID']) // Display Information from Info table If column exists with same ItemID.
{
echo ("<tr><td colspan=\"12\">");
echo ("Manufacturer:$info_row[Manufacturer]<br>");
echo ("Model:$info_row[Model]<br>");
echo ("Model Number:$info_row[ModelNumber]<br>");
echo ("Serial Number:$info_row[SerialNumber]<br>");
echo ("Service Number:$info_row[ServiceNumber]<br>");
echo ("</td></tr>");
} else //else dispaly Add information to info link.
{
echo ("<tr><td colspan=\"12\">Add Extra Information</td></tr>");
}
}
try this
if(mysql_num_rows($info)>0){
while($info_row = mysql_fetch_array($info){
echo ("<tr><td colspan=\"12\">");
echo ("Manufacturer:$info_row[Manufacturer]<br>");
echo ("Model:$info_row[Model]<br>");
echo ("Model Number:$info_row[ModelNumber]<br>");
echo ("Serial Number:$info_row[SerialNumber]<br>");
echo ("Service Number:$info_row[ServiceNumber]<br>");
echo ("</td></tr>");
}
}else {
echo ("<tr><td colspan=\"12\">Add Extra Information</td></tr>");
}
i try to edit your code but im not able to try it.
<?
include"db.inc.php";//database connection
$item = "SELECT * FROM Item";
$result = mysql_query($item);
while ($row=mysql_fetch_array($result)) // Display information from Item Table.
{
echo ("<tr><td>Edit</td>");
echo ("<td>$row[ItemID]</td>");
echo ("<td>$row[Category]</td>");
echo ("<td>$row[Cost]</td>");
echo ("<td>$row[Condition]</td>");
echo ("<td>$row[PurchaseLot_PurchaseLotID]</td>");
echo ("<td>$row[Location]</td>");
echo ("<td>$row[Date]</td>");
echo ("<td>$row[Desc]</td>");
echo ("<td>$row[Notes]</td>");
echo ("<td>Pics</td>");
echo ("<td>Info</td></tr>");
$info = "SELECT * FROM Info where Item_ItemID = ".$row['ItemID'];
$info_results = mysql_query($info);
if ($info_row = mysql_fetch_array($info_results))
{
echo ("<tr><td colspan=\"12\">");
echo ("Manufacturer:$info_row[Manufacturer]<br>");
echo ("Model:$info_row[Model]<br>");
echo ("Model Number:$info_row[ModelNumber]<br>");
echo ("Serial Number:$info_row[SerialNumber]<br>");
echo ("Service Number:$info_row[ServiceNumber]<br>");
echo ("</td></tr>");
}
else { //else dispaly Add information to info link.
echo ("<tr><td colspan=\"12\">Add Extra Information</td></tr>");
}
}
?>
i hope this is the answer.