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.
Related
<?php
$servername = "*****";
$username = "****";
$password = "";
$dbname = "****";
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$product_id = intval($_GET['product_id']);
// make sure its only an id (SQL Incjection problems)
$sql = "SELECT * FROM product_gallery WHERE pid=$product_id";
$result = mysqli_query($conn, $sql);
if ($result->num_rows > 0) {
while ($row = mysqli_fetch_array($result)) {
echo"<div class='tg-productbox'>";
echo" <div id='tg-thumblider' class='tg-thumblider tg-productgalleryslider owl-carousel'>";
echo" <figure class='item'><img src='$row[product_image]' alt='image description'></figure>";
echo" </div>";
echo" </div>";
echo" </div>";
};
} else {
echo "0 results";
}
$conn->close();
?>
This is my php code to view multiple images from database.but I can only see one image in front end.please help me..there is a main image and a slider to view to multiple images. It is similar to how we see product images in a shopping site
this is my php code to view multiple images from database.but i can only see one image in front end.please help me..there is a main image and a slider to view to multiple images.its is similar to how we see product images in a shopping site
There are some other mistakes like one extra closing div in loop and semicolon }; which not necessary.
Try something like below if you are getting multiple records it will definitely work.
while ($row = mysqli_fetch_array($result)) {
extract($row);
?>
<div class='tg-productbox'>
<div id='tg-thumblider' class='tg-thumblider tg-productgalleryslider owl-carousel'>
<figure class='item'>
<img src="<?php echo $product_image; ?>" alt="image description">
</figure>
</div>
</div>
<?php
}
Changes
Remove id='tg-thumblider' from echo "<div id='tg-thumblider' class='tg-thumblider. ID must need to be unique. According to the code provided, In while loop, every <div> will have same ID, which is wrong. So, either remove that ID or give some unique ID to each div.
Remove extra echo "</div>";
Remove ; from closing of while loop.
Code:
<?php
if ($result->num_rows > 0) {
while ($row = mysqli_fetch_array($result)) {
echo "<div class='tg-productbox'>";
echo "<div class='tg-thumblider tg-productgalleryslider owl-carousel'>";
echo "<figure class='item'><img src='".$row['product_image']."' alt='image description'></figure>";
echo "</div>";
echo "</div>";
}
}?>
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();
?>
I am working on a search page where I am getting the input from the user and then using PHP, I am connecting to a database, executing a query and trying to display the results on the page. I need to display the result in a div tag where I need various search results to be displayed.
<?php
ob_start();
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="test1"; // Database name
$tbl_name="postjob"; // Table name
// Create connection
$conn = new mysqli($host, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
$dbconn = mysqli_select_db($conn, $db_name) or die(mysql_error());
echo $dbconn;
echo "Connected to Database<br />";
$output = '';
$keywords=$_POST['keywords'];
$location=$_POST['location'];
$keywords = mysqli_real_escape_string($conn, $keywords);
$location = mysqli_real_escape_string($conn, $location);
$sql="SELECT * FROM $tbl_name WHERE job_title LIKE '%$keywords%' OR job_type
LIKE '%$keywords%' OR job_category LIKE '%$keywords%' OR job_tags LIKE
'%$keywords%' OR description LIKE '%$keywords%' OR company_name LIKE
'%$keywords%' OR location LIKE'%$location%'";
$result=mysqli_query($conn, $sql);
$count=mysqli_num_rows($result);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Sign Up</title>
</head>
<body>
<section class="content_area">
<div class="banner">
<div class ="result">
<table>
<tr>
<?php
while($jobsearch=mysqli_fetch_assoc($result)) {
echo "<tr>";
echo "<td>"."<h3>".$jobsearch['job_title']."</h3>"."
<br>".$jobsearch['company_name']."<br>".$jobsearch['description']."</td>";
echo "</tr>";
}
?>
</tr>
</table>
</div>
</div>
</section>
</body>
</html>
I am calling the above PHP file using post action from HTML form page which has Keywords and location textbox. I am fetching job_title, company_name and description from Database and displaying it. Here I am using a table to display. But I wanted use a div where I need to do some styling. I tried to use below code replacing table with div, but it is not displaying any result.
<?php
while($jobsearch=mysqli_fetch_assoc($result)) {
echo "<div>"."<h3>".$jobsearch['job_title']."</h3>"."
<br>".$jobsearch['company_name']."<br>".$jobsearch['description'].
</div>";
}
?>
I am expecting result like below inside a div
job_title
company_name
description
<button>Apply</button>
I tried the below code, but it is not displaying any result.
<?php
while($jobsearch=mysqli_fetch_assoc($result)) {
echo "<div><h3>".$jobsearch['job_title']."</h3></div>
<div>".$jobsearch['company_name']."</div>
<div>".$jobsearch['description']. "</div>";
}
?>
I need to customize this div to do some styling. Could someone help me how to achieve this?
Hope it will work.
<ul>
<?php
while($jobsearch=mysqli_fetch_assoc($result)) {
echo "<li>"."<h3>".$jobsearch['job_title']."</h3>"."</li>"
echo "<li>"."<h3>".$jobsearch['company_name']."</h3>"."</li>"
echo "<li>"."<h3>".$jobsearch['description']."</h3>"."</li>"
echo "<button>Apply</button>";
}
?>
</ul>
your code should throw an error as " is missing in your echo statement
try this
echo "<div><h3>".$jobsearch['job_title']."</h3></div>
<div>".$jobsearch['company_name']."</div>
<div>".$jobsearch['description']. "</div>";
attached is the output, this output is by replacing your variables with some defined values
Try this.
<div class ="result">
<table>
<?php
while($jobsearch=mysqli_fetch_assoc($result)) {
echo "<tr>";
echo "<td>"."<h3>".$jobsearch['job_title']."</h3></td>";
echo "</tr>";
echo "<tr>";
echo "<td>"."<h3>".$jobsearch['company_name']."</h3></td>";
echo "</tr>";
echo "<tr>";
echo "<td>"."<h3>".$jobsearch['description']."</h3></td>";
echo "</tr>";
echo "<br />";
}
?>
</table>
</div>
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?
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>";
}
?>