PHP & MySQL - Update Query not updating database - php

I'm making a personal website and I just wanted it to be a bit easier for me to add/edit my posts without manually going into phpmyadmin.
When I go to the edit_post.php page and press update I get an "=" sign next to the pid eg.(foo.php?pid=3), and if i let it redirect to the blog page it doesn't update it.
Blog page
<?php
session_start();
include_once("../IncBlog/db.php");
?>
<?php include "../Includes/navHead.php"; ?>
<title>Adam Brickhill - Life Journal </title>
</head>
<body>
<div class="box">
<div class="header">
<nav class="nav"><p class="title"><a class="postLink" href="../IncBlog/post.php">- Lone Tree -</a></p></nav>
</div>
<!-- JOURNAL !-->
<?php
require_once("../nbbc/nbbc.php");
$bbcode = new BBCode;
$sql = "SELECT * FROM posts ORDER BY id DESC";
$res = mysqli_query($db, $sql) or die(mysqli_error());
$posts = "";
if (mysqli_num_rows($res) > 0) {
while ($row = mysqli_fetch_assoc($res)) {
$id = $row['id'];
$title = $row['title'];
$img = $row['img'];
$content = $row['content'];
$date = $row['date'];
$admin = "<div><a href='../IncBlog/del_post.php?pid$id'>Delete</a> <div><a href='../IncBlog/edit_post.php?pid$id'>Edit</a>";
$output = $bbcode->Parse($content);
ob_start();
include('../IncBlog/blogSkel.php');
$posts .= ob_get_contents();
ob_end_clean();
}
echo $posts;
}
else {
echo "There are no posts to display";
}
?>
<div class="journal">
<div class="catagories">
</div>
<!-- Date !-->
<!-- Picture !-->
<!-- Description !-->
</div>
<?php include "../Includes/navFooter.php"; ?>
<!-- SCRIPTS !-->
<?php include "../Includes/navScriptImport.php"; ?>
</div>
</body>
</html>
Edit Page
<?php
session_start();
include_once("db.php");
if (!isset($_SESSION['username'])) {
header("Location: login.php");
return;
}
$pid = $_SERVER['REQUEST_URI'];
$pid = trim($pid, "/IncBlog/edit_post.php?pid");
$pid = strip_tags($pid);
$pid = stripslashes($pid);
$pid = mysqli_real_escape_string($db, $pid);
//echo "$pid";
if ($pid == "") {
header("Location: ../Nav/life.php");
}
if (isset($_POST['update'])) {
$title = strip_tags($_POST['title']);
$content = strip_tags($_POST['content']);
$img = strip_tags($_POST['image']);
$title = mysqli_real_escape_string($db, $title);
$content = mysqli_real_escape_string($db, $content);
$img = mysqli_real_escape_string($db, $img);
$date = date("l jS \of F Y h:i:s A");
$sql = "UPDATE posts SET title='$title', content='$content', img='$img', date='$date' WHERE id=$pid";
if ($title == "" || $content == "") {
echo "The database is hungry you can't feed it nothing!";
return;
}
mysqli_query($db, $sql);
header("Location: ../Nav/life.php");
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Blog - Post</title>
</head>
<body>
<?php
$sql_get = "SELECT * FROM posts WHERE id=$pid LIMIT 1";
$res = mysqli_query($db, $sql_get);
if (mysqli_num_rows($res) > 0) {
while ($row = mysqli_fetch_assoc($res)) {
$title = $row['title'];
$content = $row['content'];
$img = $row['image'];
echo "<form action='edit_post.php?pid=$pid' method='post' enctype='multipart/form-data'>";
echo " <input placeholder='Title' type='text' name='title' value='$title' autofocus size='48'><br /><br />";
echo " <input placeholder='Image' type='text' name='image' value='$img' autofocus size='48'><br /><br />";
echo " <textarea placeholder='Content' name='content' rows='40' cols='40'>$content</textarea><br />";
}
}
?>
<input type="submit" name="update" value="Update">
</form>
</body>
</html>

You have to set the columns and set the values for the columns.
UPDATE posts SET ( title, content, img, date ) VALUES ($title, $content, $img, $date) WHERE id = $pid;

$date = date("l jS \of F Y h:i:s A");
use instead of above line
$date = date('Y-m-d H:i:s', strtotime());
$sql = 'UPDATE posts SET title='".$title."', content='".$content."', img='".$img."', date='".$date."' WHERE id='".$pid."'";

Ok so I figured it out, very small programmers blindness but hey we all get it.
Line 66 echo "<form action='edit_post.php?pid$pid' method='post' enctype='multipart/form-data'>";
Where it says "?pid$pid" I just needed to remove the "=" that i had there.

Related

How do I show just the first image from mysql database in php

I have the following code. How do I show the first image in the database with index of 0 for the large image display at end of the code? Right now it is showing the last image in the database.
<div id="imgWheel" class="treatmentContainer">
<?php
$query = "SELECT * FROM images WHERE user = 0 ORDER BY id;";
$result = $mysqli->query($query);
while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
$product = $row["product"];
$room = $row["room"];
$style = $row["style"];
$tags = $row["tags"];
$src = $row["url"];
$dataid = $row["id"];
$imgClass = "";
if (in_array($src, $favourites)) {
$imgClass = " favourite";
}
echo "<div class='treatment$imgClass' data-url='$src' data-product='$product' data-room='$room' data-style='$style' data-tags='$tags' data-number='$dataid' id='pic_$dataid' >";
echo "<img src='$src' crossorigin='anonymous'/>";
echo "</div>";
}
?>
</div> <!-- close imgWheel -->
<!-------- Large Image Display------- -->
<div id="display">
<img id="mainImage" src="<?php echo $src ?>" />
</div>
Your result set is alreadyx ordered by id, so you need only a variable, to be filled once with the first imageurl
<div id="imgWheel" class="treatmentContainer">
<?php
$bigpictureurl = "";
$query = "SELECT * FROM images WHERE user = 0 ORDER BY id;";
$result = $mysqli->query($query);
while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
$product = $row["product"];
$room = $row["room"];
$style = $row["style"];
$tags = $row["tags"];
$src = $row["url"];
$dataid = $row["id"];
if (empty($bigpictureurl)) {
$bigpictureurl = $src ;
}
$imgClass = "";
if (in_array($src, $favourites)) {
$imgClass = " favourite";
}
echo "<div class='treatment$imgClass' data-url='$src' data-product='$product' data-room='$room' data-style='$style' data-tags='$tags' data-number='$dataid' id='pic_$dataid' >";
echo "<img src='$src' crossorigin='anonymous'/>";
echo "</div>";
}
?>
</div> <!-- close imgWheel -->
<!-------- Large Image Display------- -->
<div id="display">
<img id="mainImage" src="<?php echo $bigpictureurl ?>" />
</div>
You just need to update your SQL query, just add LIMIT 1. This will limit the result just to 1 record and as you have ORDER id ASC, it will show the first record of the given user (as per you it is 0).
$query = "SELECT * FROM images WHERE user = 0 ORDER BY id ASC LIMIT 1;";
A quick and dirty solution is to save your first image in some separate variables, for example like this:
$isFirst = true;
$firstImageSrc = "";
$result = ....;
while (...) {
// set your $product, $room etc here
if ($isFirst) {
$isFirst = false;
$firstImageSrc = $src;
}
}
echo ...
?>
</div> <!-- close imgWheel -->
<!-------- Large Image Display------- -->
<div id="display">
<img id="mainImage" src="<?php echo $firstImageSrc ?>" />
</div>
A much more elegant solution would be to create an array with all your images, so that you can separate your php from your html. I will refactor your code below, and fix your first image problem as well:
<?php
$images = [];
$idx = 0;
$query = "SELECT * FROM images WHERE user = 0 ORDER BY id;";
$result = $mysqli->query($query);
while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
$images[$idx]["product"] = $row["product"];
$images[$idx]["room"] = $row["room"];
$images[$idx]["style"] = $row["style"];
$images[$idx]["tags"] = $row["tags"];
$images[$idx]["src"] = $row["url"];
$images[$idx]["dataid"] = $row["id"];
$images[$idx]["imgClass"] = "";
if (in_array($src, $favourites)) {
$images[$idx]["imgClass"] = " favourite";
}
$idx++;
}
?>
<div id="imgWheel" class="treatmentContainer">
<?php foreach ($images as $image) { ?>
<div class='treatment<?=$image["imgClass"]?>' data-url='<?=$image["src"]?>' data-product='<?=$image["product"]?>' data-room='<?=$image["room"]?>' data-style='<?=$image["style"]?>' data-tags='<?=$image["tags"]?>' data-number='<?=$image["dataid"]?>' id='pic_<?=$image["dataid"]?>' >
<img src='<?=$image["src"]?>' crossorigin='anonymous'/>
</div>
<?php } ?>
</div> <!-- close imgWheel -->
<!-------- Large Image Display------- -->
<div id="display">
<img id="mainImage" src="<?=$images[0]["src"]?>" />
</div>
Since you have all of that in your WHILE statement, I assume you want to echo all those records. And then at the end show the 1st pic. So for the "Large Image Display," give this a try:
<div id="display">
$query = "SELECT * FROM images WHERE user = 0;";
$result = $mysqli->query($query);
$row = $result->fetch_array(MYSQLI_ASSOC)
$src = $row["url"];
<img id="mainImage" src="<?php echo $src ?>" />
</div>
If you'd like less code, then save the value of $src inside your WHILE loop when user=0 into some other variable like $src2. And then your code simply becomes:
<img id="mainImage" src="<?php echo $src2 ?>" />

Display Related Items PHP/MySQL

I have a page that displays the display of an item with a specific id and I want to display other items that are related based on title, subject, or author. I am having issues with displaying related items. Where I put my query together and execute it, the related results are just the same title of the item whose page I am already on (https://brawlins.com/soarOpen/itemRecord.php?id=65437). How do I displays related results that are not the same as the item whose page I currently am on?
<?php
include("config.php");
if(!isset($_GET["id"])) {
echo "No id passed into the page";
exit();
}
$id = $_GET['id'];
?>
<!DOCTYPE html>
<html lang="en">
<head>
<?php
$item_query = $conn->prepare("SELECT * FROM oer_search WHERE id = :id");
$item_query->bindParam(":id", $id);
$item_query->execute();
while($row = $item_query->fetch(PDO::FETCH_ASSOC)){
$type = $row['type'];
$link = $row['link'];
$title = $row['title'];
$description = $row['description'];
$subject = $row['subject'];
$pub_date = $row["publication_date"];
$source = $row['source'];
$isbn = $row["isbn_number"];
$e_isbn = $row["e_isbn_number"];
$license = $row['license'];
$license_url = $row['license_url'];
$base_url = $row['base_url'];
$author = $row['author'];
$review = $row['review'];
$image = $row["image_url"];
$loc_collection = $row["loc_collection"];
$publisher = $row['publisher'];
$pub_url = $row['publisher_url'];
?>
<title><?php echo $title ?></title>
<div class="container content-container">
<div class="card card-content">
<div class="card-body">
<?php
echo "<strong><a class='itemRecordLink' rel='external' href='$link'><h3>$title</h3></a></strong><br/>";
}
$like_query = $conn->prepare("SELECT title FROM oer_search WHERE title LIKE :title OR subject=:subject OR author=:author LIMIT 4");
$titleLike = $searchTerm = "%". $title . "%";
$like_query->bindParam(":title", $titleLike);
$like_query->bindParam(":subject", $subject);
$like_query->bindParam(":author", $author);
$like_query->execute();
echo "<div class='row'>";
while($row = $like_query->fetch(PDO::FETCH_ASSOC)){
echo "<div class='col-md-3 text-center'>";
if($image != "") {
echo "<img src='$image' class='img-fluid img-thumbnail itemRecordImage' alt='cover image' /><br>";
}
else {
echo "<img src='images/cover-image.png' class='img-fluid itemRecordImage' alt='cover image' /><br>";
}
echo "<span>$title</span>";
echo "</div>";
}
echo "</div>";
?>
</div><!--end of card body-->
</div><!--end of card-->
</div><!--end of container-->
<?php include 'footer.php'?>
</body>
</html>

How to detect link in text by php and send default meta information

How to detect link in some text which is included from chat database and detect default meta information and put it to text like on discord.
ATTACHMENT
CODE
<?
session_start();
include('../../php/connect.php');
if(isset($_GET['uid']) && isset($_GET['cid']) && isset($_SESSION['user'])) {
$uid = $_GET['uid'];
$user = $_SESSION['user'];
$check = mysqli_query($con, "SELECT * FROM users WHERE id = '$uid'");
$ch = mysqli_fetch_array($check);
if($ch['username'] == $user) {
$cid = $_GET['cid'];
$uq = mysqli_query($con, "SELECT * FROM users WHERE id = '$cid'");
$u = mysqli_fetch_array($uq);
$asd = $u['username'];
$photo = $ch['photo'];
$query = mysqli_query($con, "SELECT * FROM users WHERE username = '$asd'");
$q = mysqli_fetch_array($query);
$row = mysqli_query($con, "SELECT * FROM messages WHERE (user1,user2,type) = ('$user','$asd','message') OR (user2,user1,type) = ('$user','$asd','message') ORDER BY id ASC");
while($result = mysqli_fetch_object($row)) { $date = date_create($result->sent); $time = date_format($date, 'H:i'); $date = date_format($date, 'd.m.Y');
?>
<div class="message" id="message">
<div class="hr-text">
<span>
<? echo $date; ?>
</span>
</div>
<div id="avatar" class="avatar-u"><img src="<? if($result->user1 == $asd) { echo '../../img/avatars/'.$q['photo']; } elseif($result->user1 == $user) { echo '../../img/avatars/'.$photo; } ?>">
</div>
<span class="username-u">
<? echo $result->user1; ?>
</span>
<span class="time">
<? echo $time; ?>
</span>
<div class="message-content">
<? echo '<xmp>'.$result->content.'</xmp>'; ?>
</div>
</div>
<?
}
} else {
header('location: ../index.php');
}
} else {
header('location: ../index.php');
}
?>
AJAX imports this site to #content-frame every 750ms.
I just want for example paste detected link to <a> element and then write some meta information like in ATTACHMENT upper

Filter system not working

Hello so i made my filter system its a simple one only location and price range are set for now everything looks like this :
so the problem i have is that min and max price range filter doesn't work location filter are working as it should be the only problem i face is min and max price got no error or warning nothing but eater nothing happens to.
php code above :
$cat1 = '';
if(isset($_GET["catid"])){
$p1 = '';
$p2 = '';
$catid = $_GET["catid"];
$l1 = substr($catid,0,1);
$l2 = substr($catid,1,1);
$p1 = "CAT".$l1;
if(!empty($l2)){
$p2 = "CAT".$l1."-".$l2;
$p3 = $p2;
}
$cat1 = #$lang[$p1];
$cat2 = #$lang[$p2];
}
$postid = '';
$userid = '';
$pricemin = '';
$pricemax = '';
$location = '';
if(isset($_POST["filter"])){
$pricemin = $_POST["min"];
$pricemax = $_POST["max"];
$location = $_POST["location"];
}
main page code :
<div class="fp">
<div class="filter">
<b style="padding-left: 10px;">Filters:</b>
<form class="filterform" action="" method="post"><br>
Location: <br>
<input name="location" ><br>
Price Range:<br>
Min:<input type="text" name="min" size="5"> Max:<input type="text" name="max" size="5"><br><br>
<input class="submit-button" type="submit" name="filter" value="Filter">
</form>
</div>
<div class="posts">
<div id="adcat"><?php
if(!empty($cat2)){
?>
<a href="cat.php?catid=<?php echo $l1; ?>" ><?php echo $cat1." ยป "; ?></a><span><?php echo $cat2; ?></span>
<?php
} else {
echo "<font color='grey'>".$cat1."</font>";
}
?>
</div><br><br>
<div id="detailformscat">
<?php
if(empty($p1) && empty($p2)){
$sql = "SELECT * FROM posts p JOIN images i ON p.id = i.postid ";
if(!empty($location)){
$sql .= "AND location='$location'";
}
if(!empty($min)){
$sql.= "AND price>='$min' ";
}
if(!empty($max)){
$sql.= "AND price<='$max' ";
}
} else if(!empty($p2)){
$sql = "SELECT * FROM posts p JOIN images i ON p.id = i.postid WHERE catid='$p2' ";
if(!empty($location)){
$sql .= "AND location='$location'";
}
if(!empty($min)){
$sql.= "AND price>='$min' ";
}
if(!empty($max)){
$sql.= "AND price<='$max' ";
}
} else {
$sql = "SELECT * FROM posts p JOIN images i ON p.id = i.postid WHERE p.catid LIKE '$p1%' ";
if(!empty($location)){
$sql .= "AND location='$location'";
}
if(!empty($min)){
$sql.= "AND price>='$min' ";
}
if(!empty($max)){
$sql.= "AND price<='$max' ";
}
}
$res = mysqli_query($connect,$sql);
while ($row = mysqli_fetch_assoc($res)) {
$postid = $row["postid"];
?>
<div id="ads">
<div id="adfavcat">
<?php if(!isset($_SESSION["userid"])) { ?>
<?php } else {
$userid = $_SESSION["userid"];
$sql2 = "SELECT * FROM fav WHERE userid='$userid' AND postid='$postid' ";
$res2 = mysqli_query($connect,$sql2);
$rowcount = mysqli_num_rows($res2);
if ($rowcount > 0){ ?>
<?php
} else { ?>
<?php }
} ?>
</div>
<div id="titlepic">
<?php echo $row["title"]; ?><br>
<img src="<?php if(!empty($row["path1"])) { echo $row["path1"]; } else echo "image/noimage.png"; ?>" height="100px" width="150px">
</div>
<div id="datescat">
<b>Date Added:</b> <?php echo date('m/d/Y H:i', $row["dateadded"]); ?><br>
<b>Renew Date:</b> <?php if($row["renewdate"] > 0){ echo date('m/d/Y H:i', $row["renewdate"]); } ?><br>
<b>Location:</b> <?php echo $row["location"]; ?><br>
<b>Price:</b> <?php echo $row["price"]."&pound"; ?><br>
</div>
</div>
<hr width="100%">
<?php
}
?>
</div>
</div>
</div>
I think it's because you are treating the price as a string, in the sql query you wrote
$sql.= "AND price>='$min' ";
Try with cast/sanitize/filter input variables $min & $max to integers and removing the quotes.
--- by the way, I personally would also change some things:
use atom instead of brackets
use an ORM and remove the query from the html page (the view)
if 2 is not possible, try to move all php logic to the php file instead of the html part
remove all that IFs and try to write a code without lots of repetitions
You are also joining the tables instead of filtering, try changing
$sql = "SELECT * FROM posts p JOIN images i ON p.id = i.postid ";
with
$sql = "SELECT * FROM posts p JOIN images i ON p.id = i.postid WHERE p.id > 0 ";
change
$pricemin = '';
$pricemax = '';
with
$min = '';
$max = '';

Next and previous links and ID displayed in the URL for sharing

I have a database with a few images already set, I would like to have the url display an ID from each query as the user hits next. The user should be able to share the URL and paste it into their browser, the url should pull that unique ID from the query. The issue i am having is every time i paste a url, I get a random image and not the image that is in the ID. I'm at a loss here and im not sure what to do :( here's the code I have so far.
<?php
if (isset($_GET['id'])) {
include("PHP/db.php");
echo $where = $_GET["id"];
echo $query = "SELECT * FROM images WHERE ID =" . $where;
$result = mysqli_query($dbc, $query);
$row = mysqli_fetch_array($result);
$ID = $row['ID'];
$title = $row['name'];
$image = "<img height=500 width=600 src=http://www.goupics.com/img/" . $row['name'] . " >";
}
if($_GET['next4']) {
echo 'HELLO THIS IS THE NEXT IF METHOD';
include("PHP/db.php");
$query = "SELECT * FROM images ORDER BY RAND() LIMIT 1";
$result = mysqli_query($dbc, $query);
$row = mysqli_fetch_array($result);
$ID = $row['ID'];
$title = $row['name'];
$image = "<img height=500 width=600 src=http://www.goupics.com/img/" . $row['name'] . " >";
}
?>
<body>
</div>
<div id="title"> <?php echo $title ?> </div>
<div id="mainpic">
<?php echo $image ?>
</div>
<div id="prevnext">
<div id="next">
<a href="?id=<?php echo $ID ?>" name="name4" >Next</a>
</div>
<div id="prev">
Previous
</div>
</div>
Try this:
<?php
include("PHP/db.php");
$query2 = "SELECT * FROM images ORDER BY RAND() LIMIT 1";
$result2 = mysqli_query($dbc, $query2);
$rand_row = mysqli_fetch_array($result2);
$rand_id = $rand_row ['ID'];
if (!isset($_GET['id'])) {
$_GET['id'] = $rand_id;
}
if (isset($_GET['id'])) {
echo $where = $_GET["id"];
echo $query = "SELECT * FROM images WHERE ID =" . $where;
$result = mysqli_query($dbc, $query);
$row = mysqli_fetch_array($result);
$ID = $row['ID'];
$title = $row['name'];
$image = "<img height=500 width=600 src=http://www.goupics.com/img/" . $row['name'] . " >";
}
?>
<body>
</div>
<div id="title"> <?php echo $title ?> </div>
<div id="mainpic">
<?php echo $image ?>
</div>
<div id="prevnext">
<div id="next">
<a href="?id=<?php echo $rand_id; ?>" name="name4" >Next</a>
</div>
<div id="prev">
Previous
</div>
</div>
I changed:
-link to next is now id=rand
-changed your code to give me a "rand ID" and its already defined on the href of the page you load
It will go inside condition (next == true).
Make sure that your variables are initialized before use.

Categories