php wont echo results from inner join query - php

I've looked around the site for similar problems with solutions and non seem to fix the problem I have. I have two tables; "friendlist" and "users". I'm trying to use the "FriendID" from "friendlist" to retrieve information from the "users" table. Everything works fine up until the while($row = mysqli_fetch_array($result)){} loop then nothing else prints.
My code is as follows:
$query = "SELECT friendlist.FriendID, users.Name, users.Surname, users.Picture
FROM friendlist
INNER JOIN users
ON friendlist.FriendID = users.Id
WHERE friendlist.UserId ='".$id."'";
$result = mysqli_query($con, $query);
if(!$result){
echo "<br/><h4>You currently do not have any friends. Please click the Find Friends button to find a friend</h4>";
}else{
echo "<center><br/>Here is a list of all your friends:<br/>";
echo "<table>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>Pro Pic: <img style='width:200px; height:200px' alt='No Profile Picture' src='uploads/" .$row['Picture']. "' /></td>";
echo "<td>Name :" .$row['Name']. "</td>";
echo "<td>Surname :" .$row['Surname']. "</td>";
echo "<td><form method='post' action='viewFriend.php'>";
echo "<input type='hidden' name='friendId' value='".$row['FriendID']."'/>";
echo "<input type='submit' name='View' value='View Profile'/>";
echo "</form></td>";
echo "</tr>";
}
echo "</table></center>";
}
Nothing is displayed on the browser. Only the level 4 heading text: "Here is a list of all your friends" shows. But after that its empty space.
I've checked the sql query on mySql and it works perfectly fine. I have no idea what's wrong. Any help will be much appreciated. Thanks

Your code is correct but you miss the declaration of $id variable only.
Use like below.
//initialize the $id as.
$id = $_REQUEST['id'];
$query = "SELECT friendlist.FriendID, users.Name, users.Surname, users.Picture
FROM friendlist
INNER JOIN users
ON friendlist.FriendID = users.Id
WHERE friendlist.UserId ='".$id."'";
$result = mysqli_query($con, $query);
if(!$result){
echo "<br/><h4>You currently do not have any friends. Please click the Find Friends button to find a friend</h4>";
}else{
echo "<center><br/>Here is a list of all your friends:<br/>";
echo "<table>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>Pro Pic: <img style='width:200px; height:200px' alt='No Profile Picture' src='uploads/" .$row['Picture']. "' /></td>";
echo "<td>Name :" .$row['Name']. "</td>";
echo "<td>Surname :" .$row['Surname']. "</td>";
echo "<td><form method='post' action='viewFriend.php'>";
echo "<input type='hidden' name='friendId' value='".$row['FriendID']."'/>";
echo "<input type='submit' name='View' value='View Profile'/>";
echo "</form></td>";
echo "</tr>";
}
echo "</table></center>";
}

The problem is on your SQL Query.
You did not put friendlist.UserId on the select part.
That's why the where clause do not see where to compare it.
$query = "SELECT friendlist.FriendID, users.Name, users.Surname, users.Picture
FROM friendlist
INNER JOIN users
ON friendlist.FriendID = users.Id
WHERE friendlist.UserId ='".$id."'";

Related

PHP Extracting MYSQL Value Issue

I have 2 tables: users and transactions
In users I have: userid, name, email
And in transactions I have: id, idsender, idreceiver
I want to make a log-table showing all the transactions and I want them to be displayed like: Transaction ID - SENDER'S NAME - RECEIVER'S NAME
I tried like this but it doesn't seem to work, at "Receiver" it doesn't show anything .. :
echo "<table>";
echo "<tr>";
echo "<th>Transaction ID</th>";
echo "<th>Sender</th>";
echo "<th>Receiver</th>";
echo "</tr>";
$resultuser = mysqli_query($conn, "SELECT * FROM users");
$rowuser=mysqli_fetch_array($resultuser);
$resulttrans = mysqli_query($conn, "SELECT * FROM transactions");
$rowtrans=mysqli_fetch_array($resulttrans);
$ress=mysqli_query($conn, "SELECT * FROM transactions");
while($row=mysqli_fetch_array($ress)){
echo "<tr>";
echo "<td>".$row['id']."</td>";
echo "<td>"." same as receiver"."</td>";
$receiver=mysqli_query($conn, "SELECT name FROM users WHERE userid='" . $rowtrans['idreceiver'] . "'");
$receivername = mysqli_fetch_array($receiver);
echo "<td>". $receivername ."</td>";
echo "</tr>";
}
Don't use two queries. Join the two tables in one query:
$ress = mysqli_query("SELECT t.id, u1.name AS sendername, u2.name AS receivername
FROM transactions AS t
JOIN users AS u1 ON u1.userid = t.idsender
JOIN users AS u2 ON u2.userid = t.idreceiver");
while ($row = mysqli_fetch_assoc($ress)) {
echo "<tr>";
echo "<td>".$row['id']."</td>";
echo "<td>" . $row['sendername'] ."</td>";
echo "<td>" . $row['receivername'] . "</td>";
echo "</tr>";
}
You are echoing from fetched array in $receivername = mysqli_fetch_array($receiver);
you should do echo "<td>". $receivername['name'] ."</td>";
Note:
Fetched array from mysqli_fetch_array() can be echoed using indexes by number $receivername[0] or by string $receivername['name'], that is where you are missing here.

Only show result of sql query on database table1 if there is a value in table 2

I am busy creating a website that allows people to host dinners and allows users to join those dinners. I have two different database tables called gebruikers (users) and diner (dinner). I am working on a search page that makes it possible for a user to search on a woonplaats (city) and then shows the users living in that city. That works right now, but I want to make sure that only the users are shown who have added a dinner. There is a common key in both tables: diner.gebruikersid = gebruikers.id
So in other words, I only want the users with their ID in both the diner and the gebruikers table to be displayed in the search results. How can I do that?
I have this code at the moment:
$zoeken = $_POST['woonplaats'];
$sql = "SELECT id, voornaam, straatnaam FROM gebruikers WHERE woonplaats LIKE '$zoeken'";
$result = mysql_query($sql);
echo "<div class=res>";
echo "<h3>Resulaten voor " .$zoeken. "</h3>";
echo "<table>";
echo "<thead>";
echo "<tr>";
echo "<th>Voornaam</th>";
echo "<th>Straatnaam</th>";
echo "<th></th>";
echo "</tr>";
echo "</thead>";
while($row = mysql_fetch_assoc($result)) {
echo "<tbody>";
echo "<tr>";
echo "<td>".$row['voornaam']."</td>";
echo "<td>".$row['straatnaam']."</td>";
echo "<td>";
echo "<form action='eetprofiel.php' method='post'>";
echo "<input type='hidden' name='id' value='".$row['id']."'>";
echo "<input type='submit' value='Profiel bekijken'>";
echo "</form>";
echo "<td>";
echo "</tr>";
echo "</tbody>";
}
echo "</table>";
echo "</div>";
?>
Edit:
These are all the users in my database (in table gebruikers) with woonplaats Amersfoort
These are all the diners submitted by those users.
As you see, the user with gebruikers.id and diner.gebruikersid 23 has no dinner submitted, so I don't want him to be shown in the search results.
You should join the 2 tables. With JOIN take a look at this
https://www.tutorialspoint.com/mysql/mysql-using-joins.htm
example of a join
select tableA.user ,tableB.user from tableA JOIN tableB on tableA.id = tableB.id
note: don't use mysql it is deprecated use mysqli or PDO instead

In php-mysql, How can I union the results of the sql query in a cell of the result table?

I am making the code of PHP-MySQL. But I gain have the 2 and more as the result of the left join in the MySQL. So I want to union the results in a cell of the result table as the captured picture.
Here is my real captured picture in my computer.
My php code is as like below.
$result = mysqli_query($con,$sql);
while($row = mysqli_fetch_array($result))
{
echo "<table border='1' style='background:#dddddd;border-color:green;'>";
echo "<h2><p >진료과 (Subject) : ".$row['subject']."</p></h2>";
echo "<tr>";
echo "<th >"."<form action='search1.php' method='get'>"."<button type='submit'
name='code' value='".$row['code']."'>Code</th>";
echo "<th ><a href='".$row['ds_url']."'>"."한국병명</a></th>";
echo "<th ><a href='".$row['ds_url']."'>"."Disease name(En.)</a></th>";
echo "<th >"."<form action='search1.php' method='get'>"."<button type='submit'
name='code' value='".$row['family']."'>"." Family History</button> </th>";
echo "</tr>";
echo "<tr>";
echo "<td >" . $row['code'] . "</td>";
echo "<td >" .$row['disease_co']."</td>";
echo "<td >" .$row['disease_en']."</td>";
echo "<td >" .$row['family']."</td>";
echo "</tr>";
---
echo "<tr>";
echo "<th>소아과</th>";
echo "<th>내과</th>";
echo "<th>산과</th>";
echo "<th>정형외과</th>";
echo "</tr>";
echo "<tr>";
echo "<td>".$row['ped_content']."</td>";
echo "<td>".$row['im_content']."</td>";
echo "<td>".$row['ob_content']."</td>";
echo "<td>".$row['os_content']."</td>";
echo "</tr>";
----
echo "</table>";
}
The While phrase in the above php code is just 1, but the number of the result table depend on the number of im_content.
So, I want to union the result of im_content in a cell as the above captured picture.
But, I am short of ability.
Please give me a piece of advice.
Thank you for your concern.
My sql query sentence is as like below.
SELECT code_en.code, code_co.disease_co, code_en.disease_en , hx.family,
hx.personal, note.note, inclusion.inclusion, exclusion.exclusion, ds.ds_content,
------
subject.icd_category, subject.group_code, im.im_content
FROM code_en
LEFT JOIN subject ON code_en.code = subject.code
LEFT JOIN note ON code_en.code = note.code
-------
left join im on code_en.code = im.code
WHERE code_en.code = '".$code."'"
Thank you, Barmar!
I'm not totally sure I understand the question, but I think this is what you want:
SELECT code_en.code, code_co.disease_co, code_en.disease_en , hx.family,
hx.personal, note.note, inclusion.inclusion, exclusion.exclusion, ds.ds_content,
------
subject.icd_category, subject.group_code, GROUP_CONCAT(im.im_content) AS im_content
FROM code_en
LEFT JOIN subject ON code_en.code = subject.code
LEFT JOIN note ON code_en.code = note.code
-------
left join im on code_en.code = im.code
WHERE code_en.code = '".$code."'"

PHP SELECT acting strange

I have a problem with PHP and Mysql. PHP is acting very strange. This is my code
echo "<form action='scripts/leerling.php' method='post'>";
echo "Nieuwe Leerling: <br/><br/>";
echo "Naam<br/>";
echo "<input type='text' name='naam'/><br/><br/>";
echo "Leeftijd<br/><input type='number' name='leeftijd'/><br/><br/>";
echo "Ouder:<br/>";
echo "<select name='ouder'>";
$result2 = mysqli_query($con, "SELECT * FROM users WHERE group=3");
while($record2 = mysqli_fetch_array($result2)){
echo "<option value='" . $record2["id"] . "'>" . $record2["username"] . "</option>";
}
echo "</select><br/> <br/>";
echo "<input type='image' src='img/plus.png'/><span style='font-size: 11pt;'> Leerling Toevoegen</span>";
echo "</form>";
i think everyting is allright. I want to make a selectbox with variable options. Now comes the annoying part: if i change this:mysqli_query($con, "SELECT * FROM users WHERE group=3") To this:mysqli_query($con, "SELECT * FROM users WHERE id=3") it works! and i dont know why... My table of my database sure has a column named ID AND a column named group and they are both the datatype INT but ID is also A_I. I dont know if that matters...
Perhaps because GROUP is a reserved word in MySQL? Try this:
$result2 = mysqli_query( $con, "SELECT * FROM users WHERE 'group' = 3" );

Handling a radio button array in php

I have a bit of PHP code that is really taking a toll on me. To simplify, I have a table where three of the columns are foreign keys (event_type, accommodation_type and public_user). I am dynamically creating a set of radio buttons and I have set the ID attribute on each input value correctly.
$result2 = mysql_query("SELECT * FROM event_type", $connection);
if(!$result2){
die("Database query failed: " . mysql_error());
}
$event = mysql_fetch_array($result2);
echo "<input type='radio' name='event_type' value='";
echo $event["id"];
echo "' > ";
echo $event['name'];
echo "</input><br /><br />";$result4 = mysql_query("SELECT * FROM accommodation_type", $connection);
if(!$result4){
die("Database query failed: " . mysql_error());
}
while($row = mysql_fetch_array($result4)){
if($row["id"] == 7 || $row["id"] == 8){
echo"<tr><td>";
echo $row["name"] ;
echo"</td><td>$";
echo $row["price"];
echo ".00</td><td>";
echo "<input type='radio' name='accommodation_type' value='";
echo $row["id"];
echo "' />";
echo "</td></tr>";
}
}
I retrieved the correct id from the query. Thus after submitting the form with POST, I go on to do some minor validation and prepped the names from my post as follows:
$event_type = trim(mysql_prep($_POST['event_type']));
$accommodation_type= trim(mysql_prep($_POST['accommodation_type']));
$public_user = trim(mysql_prep($_POST['public_user']));
$comments = trim(mysql_prep($_POST['comments']));
$grand_total = trim(mysql_prep($_POST['grand_total']));
I then proceeded to write insert statements to insert the data into the relevant tables. This requires two queries as follows.
$query = "INSERT INTO event_registration (event_type, accommodation_type, public_user, comments, grand_total) VALUES ('{$event_type}','{$accommodation_type}','{$public_user}','{$comments}','{$grand_total}')";
$query1 = "INSERT INTO additional_member (first_name, last_name, gender, age_group, public_user) VALUES ('{$first_name}','{$last_name}','{$gender}','{$age_group}','{$public_user}')";
$result = mysql_query($query, $connection);
$result1 = mysql_query($query1, $connection);
The query1 works as expected, however the first query fails to insert the data. It says undefined index at the lines where I have
$event_type = trim(mysql_prep($_POST['event_type']));
$accommodation_type= trim(mysql_prep($_POST['accommodation_type']));
I am not entirely sure where things went wrong. Everything seems to be set up correctly and the data from the form just refuses to be saved.
Any ideas ?
why not try reviewing the code from scratch and see if there are any syntax errors and then move on.
You are creating accommodation type
echo "<input type='radio' name='accommodation_type' value='";
echo $row["id"];
But not echoing event_type anywhere. Try echoing that too:
echo "<input type='radio' name='accommodation_type' value='";
echo $row["id"];
echo "<input type='radio' name='event_type' value='";
echo $row["whatever"];

Categories