Having trouble getting id from while loop to query - php

I have a bunch of items I list on a page and each of these items have an id associated with them on there link.
For instance, when you hover over the items you get a url of items.php?id=5.
I have my while loop which produces all of the items on this page including the links, how do I carryover that ID into my other code?
Here is my code that brings up all the items.
while ($results = mysqli_fetch_assoc($query)) {
echo
"<li>
<img style='width: 200px; height: 200px' src='images/inventory/" . $results['Image'] . "'/>" .
"<div class='infoContainer'>" .
"<a class='productLink' href='selectItems.php?status=active&id=" . $results['ID'] . "'>" . $results['ProductName'] . "</a>" .
"<br />" .
"<i>Currently Available </i>" .
"<i class='quantity'>" . $results['Quantity'] . "</i>" .
"</div>" .
"</li>";
}
Now I simply want to put each items attributes into a dialog box. How do I carryover an id from above to get the right data back?
<div id="dialog" title="A title">
<?php
$sql = "SELECT * FROM `my_table` WHERE `id` = ";
$query = mysqli_query($sql) or die(mysqli_error());
var_dump($sql);
$results = mysqli_fetch_assoc($query);
var_dump($results);
?>
</div>

You should call a javascript function and send id in it..
donot forget to include the jquery files..
this method loads the data using AJAX..
<script>
function getData(idtosend){
$.post('getdata.php', id: idtosend, function (response){
$("#dialog").html(response);
});
}
</script>
Here is the code of getdata.php file:
<?php
//Connect mysql here
$id = mysql_real_escape_string($_POST['id']);
$sql = "SELECT * FROM `my_table` WHERE `id` = $id";
$query = mysqli_query($sql) or die(mysqli_error());
var_dump($sql);
$results = mysqli_fetch_assoc($query);
var_dump($results);
?>

in PHP you cat use ob_start() - this is output buffer for echo operations.
after save result in variable or array and put it into needful block on page

Related

How to retrieve items from a db and then display one row of data on a different page on button click using php and mysql?

I am having a huge problem. I have displayed data on a single events page the events being displayed are the rows of data in the database.
Here's the code.
<?php
$sql = "SELECT * FROM events ORDER BY `startdate` DESC";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<div class='event col-sm-3'>
<h1>". $row['eventname'] . "</h1>
<p><b>Regular:</b> " . $row['regular'] . "Ksh</p>
<p><b>VIP:</b> " . $row['vip'] . "Ksh</p><br>
<p><b>" . $row['startdate'] . "</b></p>
<p><b>Tickets remaining:<b>" . $row['tickets'] . "</p><br><br>
<button class='button'>book</button>
</div>";
}
}
?>
Here are the events being displayed
Now I would want when I click the button that says book the event the fields(items) in the specific row that I have clicked(using the book button) be displayed in a different page and get stored in variables.
If I understand what you are trying to do, you might want to use an 'a' tag and not a button.
You should pass the 'id' value of the ticket to the page where you want to have an independent query (individual_ticket.php)
Your code should look like this
<?php
$sql = "SELECT * FROM events ORDER BY `startdate` DESC";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<div class='event col-sm-3'>
<h1>". $row['eventname'] . "</h1>
<p><b>Regular:</b> " . $row['regular'] . "Ksh</p>
<p><b>VIP:</b> " . $row['vip'] . "Ksh</p><br>
<p><b>" . $row['startdate'] . "</b></p>
<p><b>Tickets remaining:<b>" . $row['tickets'] . "</p><br><br>
Book
</div>";
}
}
?>
And inside the 'individual_ticket.php'.
Do something like.
<?php
if(isset($_GET['id']))
{
$id = filter_var($_GET['id'], FILTER_SANITIZE_NUMBER_INT, FILTER_FLAG_STRIP_HIGH);
$sql = "SELECT * FROM events WHERE id = '$id'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
//you can start assigning variables here
}
}
}
?>

Passing value from SQL query to another SQL query in php

I want to use a variable that comes from a table i MySQL and pass it to Another SQL-Query with PHP. Can´t get it to work and I can´t find out why.
Here is the code:
<html>
<head><title></title></head>
<body>
<div>
<?php
if (isset($_GET['read_blog_posts_scrolling']))
{
$result = mysql_query("SELECT blogpost.Blogpost_title, blog.Blogwriters_name, blogpost.Date
FROM blog
INNER JOIN blogpost ON blog.BlogID=blogpost.BlogID
WHERE blog.BlogID='$blogs_profile_id' // Here it is, it says undefined variable
ORDER BY blogpost.Date DESC")
or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
echo '<p>';
echo "Titel: " . "<strong>" . $row['Blogpost_title'] . "</strong>" . " - Bloggare " . $row['Blogwriters_name'] . " " . $row['Date'] . '<br />';
echo '<hr />';
echo '</p>';
}
}
?>
<?php
$result = mysql_query("SELECT BlogID, Blogwriters_name FROM blog")
or die(mysql_error());
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$blogs_profile_id = $row['BlogID']; // I want to pass this value to above and use it in the query
echo '<p>';
echo $row['Blogwriters_name'] . '<br />';
//When clicking in this link I want the query to execute and values in BlogID to be passed
echo 'Choose blogwriter';
echo '</p>';
?>
</div>
</body>
</html>
it says the variable is undefined. How can I define it and pass the value when the a href-link is clicked?
Error is clear. Undefined variable:
You didn't defined this variable anywhere
before select statement
$blogs_profile_id
I think you need to add this variable in query string and get from $_GET.
UPDATE 1:
You have following issues in your code.
Missing blog_profile_id in your query string.
Undefined variable means you are using a variable but didn't defined.
Using mysql_* extension its deprecated
Solution:
Replace this:
echo 'Choose blogwriter';
With:
echo 'Choose blogwriter';
And than use that:
if (intval($_GET['blog_id']) > 0)
{
$blogs_profile_id = intval( $_GET['blog_id']);
$result = mysql_query("SELECT blogpost.Blogpost_title, blog.Blogwriters_name, blogpost.Date FROM blog INNER JOIN blogpost ON blog.BlogID=blogpost.BlogID WHERE blog.BlogID=".$blogs_profile_id." ORDER BY blogpost.Date DESC")
or die(mysql_error());
.....
Change the order of your queries. The second query code has to be coming first in order as below
<html>
<head><title></title></head>
<body>
<div>
<?php
$result = mysql_query("SELECT BlogID, Blogwriters_name FROM blog")
or die(mysql_error());
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$blogs_profile_id = $row['BlogID']; // I want to pass this value to above and use it in the query
echo '<p>';
echo $row['Blogwriters_name'] . '<br />';
//When clicking in this link I want the query to execute and values in BlogID to be passed
echo 'Choose blogwriter';
echo '</p>';
?>
<?php
if (isset($_GET['read_blog_posts_scrolling']))
{
$result = mysql_query("SELECT blogpost.Blogpost_title, blog.Blogwriters_name, blogpost.Date
FROM blog
INNER JOIN blogpost ON blog.BlogID=blogpost.BlogID
WHERE blog.BlogID='"+$blogs_profile_id+"' // Here it is, it says undefined variable
ORDER BY blogpost.Date DESC")
or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
echo '<p>';
echo "Titel: " . "<strong>" . $row['Blogpost_title'] . "</strong>" . " - Bloggare " . $row['Blogwriters_name'] . " " . $row['Date'] . '<br />';
echo '<hr />';
echo '</p>';
}
}
?>
</div>
</body>
</html>

php unable to display result webpage

I am having problem trying to get a web url working within php, I am basically getting a list of content displayed, the list content becomes a links to a result webpage (detail.php). I want to display the result webpage with more detailed info. on the same webpage with a back button to original list.
=====
my list webpage code
=====
$c = 0; //Variable to keep count of categories
$servicetype = '$brand'; //variable declaration last displayed servicetype.
$strSQL = "SELECT * FROM <tablename> ORDER BY serviceType, serviceName ASC";
// Execute the query (the recordset $rs contains the result)
$rs = mysql_query($strSQL);
// Loop the recordset $rs
while($row = mysql_fetch_array($rs)) {
//If the servicetype name has changed, display it and update the tracking variable
if ($servicetype != $row['serviceType']){
$servicetype = $row['serviceType'];
//If this isn't the first category, end the previous list.
if ($c>0) echo "</ul>";
echo '<h3>'.$row['serviceType'].'</h3><ul>'; // subheading & related content.
$c++;
}
$strName = $row['serviceName'];
$strLink = "<a href = 'detail.php?id = " . $row['ID'] . "'>" . $strName . "</a>";
// List link
echo "<li>" . $strLink . "</li>";
}
// Close the database connection
mysql_close();
?>
====
my detail.php code below
=====
$strSQL = "SELECT * FROM gu_service_cat WHERE id = " . $_GET["id"];
$rs = mysql_query($strSQL);
// Loop the recordset $rs
while($row = mysql_fetch_array($rs)) {
// Write the detail data of the ID
echo '<h3>ID:</h3>' . $row['ID'] . ' ' . $row['guUrl'] . "</dd>";
echo "<dt>availability:</dt><dd>" . $row["availability"] . "</dd>";
}
//echo '<h3>'.$row['serviceType'].'</h3><ul>';
// Close the database connection
mysql_close();
?>
</dl>
<p>
Return to the list
I do see you incorectly using variable variables.
This is incorrect:
$servicetype = '$brand';
if ($servicetype != $row['serviceType'])
What you need to do is assign the variable $servicetype a string, in this case 'brand' and use that in the if statement:
$servicetype = 'brand';
if ($$servicetype != $row['serviceType'])
Notice the double dollar sign. Read more about Variable Variables.

Mysql Field Data not displaying when a link is clicked?

I'm trying to get data from a database if a link is clicked.
I used the example codes suggested from this example -Getting mysql field data when a link is clicked?
But it doesn't work when I click on a link nothing comes up.
main.php
<?php
include('conn.php');
$sql2 = "SELECT Title FROM addpromo";
$result2 = mysql_query($sql2);
echo "<div id=\"links\">\n";
echo "<ul>\n";
while ($row2 = mysql_fetch_assoc($result2)) {
echo "<li> <a href=\"fullproject.php?title=\""
. urlencode($row2['Title']) . "\">"
. htmlentities($row2['Title']) . "</a>\n</li>";
}
echo "</ul>";
echo "</div>";
?>
This is displaying correct.but when I click at a link nothing is showing up in fullproject.php, Just a blank page.
fullproject.php
<?php
// Connect to server.
include('conn.php');
$projectname = isset($_GET['Title']);
$sql1 = "SELECT Title FROM addpromo WHERE Title = '$projectname'";
$result1 = mysql_query($sql1);
while ($row1 = mysql_fetch_assoc($result1)) {
echo "Project Name: " . $row1['Title'] . "<br />";
echo "<br /> ";
}
?>
Can someone help me to fix this, or any other way to make this(to get data from a database if a link is clicked) possible?
Change to this
main.php
<?php
include('conn.php');
$sql2="SELECT Title FROM addpromo";
$result2=mysql_query($sql2);
echo '<div id="links">';
echo '<ul>';
while($row2 = mysql_fetch_assoc($result2)){
echo '<li>'.htmlentities($row2['Title']).'</li>';
}
echo '</ul>';
echo '</div>';
?>
fullproject.php
<?php
if(isset($_GET['title'])){
include('conn.php');
$projectname= $_GET['title'];
$sql1="SELECT Title FROM addpromo WHERE Title = '$projectname'";
$result1=mysql_query($sql1);
while($row1 = mysql_fetch_assoc($result1)) {
echo "Project Name: " . $row1['Title']. "<br />";
echo "<br /> ";
}
}
?>
This is storing a boolean value $projectname= isset($_GET['Title']);, whether or not the title is set. Instead use $projectname = $_GET['Title'];
isset returns a boolean value (true/false) and you want the actual value of the variable:
$projectname= $_GET['title'];
Furthermore, you have to pass only the title as the URL parameter, without enclosing it within quotes. So there is an error in this line:
echo "<li> <a href=\"fullproject.php?title=" . urlencode($row2['Title']) . "\">"
Note the lack of \" after title=

using dynamic data in a javascript method

I'm new at php and JavaScript so I hope you can help me!
below is a php code! it take data from my db and display my photos and photo's title. what I want to do is if you click on one of this photos and than a new php expe: photo.php opens and you can see the same photo alone not with the other ones.
<?php
$link = mysql_connect("localhost", "root","");
mysql_select_db("test", $link);
$query = "select * from post order by id DESC;";
$results = mysql_query($query, $link) or die ("you comand can't be executed!".mysql_error());
if($results){
$i = 1;
while ($row = mysql_fetch_assoc($results)){
echo $row['title']. "<img src=/1/" . $row['location']. " width=580px > " . "<br /><br /><br />";
}
}
?> '
<?php
$link = mysql_connect("localhost", "root","");
mysql_select_db("test", $link);
$query = "select * from post order by id DESC;";
$results = mysql_query($query, $link) or die ("you comand can't be executed!".mysql_error());
if($results){
$i = 1;
while ($row = mysql_fetch_assoc($results)){
//send photo_id (id of photo from your table) from url NOTE: I've put link here
echo $row['title']. "<a href='index.php?photo_id=".$row['id']."'><img src=/1/" . $row['location']. " width=580px ></a> " . "<br /><br /><br />";
}
if(isset($_GET['photo_id'])) {
//here you get only one id of photo now retrieve the data from database and display the image
}
}
?>
You will get the photo by the help of the url value here photo_id.
do this way you are getting results from database and you are
displaying them with tile and image your aim is to click on
image or image title go to a new page and display image alone
the image title as link in first page
if($results){
$i = 1;
while ($row = mysql_fetch_assoc($results)){
<a href='new page url?id=<?php echo $row["id"] ?>'>
echo $row['title'];
echo "</a>"
}
}
where $row['id'] is database unique id if have no it is always preferred
to create a database with unique id and in new page get the uid
using $id=$_GET['id'] and
do as below
if($results){
$i = 1;
while ($row = mysql_fetch_assoc($results)){
if($id==$roe['id']{
echo $row['title']. "<img src=/1/" . $row['location']. " width=580px > ";
}
}
}

Categories