unable to retrieve data from mysql to my webpage - php

I want to create an automatic cms in my website using php and mysql
i.e. I would just feed data to mysql table and it will generate result.
so my code is:
<!DOCTYPE html>
<html>
<head>
<?php
include 'head.php';
include 'conct.php';
?>
<title>GIZMOMANIACS|DOWNLOADS</title>
</head>
<body>
<div id="container" style="width:100%;height:100%;clear:both">
<div id="header" style="background-color:#FFFFFF;">
<div class="head" style="clear:both">
<a href= "http://gizmomaniacs.site88.net">
<img src="/GM%203D.gif" width= "200" height="100" alt='gizmomaniacs logo'></a></div>
<h1 style="margin-bottom:0; float:right"><font id="gmfont">GIZMOMANIACS</font></h1>
</div>
<div id="menu" style="clear:both;background-color:#0762AE">
<?php include 'head.html'; ?></div> <div class="content" >
<?php include 'search.php'; ?>
<ul>
<?php
$sql = "SELECT * from downloads";
$result = mysql_query($sql);
if($result==false){
$view = "error";
$error = "Could not retrieve values";
}
else {
$dload = $GET_['downloadname'];
$imagelink = $GET_['imagelink'];
$title = $GET_['title'];
while ($row = mysql_fetch_array($result))
{
echo "<li><a class =\"alldld\" href=\"$dload\" title=\"$title\"><img class=\"downloads\" src =\"$imagelink\"/>$title</a><br></li>";
}
}
?>
</ul>
</div>
<div class="social">
<?php
include 'social.php';
?></div>
</div>
</body>
</html>
everything is gong well but three thing are not happening they are:
a href atrribute not retreiving from db
a title atrribute not retreiving from db
img src atrribute not retreiving from db
in the actual source it is showing
<a class ="alldld" href="" title=""><img class="downloads" src =""/></a><br></li>
<li><a class ="alldld" href="" title=""><img class="downloads" src =""/></a>
the src href title attribute are blank
so what to do?

change this part of code
$dload = $GET_['downloadname'];
$imagelink = $GET_['imagelink'];
$title = $GET_['title'];
while ($row = mysql_fetch_array($result))
{
echo "<li><a class =\"alldld\" href=\"$dload\" title=\"$title\"><img class=\"downloads\" src =\"$imagelink\"/>$title</a><br></li>";
}
to
while ($row = mysql_fetch_array($result))
{
$dload = $row['downloadname'];
$imagelink = $row['imagelink'];
$title = $row['title'];
echo "<li><a class =\"alldld\" href=\"$dload\" title=\"$title\"><img class=\"downloads\" src =\"$imagelink\"/>$title</a><br></li>";
}
While retrieving from DB you need to use the array in which the values are fetched and not the $_GET array. also there is no such thing as $GET_.
Important Note: Stop using mysql_ function and start using mysqli_* functions or PDO.For more info see here

You're outputting the rows like this:
$dload = $GET_['downloadname'];
$imagelink = $GET_['imagelink'];
$title = $GET_['title'];
while ($row = mysql_fetch_array($result))
{
echo "<li><a class =\"alldld\" href=\"$dload\" title=\"$title\"><img class=\"downloads\" src =\"$imagelink\"/>$title</a><br></li>";
}
}
There's a couple of issues with that. First of all, if you wanted to retrieve it from the query string, you'd want to use $_GET, not $GET_. Second, you don't want to retrieve it from the query string with $_GET; you want to retrieve it from $row. Thirdly, you need to put it inside the while loop. Once you've fixed that, it should work.

$_GET is the array containing values passed in the URL.
<?php
// Let's take the following URL
// http://localhost/index.php?name=John&age=20
echo $_GET["name"]; // Will display 'John'
echo $_GET["age"]; // Will display '20'
?>
When you select something from the database, the variable $row contains each line of the results that you get using the function mysql_fetch_array. Try this to display your data :
<?php
// ...
while ($row = mysql_fetch_array($result)) {
$dload = $row["downloadname"];
$imagelink = $row["imagelink"];
$title = $row["title"];
echo "<li><a class =\"alldld\" href=\"$dload\" title=\"$title\"><img class=\"downloads\" src =\"$imagelink\"/>$title</a><br></li>";
}
?>

Related

How to echo each row from mySQLi query using PHP?

The following query returns all rows from the campaign table that have the user_id of 1:
SELECT * FROM campaign WHERE user_id=1
In the case of testing this is two results. How would I be able to echo a set column from each of the rows. For example, I want to echo the campaign_name from each of the results. I have tried various methods however, I have had no success.
My end goal would be something like this:
<?php foreach($queryRow as $row) { ?>
<li>
<a>
<div>
<p><?php echo($row['campaign_name']); ?></p>
<p>
Description Text
</p>
</div>
</a>
</li>
<?php } ?>
I'm quite at lost with this so I apologise if my intended result is completely off...
Try this:
$qry = "SELECT * FROM campaign WHERE user_id=1";
$res = mysqli_query($conn, $qry);
if(mysqli_num_rows($res) > 0) // checking if there is any row in the resultset
{
while($row = mysqli_fetch_assoc($res)) // Iterate for each rows
{
?>
<li>
<a>
<div>
<p><?php echo($row['campaign_name']); ?></p>
<p>
Description Text
</p>
</div>
</a>
</li>
<?php
}
}
It will iterate for each row in the resultset.
I looked into the documentation as Oldskool kindly suggested and realised I was using the wrong method to create an array. I instead use the fetch_all feature of the mysqli_results class to create a multidimensional array with my results. Then I was able to use the following code to echo out the results:
<!DOCTYPE html>
<html>
<body>
<?php
include('inc/database_initiation.php');
//print_r($userCampaigns);
foreach ($userCampaigns as $row) {
//echo $row[2];
//echo $row[4];
echo $row['campaign_name'];
echo '<br>';
echo $row['promotion_coins'];
echo '<br>';
}
?>
</body>
</html>
The include 'inc/database_initiation is as follows'
<?php
session_start();
include_once 'userManagement/dbconnect.php';
if(!isset($_SESSION['userSession']))
{
header("Location: login.php");
}
$query = $MySQLi_CON->query("SELECT * FROM users WHERE user_id=".$_SESSION['userSession']);
$userRow=$query->fetch_array();
$campaignQuery = $MySQLi_CON->query("SELECT * FROM campaign WHERE user_id=1");
$userCampaigns = $campaignQuery->fetch_all(MYSQLI_ASSOC);
//$MySQLi_CON->close();
?>

Can i get only the img from a database field where different html tags are used?

I have this php array. Where in my database i have a field called body, and in that field there is some html code. Like this:
<h1>title</h1><img src="http://farm5.staticflickr.com/4075/4788694752_d03557765b_z.jpg" alt=""/>
Here is my php code:
<?php
$q = "SELECT * FROM journals ORDER BY timestamp DESC";
$r = mysqli_query($dbc, $q);
while($journal_list = mysqli_fetch_assoc($r)) { ?>
<div class="col-md-4">
<a class="list-group-item" href="journal.php?id=<?php echo $journal_list['id']; ? >">
<h4 class="list-group-item-heading"><?php echo $journal_list['body']; ยด?></h4>
</a>
</div>
<?php } ?>
In the h4 im calling the body field in the database. But i only want the img in that field??
Try phpquery:
$src = phpQuery::newDocumentHTML($journal_list['body'])->find('img')->attr('src');
This is a little hack that you can use
$img = explode('img',$journal['body']);
$final_img = '<img '.$img[1];
Now echo image as
<h4 class="list-group-item-heading"><?php echo $final_img; ?></h4>

undefined index when trying to display number from database

i am trying to display the isbn number from my database when they click an image from another page and for some reason i keep getting this error
Notice: Undefined index: isbn on line 8
<html><title>Employee List</title> <head></head>
<body>
<h1> List of Employees </h1>
<?php
require_once("include/db_connect.php");
$db_link = db_connect("project");
$isbn = $_REQUEST['isbn'];
$query = "SELECT * FROM bookstore WHERE isbn = '$isbn'";
$result = mysql_query($query);
if ($row = mysql_fetch_assoc($result)) {
$isbn = $row['isbn'];
echo "<tr><td><strong>Isbn:</strong>". $isbn. "</td>";
}
?>
</body>
</html>
main program for my php program
<?php require_once("include/db_connect.php"); ?>
<html>
<head><title>Displaying Image files using PHP</title></head>
<body>
<h1>Bookworms Booksite</h1>
<style> #import "css/style.css";</style>
<div id = "nav">
<ul>
<li><a class = "navbutton" href="index.html">Home</a></li>
<li><a class = "navbutton" href="membership.html">Books</a></li>
<li><a class = "navbutton" href="classes.html">Shopping Cart</a></li>
<li><a class = "navbutton" href="">Checkout</a></li>
<li><a class = "navbutton" href="shop.html" id ="current" >About</a></li>
</ul>
</div>
<p></p>
<p></p>
<div id = "sideNav">
<ul>
<li>Enter Bookstore</li><br>
<li>Bookstore Administartion</li><br>
<li>Search</li>
</ul>
</div>
<?php
$db_link = db_connect("project");
// Retrieve table properties
$fields = mysql_list_fields("project", "bookstore");
$num_columns = mysql_num_fields($fields);
// Make a simple database query to select all columns and rows
$query = "SELECT * FROM bookstore";
$result = mysql_query($query) or die("SQL query failed");
// Display results as an HTML table. Note how mysql_field name
// uses the $fields object to extract the column names
echo '<table border="1" cellpadding = "5" >';
// Display the column names
echo "<tr>";
echo "</tr>";
// Loop over the rows of the table.
// $row contains the information for each row
// Refer to the names of the fields in the table
// Must ahow the path where the image files are held
while ($row = mysql_fetch_assoc($result))
{
echo "<tr>";
$isbn = $row['isbn'];
$title = $row['title'];
echo "<td><a href='bookDetail.php'><img src = 'images/". $row['image']. "'></a></td>";
echo "<a href='bookDetail.php? id= ". $isbn . "'>". $title . "</a><br/>";
echo "<td>". $row['isbn']."</td>";
echo "<td>". $row['title']."</td>";
echo "<td>". $row['author']."</td>";
echo "<td>". $row['pub']."</td>";
echo "<td>". $row['year']."</td>";
echo "<td>". $row['price']."</td>";
echo "</tr>";
}
echo "</table>";
// Free the resources and close the connection
mysql_free_result($result);
mysql_close($db_link);
?>
</body>
</html>
Line 8 is this :
$isbn = $_REQUEST['isbn'];
Therefore, your $_REQUEST['isbn'] variable is undefined. Do a print_r ($_REQUEST); to check your request parameters.
To use $_REQUEST['isbn'], you need the page to be called by a form and have a field with name='isbn', otherwise it won't exist.
More information about forms : http://www.php.net/manual/en/tutorial.forms.php
Actually the line 8 is "$_REQUEST['isbn'];" which has nothing to do with database. Are you sure you're passing the parameter to the page?

Passing the url in a link not working

I'm trying to pass the id of a blogpost in an url. It works in the adminpanel (read blogpost) now I want to do the same on my frontpage, so that it says read more under every blogpost, but when I use the same code it doesn'nt pass the id.
My code that isn't working:
$db_connection = mysql_connect('localhost', 'root', '')or die("cannot connect");
mysql_select_db('database',$conn);
// mysql query
$sql_query="SELECT * FROM blog_posts ORDER BY id DESC";
// Create the ps_pagination object here
$pager = new ps_pagination($db_connection,$sql_query,5,5);
echo $pager->renderFullNav();
//The paginate() function returns a mysql result set
$rs = $pager->paginate();
while($rows = mysql_fetch_assoc($rs)) {
// table to display results here // modify here
echo '<h1>'.$rows["title"].'</p></h1>';
echo '<p> '.$rows["post"].'</p>';
echo '<p class="klein"><span>Door</span>: '.$rows["first_name"].' ';
echo '<p class="klein">Geplaatst op</span>: '.$rows["date_posted"].'</p>';
echo "<td><a class='link' href=\"/admin/bekijk.php?id=$row->id\">Lees meer</a></td>";
echo "<BR>";
}
// close mysql connection here
mysql_close();
My code that is working:
while($row = mysql_fetch_object($sql))
{
echo "<tr>";
echo "<h1><td><a class='link' href=\"bekijk.php?id=$row->id\">$row->title</a></td></h1>";
if($row->id == 0)
{ //home page verberg delete link
}
else
{
// delete functie waarbij een alert word weergegeven of je dit zeker wil
echo "<td><a class='link' href=\"javascript:delpage('$row->id','$row->title');\">Verwijder</a></td>";
echo '<br>';
echo "<td><a class='link' href=\"edit.php?id=$row->id\">Pas aan</a></td>";
echo '<br>';
echo "<td><a class='link' href=\"bekijk.php?id=$row->id\">Bekijk</a></td>";
}
echo "</tr>";
}
They both link to this page:
<?php
session_start();
include '../includes/includes.php';
require('../includes/functions.php');
//make sure user is logged in, function will redirect use if not logged in
login_required();
//if logout has been clicked run the logout function which will destroy any active sessions and redirect to the login page
if(isset($_GET['logout'])){
logout();
}
?>
<html>
<title>Admin</title>
<script src="../ckeditor/ckeditor.js"></script>
<header><link rel="stylesheet" type="text/css" href="../css/style.css"></header>
<body>
<nav>
<ul>
<li>Admin</li>
<li>Bekijk site</li>
<li>Toevoegen</li>
<li>Posts</li>
<li>Uitloggen</li>
</ul>
</nav>
</body>
</html>
<?php
//pak het ID van de pagina die aangepast moet worden
$id = $_GET['id'];
$id = mysql_real_escape_string($id);
$q = mysql_query("SELECT * FROM blog_posts WHERE id='$id'");
$row = mysql_fetch_object($q);
?>
<div id="bekijkwrap">
<form action="" method="post">
<input type="hidden" name="id" value="<?php echo $row->id;?>" />
<h1><?php echo $row->title;?></h1>
<p><?php echo $row->post;?></p>
</form>
</div>
Your broken code loops through mysql_fetch_assoc(), setting your $rows variable as an (associative) array for each iteration.
Your working code calls mysql_fetch_object(), pulling back an object (of class StdClass) for each iteration.
Your broken code attempts to reference the id by treating the $row variable as if it was an object. Not only does this variable not exist (it should be $rows), but the $rows variable is an associative array, not an object.
Therefore, instead of accessing the value using the syntax $foo->id, it must be accessed like $foo['id'] instead.
To fix the problem, change:
echo "<td><a class='link' href=\"/admin/bekijk.php?id=$row->id\">Lees meer</a>
To
echo "<td><a class='link' href=\"/admin/bekijk.php?id=" . $rows['id'] . "\">Lees meer</a>
You can see in the lines above that in your original code that you're referencing $rows and treating it like an array. This is what you must do to the problem line too.

Displying images stored in database

I am doing gallery and I need some help. I have uploaded images to database through website - they are stored in MySQL (names) and in folder called images. What I want is to display (miniatures) them in line and on click enlarge them.
What my code does is displaying miniatures and links them to nothing :/ ...
This is my code:
<?php
$hostname_connect= "localhost";
$database_connect= "gallery";
$username_connect= "root";
$password_connect= "root";
$connect_solning = mysql_connect($hostname_connect, $username_connect, $password_connect) or trigger_error(mysql_error(),E_USER_ERROR);
#mysql_select_db($database_connect) or die (mysql_error());
$query_image = "SELECT * FROM gallery_images";
$result = mysql_query($query_image);
if(mysql_num_rows($result) > 0)
{
while($row = mysql_fetch_array($result))
{
?>
<a href = "javascript:void(0)" onclick = "document.getElementById('light').style.display='block';document.getElementById('fade').style.display='block'">
<?php
echo '<img alt="gallery" src="images/'.$row["image"].'" class="pic-resize" alt=""></a>';
}
while($row = mysql_fetch_array($result))
{
?>
<div id="light" class="white_content">
<?php
echo '<img alt="gallery" src="images/'.$row["image"].'" class="" alt=""></a>';
?>
Close
</div>
<?php
}
}
else
{
echo 'File name not found in database';
}
?>
You are using this in a loop:
<div id="light" class="white_content">
So you will have multiple elements with the same ID and that is not allowed.
Then you try to access them like:
document.getElementById('light')
Which will give you the first element it finds and not the actual one you want to enlarge (unless it is the first...).
Personally I would use the standard lightbox solution, link your thumbnail to your big image (instead of javascript:void(0)) and use the href attribute of your link to set the source for the big image using javascript when the thumbnail gets clicked.
Edit: An example for the html to get you started:
<?php
while($row = mysql_fetch_array($result))
{
?>
<a href="<?php echo 'images/'.$row["image"]; ?>" onclick="return showImage(this);">
<?php
echo '<img alt="gallery" src="images/'.$row["image"].'" class="pic-resize" alt=""></a>';
}
?>
<div id="light"><img src='' alt=''></div>
Now you just have to write the showImage() function in javascript that will do the actual work:
get the href attribute of the clicked link;
set the source of the image in #light to that value;
show the #light element.

Categories