Passing PHP select elements into a url - php

Ok so I have a simple php select script and I have the code below to define variables.
$result = mysqli_query($con,"SELECT * FROM practice_sheets WHERE student_name='$_SESSION[SESS_FIRST_NAME] $_SESSION[SESS_LAST_NAME]'");
$numrows = mysqli_num_rows($result);
$id = $row['id'];
$total_min = $row['total_min'];
$due_date = $row['due_date'];
I then have:
echo "<td> <a href='account/practiceSheets?id='$id'> <i class='icon-eye-open'> </i> </a> </td>";
and this is supposed to pass the variables from the php select script into the url when the <a> is clicked.
All I end up with is the account/practiceSheets?id= with no actual id. I'm sure this is something stupidly simple and I do apologize as I am new to PHP and also didn't know what to call this to get a useable result in search engines! My full code is below if it helps.
<?php
$con = mysqli_connect("50.63.106.47", "usd309bands", "MacBook1!", "usd309bands");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con, "SELECT * FROM practice_sheets
WHERE student_name='$_SESSION[SESS_FIRST_NAME] $_SESSION[SESS_LAST_NAME]'");
$numrows = mysqli_num_rows($result);
$id = $row['id'];
$total_min = $row['total_min'];
$due_date = $row['due_date'];
if ($numrows == 0) {
echo "<div class='alert alert-danger'>";
echo "No Entries, See your instructor for details.";
echo "</div>";
} else {
echo "<table class='mws-table table-striped table-hover'>";
echo "<thead align='center'>";
echo "<tr>";
echo "<th>Sheet Number</th>";
echo "<th>Total Minutes</th>";
echo "<th>Due Date</th>";
echo "<th>View</th>";
echo "</tr>";
echo "</thead>";
echo "<tbody align='center'>";
while ($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['total_min'] . "</td>";
echo "<td>" . $row['due_date'] . "</td>";
echo "<td> <a href='account/practiceSheets?id='$id'> <i class='icon-eye-open'> </i> </a> </td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
mysqli_close($con);
}
?>

$result = mysqli_query($con,"SELECT * FROM practice_sheets
WHERE student_name='$_SESSION[SESS_FIRST_NAME] $_SESSION[SESS_LAST_NAME]'");
$numrows = mysqli_num_rows($result);
$id = $row['id'];
Can't see the where the $row is coming from.
I think you've forgotten to fetch the MySQL response into an associated array.
(You could use mysqli_fetch_assoc() to acomplish that)
Here's a quick example:
$result = mysqli_query($con,"SELECT * FROM practice_sheets WHERE student_name='$_SESSION[SESS_FIRST_NAME] $_SESSION[SESS_LAST_NAME]'");
$allRows = mysqli_fetch_assoc($result);
foreach($allRows as $row) {
echo $row['id'].'<br/>';
}
This one should list all ID's from the database request.

This sql query is probably not going to return anything unless names are stored in your table like this
firstlast
"SELECT * FROM practice_sheets WHERE student_name='$_SESSION[SESS_FIRST_NAME] $_SESSION[SESS_LAST_NAME]'"
you probably want something like this
$_SESSION[SESS_FIRST_NAME] . " " . $_SESSION[SESS_LAST_NAME]
to return a name like
first last

I changed
echo "<td> <a href='account/practiceSheets?id='$id'> <i class='icon-eye-open'></i></a></td>"
to this:
echo "<td> <a href='account/practiceSheets?id=" . $row["id"] . "'> <i class='icon-eye-open'> </i> </a> </td>";
and got the desired results!

Related

How to make sql query to display 1 result based on id in the database table

I need help, I cannot figure out, I cannot find why I am having errors and I am not able to achieve something freaking simple.
Long story short, I have a website to manage projects, so when I run the search function it throws a table with some records from the database, there is a button called "see details" which is assigned to a project id with database i.e. 21, 1, 48 etc, the problem is that when I click "see details" it throws everything from the table proposals instead of 1 project, no matter which button I click on, if its id 1, 21, 48, it prints everything.
details page
details.php:
<?php
include '../includes/config.php';
// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
// Attempt select query execution
$sql = "SELECT * FROM proposals_table WHERE id LIKE '_%'";
if($result = mysqli_query($link, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<table class='table table-bordered'>";
echo "<tr>";
echo "<th>Organisation</th>";
echo "<th>Project</th>";
echo "<th>Proposal Date</th>";
echo "<th>Date Received</th>";
echo "<th>Notes</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['company'] . "</td>";
echo "<td>" . $row['project'] . "</td>";
echo "<td>" . $row['proposal_date'] . "</td>";
echo "<td>" . $row['date_received'] . "</td>";
echo "<td>" . $row['notes'] . "</td>";
echo "</tr>";
}
echo "</table>";
// Free result set
mysqli_free_result($result);
} else{
echo "No records matching your query were found.";
}
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
?>
search/result page
proposals.php
<?php
include '../includes/config.php';
// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
// Attempt select query execution
$sql = "SELECT * FROM proposals_table";
if($result = mysqli_query($link, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<table class='table table-bordered'>";
echo "<tr>";
echo "<th>Organisation</th>";
echo "<th>Project</th>";
echo "<th>Proposal Date</th>";
echo "<th>Date Received</th>";
echo "<th>Options</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['company'] . "</td>";
echo "<td>" . $row['project'] . "</td>";
echo "<td>" . $row['proposal_date'] . "</td>";
echo "<td>" . $row['date_received'] . "</td>";
echo "<td> <a class='btn btn-primary' href='details.php?id={$row['id']}'>See details</a></td>";
echo "</tr>";
}
echo "</table>";
// Free result set
mysqli_free_result($result);
} else{
echo "No records matching your query were found.";
}
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
?>
If you want to show only the selected element on your details page then you need to fetch only that selected item from the database.
First of all you should separate HTML from PHP. The best would be to have them in separate files. In PHP you prepare the data to be displayed and then in HTML you fill in the blanks with PHP values.
To select a value from MySQL using a given ID you must use prepared statements with parameter binding. So if you create your link in this way:
echo "<td> <a class='btn btn-primary' href='details.php?id=".urlencode($row['id'])."'>See details</a></td>";
You can receive this ID in your details page using $_GET['id']. You can bind that value to your WHERE clause in SQL.
<?php
include '../includes/config.php';
// Attempt select query execution
$stmt = $link->prepare("SELECT * FROM proposals_table WHERE id=?");
$stmt->bind_param('s', $_GET['id']);
$stmt->execute();
$proposals = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
if($proposals) {
?>
<table class='table table-bordered'>
<tr>
<th>Organisation</th>
<th>Project</th>
<th>Proposal Date</th>
<th>Date Received</th>
<th>Notes</th>
</tr>
<?php foreach($proposals as $row): ?>
<tr>
<td><?=$row['company'] ?></td>
<td><?=$row['project'] ?></td>
<td><?=$row['proposal_date'] ?></td>
<td><?=$row['date_received'] ?></td>
<td><?=$row['notes'] ?></td>
</tr>
<?php endforeach; ?>
</table>
<?php
} else {
echo 'No records matching your query were found.';
}
And of course your config.php page should look like this:
<?php
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$link = new mysqli('localhost', 'user', 'pass', 'db');
$link->set_charset('utf8mb4'); // always set the charset

Display mysql data in formatted table

I am retrieving data from a database and displaying in a table. I would like the table to be in a 4 x 3 layout. The data retrieves just fine, it's the layout that isn't working. Here is my code:
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
$db = new mysqli('localhost', 'root', '', 'ezwayautos');
if($db->connect_errno > 0){
die('Unable to connect to database [' . $db->connect_error . ']');
}
$sql = "SELECT * FROM vehicles ORDER BY RAND() LIMIT 12";
if(!$result = $db->query($sql)){
die('There was an error running the query [' . $db->error . ']');
}
echo "<table>";
while($row = $result->fetch_assoc())
{
echo "<tr>";
echo "<td height='160' valign='top' class='featured'>";
echo "<div class='Image-Thumbnail'>";
echo "<a href=''>";
echo "<img src='".$row['image']."' width='160' height='54'>";
echo "</a>";
echo "</div> <a href=''>" .$row['vehicle_name']. "</a>";
echo "</td>";
echo "</tr>";
}
echo "</table>";
?>
What is supposed to go in each cell is a picture of the vehicle with the name underneath the picture.
Here is an example from another website as to how I would like it to look:
http://www.denisonmotors.com
I am not stealing any information from their site I am just trying to get my data to display in the format that they have on their site.
I am not concerned about the empty href tag as they will be filled in later.
After doing some research, I think that I have to use 2 for loops to create the table stating how many columns and row that I want.
Is it as simple as moving the last 3 lines that close the outer table outside the while loop?
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
$db = new mysqli('localhost', 'root', '', 'ezwayautos');
if($db->connect_errno > 0){
die('Unable to connect to database [' . $db->connect_error . ']');
}
$sql = "SELECT * FROM vehicles ORDER BY RAND() LIMIT 12";
if(!$result = $db->query($sql)){
die('There was an error running the query [' . $db->error . ']');
}
echo "<table>";
echo "<tr>";
echo "<td>";
while($row = $result->fetch_assoc())
{
echo "<table border='0' cellpadding='0' cellspacing='0'>";
echo "<tr>";
echo "<td height='160' valign='top' class='featured'>";
echo "<div class='Image-Thumbnail'>";
echo "<a href='inventory/view/7995179/2005-Volvo-XC90-4dr-2.html'>";
echo "<img src='".$row['image']."' width='160' height='54' alt='".$row['vehicle_name']."'>";
echo "</a>";
echo "</div> <a href='inventory/view/7995179/2005-Volvo-XC90-4dr-2.html'>" .$row['vehicle_name']. "</a>";
echo "</td>";
echo "</tr>";
echo "</table>";
}
echo "</td>";
echo "</tr>";
echo "</table>";
?>
The table generation is not correct. I think you don't mean to create a table inside another table, so I think this is the way you should do:
echo "<table>";
while($row = $result->fetch_assoc())
{
echo "<tr>";
echo "<td height='160' valign='top' class='featured'>";
echo "<div class='Image-Thumbnail'>";
echo "<a href='inventory/view/7995179/2005-Volvo-XC90-4dr-2.html'>";
echo "<img src='".$row['image']."' width='160' height='54' alt='".$row['vehicle_name']."'>";
echo "</a>";
echo "</div> <a href='inventory/view/7995179/2005-Volvo-XC90-4dr-2.html'>" .$row['vehicle_name']. "</a>";
echo "</td>";
echo "</tr>";
}
echo "</table>";
You have to create the table once, and then a row for each record.

Extracting MySQL data as table content

So I have a database from which I want to output data in the form of a table.
<table>
<tr>
<th class="tg-031e">Username</th>
<th class="tg-031e">Password</th>
</tr>
..
</table>
For now I get the data like this:
<?php include 'connect.php';
echo "<tr><td class='tg-031e'>";
$SQL = "SELECT `Username` FROM `Users` WHERE ID=1";
$exec = mysql_query($SQL, $connection);
while($row = mysql_fetch_array($exec)){
echo $row['Username'] . "</td>";
} ?>
However, in that case I need to echo out each column independently. How can I make PHP dynamically create an HTML table row with the information from the database whenever new data is present in the MySQL columns?
modify your php code like this:
<?php include 'connect.php';
$SQL = "SELECT `Username` FROM `Users` WHERE ID=1";
$exec = mysql_query($SQL, $connection);
echo "<table>";
echo "<tr>";
echo "<th class="tg-031e">Username</th>";
echo "<th class="tg-031e">Password</th>";
echo "</tr>";
while($row = mysql_fetch_array($exec)){
//add as many fields in record, i.e: username, password... etc.
echo "<tr>";
echo "<td class='tg-031e'>" . $row['Username'] . "</td>";
echo "<td class='tg-031e'>" . $row['Password'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
<?php include 'connect.php';
$SQL = "SELECT `Username`, Password FROM `Users`";
$exec = mysql_query($SQL, $connection);
echo "<table><tr><td class="tg-031e">Username</td><td class="tg-031e">Password</td></tr>";
while($row = mysql_fetch_array($exec))
{
echo "<tr><td class='tg-031e'>";
echo $row['Username'] . "</td>";
echo "<td class='tg-031e'>".$row['Password'] . "</td></tr>";
}
echo "</table>";
?>

Can't display image

I don't know why doesn't display the avatar 100x100. Something is wrong and I don't know how to fix it.
$result = mysqli_query($con,"SELECT * FROM `users` WHERE `verified` = 1 and lastcheck=0 order by `authoredPostCount` DESC");
echo "<table border='1'> <tr>
<th>Picture</th>
<th>Verified</th>
<th>Videos</th></tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row "<img src="['avatar']."></td>";
echo "<td>" . $row['userId'] . "</td>";
echo "<td>" . $row['authoredPostCount'] . "</td>";
echo "</tr>";
}
echo "</table>";
if (!$cuserId) {
printf("Error: %s\n", mysqli_error($con));
exit();
}
mysqli_close($con);
?>
Replace
echo "<td>" . $row "<img src="['avatar']."></td>";
With
echo "<td><img src=". $row['avatar']."></td>";
if you are saving image file name in database then use it like that
echo "<td><img src='path/to/folder/". $row['avatar'].".jpeg'></td>";

PHP beginner. Trying to display UserId

I am accessing my database on PhpMyAdmin, all my names etc are correct, but for some reason UserId is not working. Can someone point me in the right direction?
I have tried printing it but nothing displays.
<?php session_start();
$username = $_GET['username'];
$password = $_GET['password'];
// Create connection
$con=mysqli_connect("localhost","root","","test");
// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM user2 where username='$username' and password='$password'");
$row_cnt = mysqli_num_rows($result);
if($row_cnt >0){
while($row = mysqli_fetch_array($result)){
$UserId = $row['UserId'];
}
$sqlQuery2 = "SELECT ProductID, Name, Price, Description FROM product";
echo "Hello ".$username."<br>" .$UserId. "<br> This is a list of products";
$result2 = mysqli_query($con,$sqlQuery2);
echo "<table border='1'>
<tr>
<th>ProductID</th>
<th>Name</th>
<th>Price</th>
<th>Description</th>
<th>View</th>
</tr>";
while($row = mysqli_fetch_array($result2))
{
echo "<tr>";
echo "<td>" . $row['ProductID'] . "</td>";
echo "<td>" . $row['Name'] . "</td>";
echo "<td>" . $row['Price'] . "</td>";
echo "<td>" . $row['Description'] . "</td>";
echo "<td><a href=\"detailview.php?ProductID=".$row['ProductID']."\"'>Detailed View</a></td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
Update My Details
<?php } else{
echo "invalid login "; }
?>
use var_dump($var) to see what is in the variable
1st check what is returned in $result and after check $UserId before using it
after the while check if $UserId is set (if the condition is false, ou var is not set..) you should check $row_count too
you should indent your code
here is your code reindented :
<?php session_start();
$username = $_GET['username'];
$password = $_GET['password'];
// Create connection
$con=mysqli_connect("localhost","root","","test");
// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM user2 where username='$username' and password='$password'");
$row_cnt = mysqli_num_rows($result);
if($row_cnt >0){
while($row = mysqli_fetch_array($result)){
$UserId = $row['UserId'];
}
$sqlQuery2 = "SELECT ProductID, Name, Price, Description FROM product";
echo "Hello ".$username."<br>" .$UserId. "<br> This is a list of products";
$result2 = mysqli_query($con,$sqlQuery2);
echo "<table border='1'>
<tr>
<th>ProductID</th>
<th>Name</th>
<th>Price</th>
<th>Description</th>
<th>View</th>
</tr>";
while($row = mysqli_fetch_array($result2))
{
echo "<tr>";
echo "<td>" . $row['ProductID'] . "</td>";
echo "<td>" . $row['Name'] . "</td>";
echo "<td>" . $row['Price'] . "</td>";
echo "<td>" . $row['Description'] . "</td>";
echo "<td><a href=\"detailview.php?ProductID=".$row['ProductID']."\"'>Detailed View</a></td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
Update My Details
<?php
}
else
{
echo "invalid login ";
}
?>

Categories