View detailed MySQL Result when Hyperlink is Clicked - php

I'm trying to achieve the following. On the homepage a MySQL query returns results from table "parcid"(but limited results). The next step is, I would like to hyperlink a field (preferably "ID") to open a new page using the same ID but returning more detailed results. The code below works but obviously I'm missing something somewhere as it is returning all results rather than only results for the clicked on ID. Any help will be greatly appreciated.
Home url: "http://localhost/"
Pdetail url: "http://localhost/pdetail/"
Query on "Home" page
<?php
// set database server access variables:
$host = "localhost";
$user = "root";
$pass = "";
$db = "wordpress";
// open connection
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");
// select database
mysql_select_db($db) or die ("Unable to select database!");
// create query
$query = "SELECT * FROM parcid";
// execute query
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());
// see if any rows were returned
if (mysql_num_rows($result) > 0) {
// yes
// print them one after another
echo "<table cellpadding=1 >";
echo "<tr>";
echo "<td>"."ID"."</td>";
echo "<td>"."City"."</td>";
echo "<td>"."Destination City"."</td>";
echo "<td>"."Weight"."</td>";
echo "<td>"."Length"."</td>";
echo "<td>"."Width"."</td>";
echo "<td>"."Height"."</td>";
echo "<td>"."Type"."</td>";
echo "<td>"."Courier Option"."</td>";
echo "</tr>";
while($row = mysql_fetch_row($result)) {
echo "<tr>";
echo '<td>'.$row[0].'</td>';
echo "<td>" . $row[6]."</td>";
echo "<td>" . $row[12]."</td>";
echo "<td>" . $row[14]."</td>";
echo "<td>" . $row[15]."</td>";
echo "<td>" . $row[16]."</td>";
echo "<td>" . $row[17]."</td>";
echo "<td>" . $row[18]."</td>";
echo "<td>" . $row[19]."</td>";
echo "<td>" . $row[20]."</td>";
echo "</tr>";
}
echo "</table>";
}
else {
// no
// print status message
echo "No rows found!";
}
// free result set memory
mysql_free_result($result);
// close connection
mysql_close($connection);
?>
Query on"pdetailed" page.
<?php
// set database server access variables:
$host = "localhost";
$user = "root";
$pass = "";
$db = "wordpress";
// open connection
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");
// select database
mysql_select_db($db) or die ("Unable to select database!");
$ID = $_GET['ID'];
// create query
$query = "SELECT * FROM parcid WHERE ID LIKE '%$ID%'";
// execute query
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());
// see if any rows were returned
if (mysql_num_rows($result) > 0) {
// yes
// print them one after another
echo "<table cellpadding=1 >";
echo "<tr>";
echo "<td>"."ID"."</td>";
echo "<td>"."CID"."</td>";
echo "<td>"."From"."</td>";
echo "<td>"."Street"."</td>";
echo "<td>"."Suburb"."</td>";
echo "<td>"."Post Code"."</td>";
echo "<td>"."City"."</td>";
echo "<td>"."Province"."</td>";
echo "<td>"."Receiver"."</td>";
echo "<td>"."Destination Street"."</td>";
echo "<td>"."Destination Suburb"."</td>";
echo "<td>"."Destination Postcode"."</td>";
echo "<td>"."Destination City"."</td>";
echo "<td>"."Destination Province"."</td>";
echo "<td>"."Weight"."</td>";
echo "<td>"."Length"."</td>";
echo "<td>"."Width"."</td>";
echo "<td>"."Height"."</td>";
echo "<td>"."Type"."</td>";
echo "<td>"."Courier Option"."</td>";
echo "</tr>";
while($row = mysql_fetch_row($result)) {
echo "<tr>";
echo "<td>" . $row[0]."</td>";
echo "<td>" . $row[1]."</td>";
echo "<td>" . $row[2]."</td>";
echo "<td>" . $row[3]."</td>";
echo "<td>" . $row[4]."</td>";
echo "<td>" . $row[5]."</td>";
echo "<td>" . $row[6]."</td>";
echo "<td>" . $row[7]."</td>";
echo "<td>" . $row[8]."</td>";
echo "<td>" . $row[9]."</td>";
echo "<td>" . $row[10]."</td>";
echo "<td>" . $row[11]."</td>";
echo "<td>" . $row[12]."</td>";
echo "<td>" . $row[13]."</td>";
echo "<td>" . $row[14]."</td>";
echo "<td>" . $row[15]."</td>";
echo "<td>" . $row[16]."</td>";
echo "<td>" . $row[17]."</td>";
echo "<td>" . $row[18]."</td>";
echo "<td>" . $row[19]."</td>";
echo "<td>" . $row[20]."</td>";
echo "</tr>";
}
echo "</table>";
}
else {
// no
// print status message
echo "No rows found!";
}
// free result set memory
mysql_free_result($result);
// close connection
mysql_close($connection);
?>

If you want just the single row identified by $ID then the query is
$query = "SELECT * FROM parcid WHERE ID = '$ID'";
Also when you know you should only receive a single result row from a query, you do not need to run the mysql_fetch_row($result) in a while loop.
Please dont use the mysql_ database extension, it
is deprecated (gone for ever in PHP7)
Especially if you are just learning PHP, spend your energies learning the PDO or mysqli_ database extensions,
and here is some help to decide which to use
Unless you are using a framework this statement might be wrong
echo '<td>'.$row[0].'</td>';
Maybe it should be
echo '<td>'.$row[0].'</td>';

Related

images appearing in warning but not in table?

I am working on a website whereby a load of advertisers are stored in the DB and then displayed to the user by there logo. I know storing directly in to the DB for images is not the done thing, however, I am starting out this way, to get the website running and then will refactor to move to a much more suitable approach.
Currently, I have the following PHP code:
<?php
session_start();
require_once "config.php";
// Create connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
$sql = "SELECT * FROM advertisers";
if($result = mysqli_query($link, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<table>";
echo "<tr>";
echo "<th>id</th>";
echo "<th>advertiser_Name</th>";
echo "<th>advertiser_URL</th>";
echo "<th>advertiser_Category</th>";
echo "<th>advertiser_logo</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['advertiser_id'] . "</td>";
echo "<td>" . $row['advertiser_Name'] . "</td>";
echo "<td>" . $row['advertiser_URL'] . "</td>";
echo "<td>" . $row['advertiser_Category'] . "</td>";
echo "<td>" . $row['<img src="data:image/jpeg;base64,'.base64_encode($row['advertiser_logo']).'"/>'] . "</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);
}
mysqli_close($link);
?>
However, the images are displayed when called from the DB but they are displayed in the warning message rather than in the table?
<?php
session_start();
require_once "config.php";
// Create connection
if ($link === false)
{
die("ERROR: Could not connect. " . mysqli_connect_error());
}
$sql = "SELECT * FROM advertisers";
if ($result = mysqli_query($link, $sql))
{
if (mysqli_num_rows($result) > 0)
{
echo "<table>";
echo "<tr>";
echo "<th>id</th>";
echo "<th>advertiser_Name</th>";
echo "<th>advertiser_URL</th>";
echo "<th>advertiser_Category</th>";
echo "<th>advertiser_logo</th>";
echo "</tr>";
while ($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['advertiser_id'] . "</td>";
echo "<td>" . $row['advertiser_Name'] . "</td>";
echo "<td>" . $row['advertiser_URL'] . "</td>";
echo "<td>" . $row['advertiser_Category'] . "</td>";
echo "<td><img src='data:image/jpeg;base64," . base64_encode($row['advertiser_logo']) . "'/></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);
}
mysqli_close($link);
?>
The fact that is showing the image in the warning is because you're using a tag with the source as an array key which is not correct.
The array keys, so what is inside the square bracket, is the reference to the array position. If you're familiar with C for example is the 0, 1, ecc.. and not the value itself.
Yes as #NigelRen mentioned this row $row['<img src="data:image/jpeg; looks very bad.
I think you should use:
echo "<td><img src='data:image/jpeg;base64," . base64_encode($row['advertiser_logo']) . "'/></td>";

Confusing situation happening when using PHP with Mysqli

every time I run this code below:
<?php
$servername = "******";
$username = "******";
$password = "*******";
$dbname = "*******";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error)
{
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT date, point, reason, teacher, giver FROM penalty WHERE studentid=".$_GET['id'];
$result = $conn->query($sql);
if ($result["num_rows"] > 0)
{
echo "<table>";
echo "<tr>";
echo "<td>dated</td>";
echo "<td>point</td>";
echo "<td>reason</td>";
echo "<td>giver</td>";
echo "<td>teacher</td>";
echo "</tr>";
while($row = $result->fetch_assoc())
{
echo "<tr>";
echo "<td>".$row["dated"]."</td>";
echo "<td>".$row["point"]."</td>";
echo "<td>".$row["reason"]."</td>";
echo "<td>".$row["giver"]."</td>";
echo "<td>".$row["teacher"]."</td>";
echo "</tr>";
}
echo "</table>";
}
else if ($result["num_rows"] = 0)
{
echo "it is 0!";
}
else
{
echo "Unknown Error!";
}
$conn->close();
?>
I get the error message that I have written like this:
Unknown Error!
So, I'm thinking that the code has some problems in
if ($result["num_rows"] > 0)
What is the problem in the code, and what should I do to solve it?
ps. I'm a newbie in PHP, feel free to answer. Thanks in advance.
To check number of rows return from query we use
$result = $conn->query($sql);
if($result->num_rows > 0)
{
// your code
}
Instead
if ($result["num_rows"] > 0)
Your whole code would be
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table>";
echo "<tr>";
echo "<td>dated</td>";
echo "<td>point</td>";
echo "<td>reason</td>";
echo "<td>giver</td>";
echo "<td>teacher</td>";
echo "</tr>";
while ($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td>" . $row["dated"] . "</td>";
echo "<td>" . $row["point"] . "</td>";
echo "<td>" . $row["reason"] . "</td>";
echo "<td>" . $row["giver"] . "</td>";
echo "<td>" . $row["teacher"] . "</td>";
echo "</tr>";
}
echo "</table>";
} else {
echo "it is 0!";
}
Read http://php.net/manual/en/mysqli-result.num-rows.php
Your code is open for sql injection
Read http://php.net/manual/en/mysqli.prepare.php Prepare statement in mysqli

How I can display all rows in php

I wrote this code to retrieve some rows form database
session_start();
$con = mysqli_connect('localhost', 'root', '');
if(!$con)
{
die("not ok");
}
mysqli_select_db($con,"uoh");
$q = " SELECT * FROM student WHERE id = " . $_SESSION['user_id'] ." and password = " . $_SESSION['user_pass'];
$result = mysqli_query($con , $q ) ;
if($row = mysqli_fetch_array($result))
{
echo "this academic transcripts for " . $row["name"];
echo " and the id is " . $row["id"];
}
$q1 = " SELECT student_record.course,student_record.grade,student_record.term,coe_courses.crd
FROM student_record INNER JOIN coe_courses ON student_record.course_number = coe_courses.course_number
where student_record.id = ".$_SESSION['user_id'] ;
$result = mysqli_query($con , $q1 ) ;
if($row = mysqli_fetch_array($result))
{
echo "<br />";
echo "<table border=\"1\" style=\"width:500\">";
echo "<tr>";
echo "<th>coe_courses</th>";
echo "<th>terms</th>";
echo "<th>Grades</th>";
echo "<th>CRD</th>";
echo "</tr>";
echo "<tr>";
echo "<td>" . $row["course"]. "</td>";
echo "<td>" . $row["term"]. "</td>";
echo "<td>" . $row["grade"]. "</td>";
echo "<td>" . $row["crd"]. "</td>";
echo "</tr>";
echo "</table>";
}
The problem is that only shows the first row while I have three rows in phpMyAdmin.
enter image description here
You need to call fetch_* repeatedly to retrieve all rows from your result set; each time you call it it retrieves the next row in the result set.
In your sample code above, you would replace
if ($row = mysqli_fetch_array($result))
{
with
while ($row = mysqli_fetch_array($result))
{
This will loop until fetch_array tries to read beyond the last record in $result, at which point fetch_array returns false and the loop exits.

PHP - Change only one row, not all

I've just started with PHP and literally been stuck now for 5 hours straight trying to figure this out! I understand what's happening but cannot for the life of me find a fix anywhere D: Basically each row is displayed on the users browser. Beside each one is a 'Mark as complete' button. When this button is pressed is changes the value from 0 to 1. Problem is, it changes the value of 0 to 1 for every row D: Please help me it's a pain in the but now haha! Heres my code:
<?php
// Declare Variables
$host = localhost;
$user = root;
$pass = root;
$db = test;
// Create connection to database
$link = mysqli_connect($host, $user, $pass, $db);
// Check to see if connection was established
if($link === false) {
die("Connection could not be established to database" .mysqli_error());
}
$sql = "SELECT * FROM details WHERE Status = 0";
// Show data
if($result = mysqli_query($link, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<table>";
echo "<tr>";
echo "<th>First name</th>";
echo "<th>Last name</th>";
echo "<th>Destination</th>";
echo "<th>Value</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<form action='complete.php' method='post'>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "<td>" . $row['Destination'] . "</td>";
echo "<td>" . $row['Status'] . "</td>";
echo "<td>" . "<input type='submit' value='Mark as complete'>" . "</td>";
echo "</tr>";
}
echo "</table>";
}
else{
echo "No records matching your query were found.";
}
}
else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
?>
and complete.php
<?php
// Declare Variables
$host = localhost;
$user = root;
$pass = root;
$db = test;
// Create connection to database
$link = mysqli_connect($host, $user, $pass, $db);
// Check to see if connection was established
if($link === false) {
die("Connection could not be established to database" .mysqli_error());
}
// sql to delete a record
$sql = "UPDATE details SET Status=1";
if ($link == true) {
echo "Marked as complete";
} else {
echo "Error updating record: " . $link->error;
}
mysqli_close($link)
?>
You are missing some steps. Here is the full process as you want to do.
Just replace with your. I hope this will solve your problem.
<?php
// Declare Variables
$host = localhost;
$user = root;
$pass = root;
$db = test;
// Create connection to database
$link = mysqli_connect($host, $user, $pass, $db);
// Check to see if connection was established
if($link === false) {
die("Connection could not be established to database" .mysqli_error());
}
$sql = "SELECT * FROM details WHERE Status = 0";
// Show data
if($result = mysqli_query($link, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<table>";
echo "<tr>";
echo "<th>First name</th>";
echo "<th>Last name</th>";
echo "<th>Destination</th>";
echo "<th>Value</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<form action='complete.php' method='post'>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "<td>" . $row['Destination'] . "</td>";
echo "<td>" . $row['Status'] . "</td>";
echo "<td>" . "<input type='submit' value='Mark as complete'>" . "</td>";
// Put your primary key column name in the place of Id
echo "<input type='hidden' name='user_id' value='".$row['Id']."'>";
echo "</form>";
echo "</tr>";
}
echo "</table>";
}
else{
echo "No records matching your query were found.";
}
}
else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
?>
and complete.php
<?php
// Declare Variables
$host = localhost;
$user = root;
$pass = root;
$db = test;
// Create connection to database
$link = mysqli_connect($host, $user, $pass, $db);
// Check to see if connection was established
if($link === false) {
die("Connection could not be established to database" .mysqli_error());
}
// sql to delete a record
// Put your primary key column name in the place of Id
$sql = "UPDATE details SET Status=1 WHERE Id='".$_POST['user_id']."' ";
if ($link == true) {
echo "Marked as complete";
} else {
echo "Error updating record: " . $link->error;
}
mysqli_close($link)
?>

MySql no output

Okay, i have this code
<?php
$email = htmlentities($_SESSION['user']['dato'], ENT_QUOTES, 'UTF-8');
$username = "mcnsaoia_onsafe";
$password = "XXX";
$hostname = "localhost";
//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";
//select a database to work with
$selected = mysql_select_db("mcnsaoia_onsafe",$dbhandle)
or die("Could not select examples");
//execute the SQL query and return records
$result = mysql_query("SELECT * FROM users WHERE id= '$email'") or die(mysql_error());
//fetch tha data from the database
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['status'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
echo "</tr>";
}
echo "</table>";
//close the connection
mysql_close($dbhandle);
?>
but when i run it, it just says Connected to MySQL and it DON'T output anything??
i have no idea why it does that?!
while($row = mysqli_fetch_array($result))
change it with mysql_fetch_array()
while($row = mysql_fetch_array($result))
Check whether rows are returned by your query using mysql_num_rows() function.
if(mysql_num_rows($result)>1)
{
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['status'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
echo "</tr>";
}
}
else
{
echo "No rows found";
}
Note : Use mysqli_* functions . mysql_* functions have been deprecated.

Categories