Sorry for my lack of experience, but I try to explain my goal the best a can...
I have 2 MySQL tables with the following fields...
tbl_cars_clients
FIELDS
"client_name,
logo_img,
phone,address,email"
tbl_cars_items
FIELDS
"car_name,
client_name,cars_img"
I have a main page called index.php that contains the following code
$query_cake = mysql_query("select * from tbl_cars_clients", $connection);
while ($row = mysql_fetch_array($query_cars)) {
echo "<img class='img' src={$row['ad_img']} width='350px' height='225px'>";
I have another page called items-cars.php
<?php
if (isset($_GET['id'])) {
$client_id = $_GET['id'];
$query1 = mysql_query("SELECT * FROM tbl_cars_items WHERE client_name = '$client_id'", $connection);
while ($row1 = mysql_fetch_array($query1)) {
?>
<?php echo "$row1['cars_img']} width='250px' height='250px'>";?>
On index.php, When I click on any of these Image hyperlinks, it opens items-cars.php and only display records with the same client_name field from the 2 tables.
My goal is to not only show the relating records on items-cars.php, but to display the records from the tbl_cars_clients table.
EXAMPLE: If I click image hyperlink for TOYOTA on index.php, It currently opens up the following records on items-cars.php...
COROLLA,
PRIUS,
CAMRY,
TUNDRA,
Instead, I would like it to display results like...
TOYOTA
888-123-4567
123 Maple Dr.
info#toyota.com
COROLLA,
PRIUS,
CAMRY,
TUNDRA,
i think you want this...
<?php
if (isset($_GET['id'])) {
$client_id = $_GET['id'];
$query1 = mysql_query("SELECT * FROM tbl_cars_items WHERE client_name = '$client_id'", $connection);
while ($row1 = mysql_fetch_array($query1)) {
$query2 = mysql_query("SELECT * FROM tbl_cars_clients WHERE client_name = '$client_id'", $connection);
$row2 = mysql_fetch_array($query2);
?>
<?php echo $row2['client_name']; ?><br />
<?php echo $row2['phone']; ?><br />
<?php echo $row2['address']; ?><br />
<?php echo $row2['email']; ?><br />
<?php echo "$row1['cars_img']} width='250px' height='250px'>";?>
}
Related
My first question on Stack Overflow, so please forgive if I make any mistake.
I have a database, in which I store the User Id and the title and corresponding content. Then in a PHP page, I loop through the rows of the table and display the title of the content in an anchor tag.
The problem is, When the user clicks on a specific title, he should be redirected to new page where the content corresponding to the title which he clicked is displayed.
Could you please guide me as to how to achieve it?
Thanks.
Here is the code that I've tried.
<?php
$counter1 = 0;
$sql = "SELECT * FROM blogdata2";
$result = $conn->query($sql);
while($row = $result->fetch_assoc()){
echo "<a style='color: #E64A19;' href='#'/>".$arrayContent[$counter1]['title'];
echo "<br/>";
$counter1++;
Try like this...
$sql = "SELECT * FROM blogdata2";
$result = $conn->query($sql);
while($row = $result->fetch_assoc()){
echo "<a style='color: #E64A19;' href='newpage.php?content=".$row['content']."'>".$row['title']."</a>";
echo "<br/>";
}
and in newpage.php
<?php
$content=$_GET['content'];
echo "<p>".$content."</p">;
But following way is best...
$sql = "SELECT * FROM blogdata2";
$result = $conn->query($sql);
while($row = $result->fetch_assoc()){
echo "<a style='color: #E64A19;' href='newpage.php?id=".$row['id']."'>".$row['title']."</a>";
echo "<br/>";
}
and in newpage.php
<?php
$id=$_GET['id'];
$sql="SELECT * FROM blogdata2 WHERE id='$id'";
$result = $conn->query($sql);
$row = $result->fetch_assoc();
echo "<p>".$row['content']."</p>";
I hope I have asked correctly; I'm a beginning PHP programmer. Here is my part of code...
$con = mysqli_connect("localhost","root","","project");
mysqli_select_db($con,"project");
$result = mysqli_query($con,"select distinct drug from drugs");
while($row = mysqli_fetch_array($result))
{
echo "<a href='review10.php'>";
echo $row['drug'];
echo "</a>";
}
echo "hai";
Here when a list of drugs are displayed, when a user clicks on any drug name, then the details of all the customers who entered the drug name and their uses should be displayed. I am using PHP/MYSQL/WAMP.
Use This Code
$con = mysqli_connect("localhost","root","","project");
mysqli_select_db($con,"project");
$result = mysqli_query($con,"select distinct drug from drugs");
while($row = mysqli_fetch_array($result))
{
echo "<a href='review10.php'> '".$row['drug']."'</a>";
}
I am trying to display an image coming from the database and I was able to get it but the problem is, it's displaying same images even though I uploaded different images.
Here's my code:
<?php
$con = mysql_connect('localhost','root','')
or die(mysql_error());
mysql_select_db ("dbname");
$query = "SELECT * FROM news ORDER BY date DESC";
$result = mysql_query($query);
echo "<table align='center'>";
while($row = mysql_fetch_assoc($result))
{
echo "<tr>";
echo "<td>";
echo "<img src='getImage.php?id='".$row['id']."' height='230px' width='300px'> <br /><br />";
//Some codes here...
echo "</table>";
mysql_close($con);
?>
getImage.php
<?php
$con = mysql_connect('localhost','root','')
or die(mysql_error());
mysql_select_db ("dbname");
$query = "SELECT * FROM news ORDER BY date DESC";
$result = mysql_query($query);
header("Content-type: image/png");
while($row = mysql_fetch_assoc($result))
{
echo $row['image'];
}
mysql_close($con);
?>
Pls. help me...
Because I'm lazy, I'm going to assume id is numeric here.
In getImage.php:
We need to tell getImage.php to just get the image that has the ID we want.
$id = intval($_GET['id']);
//Forces $id to be numeric. You wouldn't have to worry about doing this if
//you used prepared statements. Like those in John Conde's links.
$query = "SELECT * FROM news WHERE `id`=$id";
As it is now, getImage.php is actually outputting all your images. You only see the one with the latest date however. This is because it's the first retrieved by your script thanks to the ORDER BY clause.
Also, in your display loop, change this:
echo "<img src='getImage.php?id='".$row['id']."' height='230px' width='300px'> <br /><br />";
to this:
echo "<img src='getImage.php?id=".$row['id']."' height='230px' width='300px'> <br /><br />";
Removed the extra single quote between id= and it's number, this way the number will actually be sent to your script.
Your problem is the getImage.php does not return the corresponding image with id in query string. Your source codes always return the image of latest news, right?
Try to replace this :
$query = "SELECT * FROM news ORDER BY date DESC";
to
$query = "SELECT * FROM news WHERE id = {$_GET['id']} LIMIT 1";
Hello I am trying to understand why this isn't working on my page. I am using the php block below to use the variable I created at the beginning of my page $sel_subj (I used $_GET to get the id of what I clicked on on the previous page. I want the new page to reflect the data of the link I clicked on) I got the url to work to show the correct number from the database but I cannot get my page to display the name of what link was pressed; aka the data in the column labeled 'subject_name' from the 'subjects' table.
<?php
$query = "SELECT * FROM subjects WHERE id = \"$sel_subj\"";
$result_set = mysql_query($query, $connection);
if(!$result_set) {
die("Database query failed: " . mysql_error());
$subject = mysql_fetch_array($result_set);
return $subject;
?>
into this h2 tag right here.
<h2><?php echo $subject['subject_name']; ?>Hello</h2>
I can post the whole page if it will help. I appreciate everyone's input.
Thank you.
edit:new problems
Here is the bottom half of my code. I have a navigation div above this code which separates the links to the pages that relate to the database tables from the content that I'd like to pull from the db and display in the div for the page table.
However with the code I've provided nothing is showing up on the page when I open it in firefox. In my html when I "view source" while previewing on the testing server there is nothing underneath ...
<td id="page">
<?php
$query = "SELECT * FROM subjects WHERE id ='$sel_subj'";
$result_set = mysql_query($query, $connection);
if(!$result_set)
die("Database query failed: " . mysql_error());
$subject = mysql_fetch_assoc($result_set);
return $subject;
?>
<h2><?php echo $subject['subject_name']; ?>Hello</h2>
<br />
<?php echo $sel_page; ?><br />
<?php echo $sel_subj; ?><br />
<?php echo $subject; ?><br />
<?php echo $subject['id']; ?>
</td>
Change this:
$query = "SELECT * FROM subjects WHERE id = \"$sel_subj\"";
to this:
$query = "SELECT * FROM subjects WHERE id = '$sel_subj'";
and this:
$subject = mysql_fetch_array($result_set);
to this:
$subject = mysql_fetch_assoc($result_set);
PS: Try not to use the mysql class of functions anymore, they're not too good. Instead, use mysqli or PDO.
EDIT
If the column id is of numeric type, remove the apostrophes from the query. Like this:
$query = "SELECT * FROM subjects WHERE id = $sel_subj";
Try This
$query = "SELECT * FROM subjects WHERE id = '$sel_subj' ";
Try this
$query = "SELECT * FROM subjects WHERE id = {$sel_subj} ";
For example, I have created two pages and two MySQL tables.
Index.php & citys.php
citys
ID City Country Population
--------------------------------------
1 Amsterdam NL 1500000
2 Rotterdam NL 900000
3 Dusseldorf DE 1800000
comments
ID City Name Comment
---------------------------------
1 Dusseldorf Jack Great city!
2 Dusseldorf John Beautiful
3 Rotterdam Emy Love it
At the moment I only use the table citys like this:
index.php linking to citys.php with:
<a href='citys.php?cmd=menu&id=";echo $row['id'];echo "'>
And citys.php use this code to show the data from MySQL:
<?php
include "connect.php";
if(!isset($cmd))
{
if($_GET["cmd"]=="menu" || $_POST["cmd"]=="menu")
{
if (!isset($_POST["submit"]))
{
$id = $_GET["id"];
$sql = "SELECT * FROM citys WHERE id=$id";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
?>
<?php echo $row["City"] ?>
<br><br>
<?php echo $row["Country"] ?>
<br><br>
<?php echo $row["Population"] ?>
Until here everything is showing up and working fine.
But I also want to show the comments on page 2. So the query has to be edited to also get the right data from the table comments.
I tried different examples from internet that I have edited myself like:
<?php
include "connect.php";
if(!isset($cmd))
{
if($_GET["cmd"]=="menu" || $_POST["cmd"]=="menu")
{
if (!isset($_POST["submit"]))
{
$id = $_GET["id"];
$sql = "SELECT citys.*, comments.* FROM citys, comments WHERE citys.id=$id AND comments.city=citys.city";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
?>
But nothing works.
How can I fix this?
The query from VIPIN JAIN's answer works, but there is one problem left:
Query:
$sql = "SELECT * FROM citys LEFT JOIN comments ON comments.city=citys.city WHERE citys.id=$id";
If the table 'comments' has three rows, this code shows only the last two but not the first:
<?php
while($row = mysql_fetch_array($result)) {
echo "<br><br>";
echo $row['name'];
echo "<br>";
echo $row['comment'];
}
?>
And if I try this it only shows the first row.
<?php echo $row["name"] ?>
<br>
<?php echo $row["comment"] ?>
I don't know why the first record is left away in the loop.
Use this query
$sql = "SELECT * FROM citys LEFT JOIN comments ON comments.city=citys.city WHERE citys.id=$id";
Use leftjoin for this type of work
select * from citys
left join comments on comments.city = citys.city
where citys.id=$id