PHP Custom blog system, Duplicates message for every user - php

so I'm making a blog system on https://aindrigo.com/blog.php?bid=1 and for some reason the post duplicates for every user that exists.. any help?
<?php
$con = mysqli_connect("localhost","root","don't try hacking my db","data");
$bid = $_GET["bid"];
$queryb = $con->query("SELECT bid,title,content FROM blogposts WHERE bid=$bid");
$queryu = $con->query("SELECT id,username,password,avatarurl FROM accounts");
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="description" content="aindev's Website">
<link rel="stylesheet" href="style.css?v=2222">
<link href="https://fonts.googleapis.com/css?family=Inter&display=swap" rel="stylesheet">
<title>Adam Indrigo</title>
</head>
<body>
<div class="start">
<h1>aindev</h1>
</div>
<div class="info">
<div class="inner">
<h1>aindev's Blog</h1>
<?php
if(!$bid){
echo "<h1>Please enter a Blog ID (bid) in the url</h1>";
}
while($row = mysqli_fetch_assoc($queryb)){
while($row2 = mysqli_fetch_assoc($queryu)){
echo "<img src='" . $row2["avatarurl"] . "?v=22222' style='max-width: 60px; max-height: 60px; border-radius: 6px;'>";
echo "<h2>" . $row["title"] . " by " . $row2["username"] . "</h2>";
echo "<p>" . $row["content"] . "</p>";
}
}
?>
</div>
</div>
</body>
</html>
I don't know the problem. I tried adding it so that it only shows it for the 1 user that created it. Even adding a cid tag, but that didn't work..

So apparently I made a mistake when I added the CID tag. I added the cid to the user query and not the blog query.

Related

why PHP search page show blank box with no result?

I'm trying to do a search bar here.
I have done my database, filled it with data, connected it to my php file, did the search bar code everything is fine but when i tried to search for a word its showing me blank box with zero result
This is my db connect code
<?php
$dbServername = "localhost";
$dbUsername = "root";
$dbPassword = "root";
$dbName = "product_list";
$conn = mysqli_connect($dbServername, $dbUsername, $dbPassword, $dbName);
and this is search page code
<?php
include '/Applications/MAMP/htdocs/Pharmacy Project/db.php';
?>
<?php include_once '/Applications/MAMP/htdocs/Pharmacy Project/Styles/TopNav/topnav.html'; ?>
<style>
<?php include '/Applications/MAMP/htdocs/Pharmacy Project/Styles/TopNav/topnav.css';
?>
</style>
<?php ?>
<style>
<?php include '/Applications/MAMP/htdocs/Pharmacy Project/Styles/SearchBar/searchbar.css'; ?>
</style>
<style>
<?php include_once '/Applications/MAMP/htdocs/Pharmacy Project/Styles/Result/result.css' ?>
</style>
<div class="result-container">
<?php
if (isset($_POST['Search'])) {
$search = mysqli_real_escape_string($conn, $_POST['search']);
$sql = "SELECT * FROM product_list WHERE a_Name LIKE '%$search%' OR Pharmacy LIKE '%$search%'";
$result = mysqli_query($conn, $sql);
$queryresult = mysqli_num_rows($result);
echo 'There are' . $queryresult . 'results!';
if ($queryresult > 0) {
while ($row = mysqli_fetch_assoc($result)) {
echo "<div class='result-box'>
<h3>" . $row['a_Name'] . "</h3>
<p>" . $row['Price'] . "</p>
<p>" . $row['Pharmacy'] . "</p>
</div>";
}
} else {
echo "Sorry No Result :(!";
}
}
?>
</div>
this is my form page
<?php
include '/Applications/MAMP/htdocs/Pharmacy Project/db.php';
?>
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" type="text/css" href="/Applications/MAMP/htdocs/Pharmacy Project/Styles/Result/result.css">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Home Screen</title>
</head>
<body>
<?php include_once '/Applications/MAMP/htdocs/Pharmacy Project/Styles/TopNav/topnav.html'; ?>
<style>
<?php include '/Applications/MAMP/htdocs/Pharmacy Project/Styles/TopNav/topnav.css';
?>
</style>
<?php ?>
<style>
<?php include '/Applications/MAMP/htdocs/Pharmacy Project/Styles/SearchBar/searchbar.css'; ?>
</style>
<style>
<?php include_once '/Applications/MAMP/htdocs/Pharmacy Project/Styles/Result/result.css' ?>
</style>
<form action="search.php" method="POST">
<div id="searchbar">
<input type="text" name="search" placeholder="Search">
<button id='sbutton'>Search</button>
</div>
</form>
<h2 class="allresult">All Result</h2>
<div class="result-container">
<?php
$sql = "SELECT * FROM Product";
$result = mysqli_query($conn, $sql);
$queryresults = mysqli_num_rows($result);
if ($queryresults > 0) {
while ($row = mysqli_fetch_assoc($result)) {
echo "<div class='result-box'>
<h3>" . $row['a_Name'] . "</h3>
<p>" . $row['Price'] . "</p>
<p>" . $row['Pharmacy'] . "</p>
</div>";
}
}
?>
</div>
</body>
</html>
I'm new to PHP
Your search field name is search in lowercase but you are checking for Search in $_POST which is capitalized. Make sure they match and it will fix the issue

Is there a way to automatically use another image as a temporary placeholder for missing images sitewide?

I am working on building a site, but right now it has several images that I don't have actual images for yet. As this site has thousands of images or places where images should be, I don't want to have to manually change each of them and then change them again when I find the correct image. Is there a way to create a function that will look for the missing images and replace them with a specified image until the correct image is found?
Update: Since I am still a bit confused as to where to even place this function, I am going to add the code for one of the pages that I need this for then maybe someone can help me figure out how to place it.
Here is the code for one of the pages:
<?php
require_once('dbconnection.php');
mysqli_select_db($conn, $dbname);
$query_master = "SELECT DISTINCT * FROM `master_list` INNER JOIN types_join ON master_list.join_id=types_join.join_id WHERE `type_id` = 171 ORDER BY `item_id`";
$master = mysqli_query($conn, $query_master) or die(mysqli_error());
$row_master = mysqli_fetch_assoc($master);
$totalrows_master = mysqli_num_rows($master);
?>
<!doctype html>
<html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Flower Trees</title>
<link href="/css/styles.css" rel="stylesheet" type="text/css">
<link rel="shortcut icon" type="image/png" href="img/favicon.png">
</head>
<body>
<div class="wrapper">
<a id="top"></a>
<?php require_once('header.php'); ?>
<?php require_once('nav.php'); ?>
<div class="category"><h2>Flower Trees</h2></div>
<div class="display">
<?php do { ?>
<ul>
<li><a href="details.php?recordID=<?php echo $row_master['master_id']; ?>"><img class="thumb" src="img/<?php echo $row_master['img']; ?>"/>
<br />
<span class="name"><?php echo $row_master['name']; ?></span></a></li>
</ul>
<?php } while ($row_master = mysqli_fetch_assoc($master)); ?>
<!-- end .display --></div>
<?php
mysqli_free_result($master);
?>
<?php require_once('footer.php'); ?>
<!-- end .wrapper --></div>
<script>
function myFunction() {
var x = document.getElementById("myTopnav");
if (x.className === "topnav") {
x.className += " responsive";
} else {
x.className = "topnav";
}
}
</script>
</body>
</html>
Since this is as simple as a foreach loop, and not tons of images scattered across your webpage, you can use something like:
$image = file_exists('img/' . $row_master['img']) ? 'img/' . $row_master['img'] : 'placeholder.png';
Full code:
<?php
require_once('dbconnection.php');
mysqli_select_db($conn, $dbname);
$query_master = "SELECT DISTINCT * FROM `master_list` INNER JOIN types_join ON master_list.join_id=types_join.join_id WHERE `type_id` = 171 ORDER BY `item_id`";
$master = mysqli_query($conn, $query_master) or die(mysqli_error());
$row_master = mysqli_fetch_assoc($master);
$totalrows_master = mysqli_num_rows($master);
?>
<!doctype html>
<html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Flower Trees</title>
<link href="/css/styles.css" rel="stylesheet" type="text/css">
<link rel="shortcut icon" type="image/png" href="img/favicon.png">
</head>
<body>
<div class="wrapper">
<a id="top"></a>
<?php require_once('header.php'); ?>
<?php require_once('nav.php'); ?>
<div class="category"><h2>Flower Trees</h2></div>
<div class="display">
<?php do {
$image = file_exists('img/' . $row_master['img']) ? 'img/' . $row_master['img'] : 'placeholder.png';
?>
<ul>
<li><a href="details.php?recordID=<?php echo $row_master['master_id']; ?>"><img class="thumb" src="<?php echo $image; ?>"/>
<br />
<span class="name"><?php echo $row_master['name']; ?></span></a></li>
</ul>
<?php } while ($row_master = mysqli_fetch_assoc($master)); ?>
<!-- end .display --></div>
<?php
mysqli_free_result($master);
?>
<?php require_once('footer.php'); ?>
<!-- end .wrapper --></div>
<script>
function myFunction() {
var x = document.getElementById("myTopnav");
if (x.className === "topnav") {
x.className += " responsive";
} else {
x.className = "topnav";
}
}
</script>
</body>
</html>
I don't want to have to manually change each of them and then change them again when I find the correct image. Is there a way to create a function that will look for the missing images and replace them with a specified image until the correct image is found?
Such a function might be written as:
function im($imgName) {
$pathToImgs = "images/";
if (file_exists( $pathToImgs . $imgName )) {
echo $pathToImgs . $imgName;
}
else {
echo $pathToImgs . "placeholder.jpg";
}
}
Then in your html:
<img src="<?php im("product1.jpg"); ?>">
<img src="<?php im("product2.jpg"); ?>">
<img src="<?php im("product3.jpg"); ?>">
As a start.
***Edit 1:
Given your code where it says:
<img class="thumb" src="img/<?php echo $row_master['img']; ?>"/>
You might modify it with a conditional that inserts the placeholder image in the event that the target image simply doesn't exist, yet.
<img class="thumb" src="<?php
if (file_exists("img/" . $row_master['img'])) {
echo "img/" . $row_master['img'];
}
else {
echo 'img/placeholder.jpg';
}
?>">
You could reuse this functionality by turning the conditional into a php function, so described as a starter above.
Using jQuery, you can accomplish something easy enough using a global $('img') handler when errors occur. Simply swap them out with your placeholder image afterwards.
$('img').on('error', function() {
const oldSrc = encodeURIComponent($(this).attr('src'));
$(this).attr('src', `https://via.placeholder.com/300/000000/FFFFFF/?text=${oldSrc}`);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<img src="imagethatdoesntexist.jpg"><br />
<img src="anotherimage.jpg">
Here I use placeholder.com (A convenient site for placeholder images such as this), and inject the old image source into the query string, which the site will render in the image itself.

PHP Basic Random Quote Generator. How do I prevent quotes that have appeared recently?

EDIT: Thank you for correcting the formatting... did not notice that mistake
I have completed this basic quote generator as a first project, but have noticed that it frequently displays the same quote upon refresh. Granted, there are only five at the moment, but this problem might still be apparent as I fill the database. I am currently learning JQuery AJAX for a more advanced one - but am only starting. To my knowledge, this is not a duplicate question. Displays database query upon reload. Thank you!
Here is the code:
connect.php:
<?php
$connection = new mysqli('localhost', 'root', '', 'random_quotes');
if ($connection->connect_error) {
die('Connect Error (' . $mysqli->connect_errno . ')'
. $mysqli->connect_error);
}
?>
functions.php:
<?php
require ('connect.php');
$query = "SELECT id, quote, author FROM quotes ORDER BY RAND() LIMIT 1";
$getQuote = $connection->query($query);
?>
HTML:
<!doctype html>
<html lang="en">
<?php
require ('includes/connect.php');
require ('includes/functions.php');
?>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
<link href='https://fonts.googleapis.com/css?family=Candal' rel='stylesheet' type='text/css'>
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
<div class="container-fluid text-center">
<h1>Random Quote Generator</h1>
<p>Some of my favourite all around quotes!</p>
<br/>
<button class="btn btn-default" onclick="newQuote()" type="submit">New Quote
</button>
<div class="quote_wrap text-center">
<span class="quote">
<?php
while($row = $getQuote->fetch_assoc()){
$stringID = $row['id'];
$stringQuote = $row['quote'];
$stringAuthor = $row['author'];
echo $stringQuote;
}
?>
</span>
</br>
<div class="author text-center"><?php echo $stringAuthor?></div>
<div class="quoteid" id="<?php echo $stringID?>"></div>
</div>
</div>
<script src="https: /ajax.googleapis.com/ajax/libs/jquery/2.2.3/jquery.min.js"></script>
<script type="text/javascript">
function newQuote() {
location.reload();}
</script>
</body>
</html>
Generate a random number and then access quote by that id. May work better..
$query = "SELECT COUNT(*) as noQuotes FROM quotes ";
$result = $connection->query($query);
$row = $result->fetch_assoc();
$noQuotes = $row["noQuotes"];
srand(time());
//echo(rand(1,$noQuotes));
$randNo = rand(1,$noQuotes);
$query = "SELECT id, quote, author FROM quotes WHERE id = $randNo ";
$getQuote = $connection->query($query);

Dynamically generated "submit" buttons in PHP

First time posting here (so please be gentle, as I am a relative PHP newbie).
I am building an intranet for our company and one of the things I need to do is to create a form that lists all outstanding sales orders (pulled from our accounting database) and provides a "submit" button beside each one to create the relevant work order.
Here is the code:
<div class="report_column">
<div class="report_header">
<div class="report_column_title" style="width:150px;margin-left:5px">ship date</div>
<div class="report_column_title" style="width:200px">customer</div>
<div class="report_column_title" style="width:140px;text-align:right">item</div>
<div class="report_column_title" style="width:120px;text-align:right">quantity</div>
</div>
<?php
// Open connection
include 'includes/dbconnect.php';
// Perform query
$result = mysqli_query($con,"SELECT * FROM tSalOrdr ORDER BY dtShipDate ASC");
// Retrieve results
while($row = mysqli_fetch_array($result)) {
$order = $row['lId'];
if ($row['bCleared'] == 0) {
$shipdate = substr($row['dtShipDate'], 0,10);
$customer = $row['sName'];
$po = $row['sComment'];
echo '<div class="report_item" style="width:750px";>';
echo '<form class="form" action="index.php?page=form&item=create_work_order" method="POST">';
echo '<div class="report_item_date" style="width:120px">'.$shipdate.'</div>';
echo '<div class="report_item_name" style="width:530px">'.$customer;
echo '<input type="hidden" name="po" value="'.$po.'" />';
echo '<input type="submit" class="submit" style="height: 25px;width:100px;margin:0px;padding:0px;margin:0px" value="work order"/>';
echo '</div>';
$result2 = mysqli_query($con,"SELECT * FROM tSOLine WHERE lSOId=$order ORDER BY sDesc ASC");
while($row = mysqli_fetch_array($result2)) {
if ($row['dRemaining'] <> 0) {
echo '<div class="report_item_details">';
echo '<div class="report_item_item">'.$row['sDesc'].'</div>';
echo '<div class="report_item_quantity">'.$row['dRemaining'].'</div>';
echo '</div>';
}
}
echo '</form>';
echo '</div>';
}
}
// Close connection
mysqli_close($con);
?>
</div>
What happens when I do this is that the first "submit" button will, for some reason, send me back to "index.php". The other buttons will load the correct page, however, they do not POST the required value.
Is there something I am doing wrong or is this something that needs different methodology than what I am currently using? My research on this seems to indicate that perhaps I should use javascript or an array to deal with this, but, having never dealt with either, I am not sure how to proceed. Any pointers would be appreciated.
Thanks.
#maniteja: The index.php is as follows:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="language" content="en" />
<title></title>
<link href="css/main.css" rel="stylesheet" type="text/css" />
<link href="css/forms.css" rel="stylesheet" type="text/css" />
<link rel="icon" type="image/ico" href="favicon.ico" />
<script type="text/javascript" src="scripts/jquery-2.1.0.min.js" ></script>
<script type="text/javascript" src="scripts/tabcontent.js" ></script>
</head>
<body>
<!-- BEGIN MAIN CONTENT -->
<div id="wrapper">
<!-- BEGIN HEADER -->
<div id="header">
<img src="images/logo.jpg">
</div>
<!-- END HEADER -->
<!-- BEGIN MAIN MENU -->
<div id="leftcolumn">
<?php include 'includes/menu.php'; ?>
</div>
<!-- END MAIN MENU -->
<!-- BEGIN CONTENT FRAME -->
<div id="rightcolumn">
<div id="content_area">
<?php
if (isset($_GET['page']))
{$page = $_GET['page'];
include('pages/' . $page . '.php');}
else {include('pages/home.php');}
?>
</div>
</div>
<!-- END MAIN CONTENT -->
</body>
</html>
I've made seventeen other forms with it, so I don't think that it is the problem. I'm hoping that this is just a typo or a logic error on my part.
u can use
<button type='submit' class='' name='' onclick=''>Submit</button>
Try to do onclick. See my example below.
input type="button" name="submit" onclick='location.href="index.php?id=$id"'
AS far as I understand, you are making your forms go to index.php:
echo '<form class="form" action="index.php?page=form&item=create_work_order" method="POST">';
Can you explain what is your expected behavior when a button is pushed?

how to add links to a list of whats trending from the Foursquare API using PHP

Hi so I have created a list of the trending places in a given area using the Foursquare API: see the following link http://createmate.co/foursquare-whatshot/
I am trying to figure out how to make each item link to its respective Foursquare URL. Here is the code so far...
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Foursquare What's Hot</title>
<!-- external CSS link -->
<link rel="stylesheet" href="css/normalize.css">
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<div id="container">
<h1>Foursquare What's Hot</h1>
<?php
/* GET THE DATA */
$trending_url = file_get_contents("https://api.foursquare.com/v2/venues/trending?ll=40.7,-74&oauth_token=BQEPEMYIFHHH2C1OGBTJDI4GBYV5HQAPNFLR5ON1JIAI42GN&v=20130220");
$trending_output = json_decode($trending_url);
/* TEST THE DATA */
//echo "<pre>";
//print_r($trending_output);
//echo "</pre>";
/* PRINT RESULTS */
for ($i=0;$i<10;$i++) {
echo "<ul id ='locations'>";
echo "<li> <a href= " . $trending_output->response->venues[$i]->canonicalURL . "> <h3>" . $trending_output->response->venues[$i]->name . "<br>";
echo "<li> <h4>" . $trending_output->response->venues[$i]->location->address . "<br>";
echo "</ul>";
}
?>
</div>
</body>
Any help is much appreciated.
Adam
The property you retrieve should be canonicalUrl, not canonicalURL.
You should also surround your href attribute with quotes and don't forget to close your <a> tag as well.

Categories