Query on PHP, web development - php

I have made a connection to mysql database and echoing values from a table.
while($data = mysql_fetch_array($res))
{
?>
<a href="nextpage.php"<?php echo $data['rowname'] ?></a>
<?php
}
?>
Problem is when I click on the particular link, on the nextpage.php, it should display only the result of the value of a href clicked. So on the nextpage.php, I have defined something like SELECT * from tablename where rowname = 'a href value'.
What's happening now is that it displays only the last rowname value regardless of whichever link I click on, very obvious!
I have tried forms, arrays, SESSIONS but to no avail. How do I fix this?

the href should be like this
<?php echo $data['rowname']; ?>
and then on next page you can use $_GET['val'] and pass it to SELECT query

Try example as below
page1.php
while($data = mysql_fetch_array($res))
{
echo "<a href='nextpage.php?id=".$data['rowname']." >". $data['rowname'] ."</a>";
}
?>
your href will look like nextpage.php?id=[your value]
nextpage.php
$qry = mysql_query("select * from [table_name] where id = ".$_GET['id']."",$con);
while($data = mysql_fetch_array($res))
{
echo $data[0];
}
on nextpage pass value using $_GET['id'] to sql query.

Related

how to retrieve the id from link URL to div using $_GET

on the same page I have
$sql = "SELECT * FROM `joedoe` ";
$result = mysql_query($sql);
$id = 'id';
<?php echo "<a href=\'page.php?id=$rows[$id]\'>View</a>" ?>
<?php echo " <div id=\'productId\' >$_GET[id]</div>" ?>
please tell me, how to retrieve the id from link to div using $_GET
You need single quotes inside your array selector, around id index...
$_GET['id']

PHP and MySQL SESSION, taking last row

I'm showing a data table from MySQL, let say in x.php like
<?php $sql = "SELECT * FROM tblname";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$id=$row["id"];
echo "<tr><td>". $row["0"]. "</td><td>". $row["1"]."</td><td>".$row["2"]."</td><td>".$row["3"]."</td><td>"."<a href='y.php?=$id'>More details</a>"."</td></tr>";
$_SESSION["id"]=$id;
}
} else {
}?>
But where I go to y.php class to see more details it shows data only from last row of MySQL records.
How can I fix it?
What's wrong is you are using the $_SESSION to fetch the detail.
What you want isn't to store the id in the $_SESSION, you want to put it in each link, so that when a user clicks a link he has access to the row detail.
Basically, parameters passed in a link (like index.php?param1=value1) are found in $_GET global variable.
So I guess in y.php you use $_SESSION['id'] to fetch the row detail, use $_GET['id'] instead.
Also, in x.php edit this :
"<a href='y.php?=$id'>More details</a>"
to :
"<a href='y.php?id=$id'>More details</a>"
And remove : $_SESSION['id'] = $id;

How to determine what ID has been clicked to display relevant information? PHP / SQL

I currently have this code which displays all required information on the page:
$sql = "select * from livecalls ORDER BY Completion_Date ";
$query = mysql_query( $sql );
while( $row = mysql_fetch_assoc($query) )
{
echo "<tr><td>$row[ID]</td>";
echo "<td>$row[Type]</td>";
echo "<td>$row[VNC_Number]</td>";
echo "<td>$row[Completion_Date]</td>";
echo "<td>$row[Logged_By]</td></tr>";
}
echo "</table>";
This works fine, however I am wanting to be able to click the $row[ID] section to open a new window and display the $row[Problem] which is related to that ID number..
I'm struggling to think how to get the ID information across to a new page to be able to search for the right Problem information to display and the code to do this?
Any help would be appreciated.
You can use a anchor tag for this purpose like
echo "<tr><td><a href='yourpage.php?id=".$row[ID]."'>".$row[ID]."</a></td>";
and in new page, you can use
$_GET['id'] for getting the id.
Also while echoing html along with php variable try to do like this:
while( $row = mysql_fetch_assoc($query) )
{
echo "<tr><td><a href='yourpage.php'>".$row['ID']."</a></td>";
echo "<td>".$row['Type']."</td>";
echo "<td>".$row['VNC_Number']."</td>";
echo "<td>".$row['Completion_Date']."</td>";
echo "<td>".$row['Logged_By']."</td></tr>";
}
I guess your not using any php framework (ex. codeigniter). Because if you do,this is easy using routing.
The very basic solution is Passing variables in a URL
http://html.net/tutorials/php/lesson10.php
Then if you want it to open in a new window instead of current window use
Use _blank in your a href
Visit W3Schools!
$sql = "select * from livecalls ORDER BY Completion_Date ";
$query = mysql_query( $sql );
while( $row = mysql_fetch_assoc($query) )
{
echo "<tr><td><a href='your_url/?id=".$row[ID]."'>$row[ID]</a>`enter code here`</td>";
echo "<td>$row[Type]</td>";
..
...
....

Put in URL an mysql_fetch_array item

I have a mysql table I want to echo the name of every user and on click on a certain user show his profile:
$user=mysql_query("SELECT * FROM signup ORDER BY name ASC ");
while($row = mysql_fetch_array($user)){
echo ''.$row['nume'].' '.$row['prenume']."";
echo "<br>";}
The problem is that i can not figure out how to concatenate $row['username'] with href
The output should look like this: www.abcde.de/profil?user=username
Try this
echo ''.$row['nume'].' '.$row['prenume'].'';

Is it possible to Query a Mysql database from a field selected from dropdown menu populated from a Query in php

Hello i am new to php and i have tried to find a piece of code that i can use to complete the task i need, i currently have a page with a form set out to view the criteria of a course. also i have a dropdown menu which currently holds all the course codes for the modules i have stored in a database. my problem is when i select a course code i wish to populate the fields in my form to show all the information about the course selected. The code i am trying to get to work is as follows:
<?php
session_start();
?>
<? include ("dbcon.php") ?>
<?php
if(!isset($_GET['coursecode'])){
$Var ='%';
}
else
{
if($_GET['coursecode'] == "ALL"){
$Var = '%';
} else {
$Var = $_GET['coursecode'];
}
}
echo "<form action=\"newq4.php\" method=\"GET\">
<table border=0 cellpadding=5 align=left><tr><td><b>Coursecode</b><br>";
$res=mysql_query("SELECT * FROM module GROUP BY mId");
if(mysql_num_rows($res)==0){
echo "there is no data in table..";
} else
{
echo "<select name=\"coursecode\" id=\"coursecode\"><option value=\"ALL\"> ALL </option>";
for($i=0;$i<mysql_num_rows($res);$i++)
{
$row=mysql_fetch_assoc($res);
echo"<option value=$row[coursecode]";
if($Var==$row[coursecode])
echo " selected";
echo ">$row[coursecode]</option>";
}
echo "</select>";
}
echo "</td><td align=\"left\"><input type=\"submit\" value=\"SELECT\" />
</td></tr></table></form><br>";
$query = "SELECT * FROM module WHERE coursecode LIKE '$Var' ";
$result = mysql_query($query) or die("Error: " . mysql_error());
if(mysql_num_rows($result) == 0){
echo("No modules match your currently selected coursecode. Please try another coursecode!");
} ELSE {
Coursecode: echo $row['coursecode'];
Module: echo $row['mName'];
echo $row['mCredits'];
echo $row['TotalContactHours'];
echo $row['mdescription'];
echo $row['Syllabus'];
}
?>
however i can only seem to get the last entry from my database any help to fix this problem or a better way of coding this so it works would be grateful
Thanks
The main error is in your final query, you're not actually fetching anything from the query, so you're just displaying the LAST row you fetched in the first query.
Some tips:
1) Don't use a for() loop to fetch results from a query result. While loops are far more concise:
$result = mysql_query(...) or die(mysql_error());
while($row = mysql_fetch_assoc($result)) {
...
}
2) Add another one of these while loops to your final query, since it's just being executed, but not fetched.
For me i would use some javascript(NOTE: i prefer jQuery)
An easy technique would be to do this(going on the assumption that when creating the drop downs, your record also contains the description):
Apart from creating your dropdown options like this <option value="...">data</option>, you could add some additional attributes like so:
echo '<option value="'.$row['coursecode'].'" data-desc="'.$row['description'].'">.....</option>
Now you have all your drop down options, next is the javascript part
Let's assume you have included jQuery onto your page; and let's also assume that the description of any selected course is to be displayed in a <div> called description like so:
<div id="course-description"> </div>
<!--style it how you wish -->
With your javascript you could then do this:
$(function(){
$("#id-of-course-drop-down").change(function(){
var desc = $(this).children("option").filter("selected").attr("data-des");
//now you have your description text
$("#course-description").html(desc);
//display the description of the course
}
});
Hope this helps you, even a little
Have fun!
NOTE: At least this is more optimal than having to use AJAX to fecch the description on selection of the option :)

Categories