no errors ....no results i tried - php

i am not getting any result ..tried a lot ...but didnt know what is the problem.
no results after editing...nothing changed ...and is also not showing any error..please help..i need to submit a project.'
Thankyou.
<form action="acat.php" method="post"> <!--Edit cat form-->
<div class ="form-group">
<label for ="title">Edit Category</label>
<?php
if(isset($_GET['edit'])) {
$editid = $_GET['edit'];
if (!$editid) {
echo "CANNOT BE EDITED";
} else {
$query = "SELECT * FROM categories WHERE cat_id = '$editid'";
$edit_query = mysqli_query($con,$query);
}
while($row=mysqli_fetch_assoc($edit_query)) {
$cat_id = $row['cat_id'];
$cat_title = $row['cat_title'];
?>
<input value="<?php if(isset($editid)) {echo $cat_title;} ?>"
class="form-control" type="text" name="update">
<?php } } ?>
<?php
if(isset($_POST['update_submit']) && isset($_GET['edit'])) {
$editid = $_GET['edit'];
$updatetitle = $_POST['update'];
$query = "UPDATE categories SET cat_title='$updatetitle' where
cat_id='$editid'";
$update_query = mysqli_query($con,$query);
if (!$update_query) {
die('QUERY FAILED' . mysqli_error($con));
}
}
?>
</div>
<div class="form-group">
<input class="btn btn-primary" type="submit"
name="update_submit" value="Update Category">
</div>
</form>
</div>

In the POST condition, it's unable to identify $editid
Change this line:
if(isset($_POST['update_submit'])) {
To this:
if(isset($_POST['update_submit']) && isset($_GET['edit'])) {
$editid = $_GET['edit']; // Add this line
$updatetitle = $_POST['update'];
$query = "SELECT * FROM categories SET cat_title='$updatetitle' WHERE
cat_id = '$editid'"; //error line
$update_query = mysqli_query($con,$query);

Related

How to show comments on specific posts

I have an application that where users can post announcements and comment on posts. My problem is that whenever a comment is posted, It shows up on every announcement post. How can I post comments so that they show up on that specific post?
I have 2 database tables: "announcement: id, name, announcementTitle, announcement, image" and "comment: id, post_id, name, comment" with foreign key attached to comment.
Here is my home.php where the announcements and comments are echoed
<div class="container">
<div class="mx-auto">
<?php
if (isset($_SESSION['username'])) {
echo'
<h1 style="text-decoration:underline">Post an announcement</h1>
<form method="post" action="announcement.php" enctype="multipart/form-data">
<input type="text" name="announcementTitle" placeholder="Enter Subject"><br>
<textarea name="announcementBox" rows="5" cols="40" placeholder="Enter Announcement"></textarea><br>
<input type="file" name="image" accept="image/jpeg">
<button name="announcement">Submit</button>
</form>';
}
$query = "SELECT * FROM announcement ORDER BY id DESC";
$result = mysqli_query($con,$query);
while ($row = mysqli_fetch_array($result)) {
echo '<div class="row" style="color:black;background-color:white;border-radius:5px;padding:10px;margin-top:10px;margin-bottom:70px">';
echo '<div class="column" style="width:100%;border:5px">';
if (isset($_SESSION['username'])) {
echo '<form method="post" action="announcement.php">';
echo "Posted by " .$row["name"]. " click X to delete:";
echo '<input type="hidden" name="postID" value="'.$row['id'].'">';
echo '<button name="delete" style="float:right">X</button>';
echo '</form>';
}
echo $row['announcementTitle'].'<br>';
echo $row['announcement'].'<br>';
echo '<img width="20%" src="data:image;base64,'.$row['image'].'"alt="Image" style="padding-top:10px">';
echo'
<form method="post" action="comment.php">
<textarea name="commentbox" rows="2" cols="50" placeholder="Leave a Comment"></textarea><br>
<button name="comment">Submit</button>
</form>';
echo "Comments:<p><p>";
echo " <p>";
$find_comment = "SELECT * FROM comment ORDER BY id DESC";
$res = mysqli_query($con,$find_comment);
while ($row = mysqli_fetch_array($res)) {
echo '<input type="hidden" name="postID" value="'.$row['post_id'].'">';
$comment_name = $row['name'];
$comment = $row['comment'];
echo "$comment_name: $comment<p>";
}
if(isset($_GET['error'])) {
echo "<p>100 Character Limit";
}
echo '</div></div>';
}
?>
</div>
</div>
Here is comment.php where comments are put in the database
<?php
session_start();
$con = mysqli_connect('localhost', 'root', 'Arv5n321');
mysqli_select_db($con, 'userregistration');
$namee = '';
$comment = '';
$comment_length = strlen($comment);
if($comment_length > 100) {
header("location: home.php?error=1");
}else {
$que = "SELECT * FROM announcement";
$res = mysqli_query($con,$que);
while ($row = mysqli_fetch_array($res)) {
$post_id = $row['id'];
}
$namee = $_SESSION['username'];
$comment = $_POST['commentbox'];
$query = "INSERT INTO comment(post_id,name,comment) VALUES('$post_id','$namee','$comment')";
$result = mysqli_query($con, $query);
if ($result) {
header("location:home.php?success=submitted");
} else {
header("location:home.php?error=couldnotsubmit");
}
}
?>
Here is announcement.php where announcements are put in the database
<?php
session_start();
//$con = mysqli_connect('freedb.tech', 'freedbtech_arvindra', 'Arv5n321', 'freedbtech_remote') or die(mysqli_error($con));
$con = mysqli_connect('localhost', 'root', 'Arv5n321', 'userregistration') or die(mysqli_error($con));
if (isset($_POST['announcement'])) {
$image = $_FILES['image']['tmp_name'];
$name = $_FILES['image']['name'];
$image = base64_encode(file_get_contents(addslashes($image)));
date_default_timezone_set("America/New_York");
$title = $_POST['announcementTitle']." (<b>".date("m/d/Y")." ".date("h:i:sa")."</b>)";
$paragraph = $_POST['announcementBox'];
if (empty($paragraph)||empty($title)) {
header('location:home.php?error=fillintheblanks');
}else{
$nam = $_SESSION['username'];
$query = "insert into announcement(name,announcementTitle,announcement,image) values('$nam','$title','$paragraph','$image')";
$result = mysqli_query($con, $query);
if ($result) {
header("location:home.php?success=submitted");
} else {
header("location:home.php?error=couldnotsubmit");
}
}
}else if (isset($_POST['delete'])){
$query = "delete from announcement where id='".$_POST['postID']."';";
$result = mysqli_query($con,$query);
if ($result) {
header('location:home.php?success=deleted');
} else {
header('location:home.php?error=couldnotdelete');
}
}
else {
header('location:home.php');
}
I am a little new to PHP so any help is good.

echo is showing output in page source instead of on page

I made a table in php and wanted to show the Id's in the dropdown select menu by making a separate file for php. So the code in main file is:
<?php include "functions.php";?>
<form action="login_update.php" method="post">
<div class="form-group">
<label for="username">username</label>
<input type="text" name="username" class="form-control">
</div>
<div class="form-group">
<label for="password">password</label>
<input type="password" name="password" class="form-control">
</div>
<div class="form-group">
<select name="id" id="">
<?php
showAllData();
echo "<br>"."askfkldfjl;adfafladfdf";
?>
</select>
</div>
<input class="btn btn-primary" type="submit" name="submit" value="update">
</form>
The code of functions.php is :
<?php
function showAllData(){
$connection = mysqli_connect('localhost','root','****','loginapp');
if($connection){
echo "We are connected. a=".$a."<br>";
}else{
die("Database connection failed");
}
$query = "SELECT * FROM users";
$result = mysqli_query($connection,$query);
if($result){
echo("<br>"." <b><h6>We are successful</h6></b>");
}
else {
die("Query FAILED" . mysqli_error());
}
while($row = mysqli_fetch_assoc($result)) {
$id = $row["id"];
echo"<option value='$id'>$id</option>";
}
}
?>
The expected output was :
But the output is:
So the top two lines in the above screenshot are not printing.
These lines are shown in the INSPECT ELEMENT in chrome.
I forgot to mention the echo command:
echo "<br>"."askfkldfjl;adfafladfdf";
below show all data is also not working.
You made a mistake.
Actually you wrote a code in selectbox and you dont add option so thats why it is not show in html
So write a code like below code so its show in select box as option.
<div class="form-group">
<select name="id" id="">
<option> <?php
showAllData();
echo "<br>"."askfkldfjl;adfafladfdf";
?></option>
</select>
</div>
And If you want to show option from showAllData(); function, the you have return the html.
For this update your showAllData(); function with below code:
function showAllData(){
$options="";
$connection = mysqli_connect('localhost','root','****','loginapp');
if($connection){
echo "We are connected. a=".$a."<br>";
}else{
die("Database connection failed");
}
$query = "SELECT * FROM users";
$result = mysqli_query($connection,$query);
if($result){
echo("<br>"." <b><h6>We are successful</h6></b>");
}
else {
die("Query FAILED" . mysqli_error());
}
while($row = mysqli_fetch_assoc($result)) {
$id = $row["id"];
$options.="<option value='$id'>$id</option>";
}
return $options;
}
Move the PHP function showAllData() before the HTML <select> element.
Because the <select> element awaits for an <option> element, but all another text will not be visible on page.
E.g.:
<div class="form-group">
<?php showAllData(); ?>
</div>
<?php
function showAllData(){
$connection = mysqli_connect('localhost','root','****','loginapp');
if($connection){
echo "We are connected. a=".$a."<br>";
}else{
die("Database connection failed");
}
$query = "SELECT * FROM users";
$result = mysqli_query($connection,$query);
if($result){
echo("<br>"." <b><h6>We are successful</h6></b>");
}
else {
die("Query FAILED" . mysqli_error());
}
echo '<select name="id" id="">';
while($row = mysqli_fetch_assoc($result)) {
$id = $row["id"];
echo"<option value='$id'>$id</option>";
}
echo "</select>";
}
?>
Your code is mixed up.
You can use below code. Create an array which gives you values which you needs to show in select.
<?php
function showAllData(){
$idArr = array('msg'=>'','data'=>'','status'=>0);
$connection = mysqli_connect('localhost','root','****','loginapp');
if($connection){
$idArr['msg'] = "We are connected";
$idArr['status'] = 1;
}else{
$idArr['msg'] = "Database connection failed";
$idArr['status'] = 0;
}
if($idArr['status'] == 1){
$query = "SELECT * FROM users";
$result = mysqli_query($connection,$query);
if($result){
$idArr['msg'] = "We are successful";
$idArr['status'] = 1;
}else {
$idArr['msg'] = "Query FAILED" . mysqli_error();
$idArr['status'] = 0;
}
if($idArr['status'] == 1){
while($row = mysqli_fetch_assoc($result)) {
$idArr['data'][] = $row["section_id"];
}
}
}
return $idArr;
}
$idArr = showAllData();
?>
<?php
if(!empty($idArr['data'])){
echo "We are connected<br>";
echo("<br>"." <b><h6>We are successful</h6></b>");
?>
<form action="login_update.php" method="post">
<div class="form-group">
<label for="username">username</label>
<input type="text" name="username" class="form-control">
</div>
<div class="form-group">
<label for="password">password</label>
<input type="password" name="password" class="form-control">
</div>
<div class="form-group">
<select name="id" id="">
<option value="0">--Select--</option>
<?php
foreach ($idArr['data'] as $key => $value) {
echo"<option value='$value'>$value</option>";
}
?>
</select>
</div>
<input class="btn btn-primary" type="submit" name="submit" value="update">
</form>
<?php }else{
echo $idArr['msg'];
}
?>
Why you are putting <br> inside the select tag? select tag only accept the options tag under it, so please remove br tag and all extra strings inside the select tag. and you should echo the message above the select tag if you want to show your users.
Thanks

My PHP Code is Not Updating Values In Database

I have tried to write a code that update category in the database using admin panel but whenever i try to do that it won't work and i don't get any errors to look into it, please help guys; thanks a lot
PHP Code:
<?php
if (isset($_GET['edit'])) {
$edit_id = $_GET['edit'];
$query = "SELECT * FROM categories WHERE category_id = $edit_id ";
$edit_get_result = mysqli_query($connection,$query);
if (!$edit_get_result) {
die("Edit Get Result Query FAILED");
}
while ($category_name_row=mysqli_fetch_assoc($edit_get_result)) {
$category_name = $category_name_row['category_name'];
}
?>
<center>
<form action="category.php" method="POST">
<div class="form-group">
<label for="update_category">Update Category</label>
<input type="text" class="form-control" id="update_category" value="<?php if(isset($category_name)){echo $category_name; } ?>" name="update_category" aria-describedby="emailHelp" placeholder="Enter Category Name">
</div>
<button type="submit" name="update_category_submit" class="btn btn-primary">Update</button>
</form>
</center>
<?php
if (isset($_POST['update_category_submit'])) {
$category_name = $_POST['update_category'];
$query = "UPDATE categories SET category_name = '$category_name' WHERE category_id = $edit_id ";
$final_update_query_result = mysqli_query($connection,$query);
if (!$final_update_query_result) {
die("Final Update Query Result FAILED");
}
}
}
?>
Please check below code. You need to pass edit_id in your form POST. I have put it in a hidden input and set it's value according to the GET parameter from top of your php part.
<?php
if (isset($_GET['edit'])) {
$edit_id = mysqli_real_escape_string($connection,$_GET['edit']);
$query = "SELECT * FROM categories WHERE category_id = '$edit_id' ";
$result = mysqli_query($connection,$query);
if(!$result) {
die("Edit Get Result Query FAILED");
}
while ($row=mysqli_fetch_assoc($result)) {
$category_name = $row['category_name'];
}
?>
<center>
<form action="category.php" method="POST">
<div class="form-group">
<label for="update_category">Update Category</label>
<input type="text" class="form-control" id="update_category" value="<?php if(isset($category_name)){echo $category_name; } ?>" name="update_category" aria-describedby="emailHelp" placeholder="Enter Category Name">
</div>
<input type="hidden" name="edit_id" value="<?php if(isset($edit_id)) echo $edit_id;?>">
<button type="submit" name="update_category_submit" class="btn btn-primary">Update</button>
</form>
</center>
<?php
if (isset($_POST['update_category_submit']) && isset($_POST['edit_id'])) {
$category_name = mysqli_real_escape_string($connection,$_POST['update_category']);
$edit_id = mysqli_real_escape_string($connection,$_POST['edit_id']);
$query = "UPDATE categories SET category_name = '$category_name' WHERE category_id = $edit_id ";
$result = mysqli_query($connection,$query);
if (!$result) {
die("Final Update Query Result FAILED");
}
else echo "Final Update Query Result Success";
}
?>
Hi have noticed that you have used raw inputs. try avoiding it. Also noticed your code had extra curly braces at the end.
Please try using the following code after replacing your end page section php script.
if (isset($_POST['update_category_submit'])) {
$category_name = $_POST['update_category'];
$query = "UPDATE categories SET category_name = '$category_name' WHERE category_id = $edit_id ";
$final_update_query_result = mysqli_query($connection,$query);
if (!$final_update_query_result) {
die("Final Update Query Result FAILED");
}
}
And Change your query variable to the following:
$query = "SELECT * FROM categories WHERE category_id = ".$edit_id;

Second updating page files disappear

I have a form with uploading multiple files.
When I upload let's say two images and when I click submit, the images are displayed properly, but when I edit some other input and click submit button, the images are gone.
Here's my code:
<?php
if(isset($_GET['id'])) {
$id = $_GET['id'];
}
$query = "SELECT * FROM posts WHERE id = $id";
$result = $db->query($query);
while($row = $db->fetch_object($result)) {
$id = $row->id;
$title = $row->title;
$body = $row->body;
$status = $row->status;
}
if(isset($_POST['submit'])) {
$title = $_POST['title'];
$body = $_POST['body'];
$image = $_POST['image'];
$status = $_POST['status'];
//if($_FILES['image']['tmp_name']) {
if(!empty($_FILES['image']['name'])) { //Edit
// delete old image
$query = "SELECT * FROM postimage WHERE post_id = $id";
$select_image = $db->query($query);
while($row = $db->fetch_object($select_image)) {
$old = $row->filename;
unlink('../uploads/' . $old);
}
$query = "DELETE FROM postimage WHERE post_id = $id";
$delete_images = $db->query($query);
foreach($_FILES['image']['tmp_name'] as $key => $tmp_name) {
$filename = rand(100,999)."-".$_FILES['image']['name'][$key];
$filetmp = $_FILES['image']['tmp_name'][$key];
if(move_uploaded_file($filetmp, '../uploads/' . $filename)) {
$query = "INSERT INTO postimage(post_id, filename) ";
$query .= "VALUES($id, '$filename')";
$insert_images = $db->query($query);
}
}
}
$query = "UPDATE posts SET ";
$query .= "title = '$title', ";
$query .= "body = '$body', ";
$query .= "status = '$status', ";
$query .= "updated = now() ";
$query .= "WHERE id = $id ";
$update_post = $db->query($query);
//header("Location: posts.php");
}
?>
<form action="" method="post" enctype="multipart/form-data">
<div class="form-item">
<label for="title">Post title</label>
<input type="text" value="<?php echo $title; ?>" name="title">
</div>
<div class="form-item">
<label for="body">Post body</label>
<textarea id="editor" name="body" rows="10" cols="30"><?php echo $body; ?></textarea>
</div>
<div class="form-item">
<label for="image">Image</label>
<?php
$query = "SELECT * FROM postimage WHERE post_id = $id";
$select_image = $db->query($query);
while($row = $db->fetch_object($select_image)) {
$filename = $row->filename;
echo '<img width="100" height="70" src="../uploads/' . $filename . '">';
}
?>
<input type="file" name="image[]" multiple>
</div>
<div class="form-item">
<label for="status">Post status</label>
<select name="status">
<option value="<?php echo $status; ?>"><?php echo $status; ?></option>
<?php
if($status == 'published') {
echo '<option value="draft">draft</option>';
} else {
echo '<option value="published">published</option>';
}
?>
</select>
</div>
<div class="form-item">
<input type="submit" class="form-submit" name="submit" value="Update post">
</div>
</form>
When I'm using simple form with uploading a single image and connects with only one table, I usually fix that problem with this:
if(empty($image)) {
$query = "SELECT * FROM posts WHERE id = $id";
$result = $db->query($query);
while($row = $db->fetch_object($result)) {
$image = $row->image;
}
}
How can I solve this problem using multiple upload input?
EDITED: change it to
if(!empty($_FILES['image']['name']))
because $_FILES['image']['tmp_name'] always return path to temp so jou cannot test it for empty
I've solved the problem.
When using multiple file upload, the if statement has to be changed, so in this case it should be: if(!empty($_FILES['image']['tmp_name'][0]))

PHP Stuck on Mysqli_error()

I am new to php still learning i have made a navigation with the drop down submenus its working when im inserting new item in navigation but the problem exist when i try to edit the same navigation i get the error in function everything seems fine need help.
![Edit Form][1]
Error:
<code>
Database Query Failed in get subject by idYou have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LIMIT 1' at line 1
</code>
functions.php
<code>
// Get Subject By ID
function get_subject_by_id($subject_id){
global $connection;
$query = "SELECT * FROM subjects WHERE id = {$subject_id} LIMIT 1";
$result_set = mysqli_query($connection, $query);
if(!$result_set){
die("Database Query Failed in get subject by id" . mysqli_error($connection));
}
if($subject_data = mysqli_fetch_array($result_set)) {
return $subject_data;
} else {
return null;
}
}
</code>
edit_subject.php
<code>
<?php include('includes/connection.php'); ?>
<?php require_once('includes/functions.php'); ?>
<?php
if(isset($_POST['submit'])) {
$id = $_GET['subj'];
$menu_name = $_POST['menu_name'];
$position = $_POST['position'];
$visible = $_POST['visible'];
$content = $_POST['content'];
$query = "UPDATE subjects SET menu_name = '{$menu_name}', position = {$position}, visible = {$visible}, content = '{$content}' WHERE id = {$id}";
$result_update = mysqli_query($connection, $query);
if(mysqli_affected_rows($connection) == 1){
$message = "The Subject was successfully created.";
} else {
}
}
?>
<?php
if(isset($_GET['subj'])){
$sel_subject = get_subject_by_id($_GET['subj']);
$sel_page = NULL;
} elseif (isset($_GET['page'])) {
$sel_subject = NULL;
$sel_page = get_page_by_id($_GET['page']);
} else {
$sel_subject = NULL;
$sel_page = NULL;
}
?>
<?php include('includes/header.php'); ?>
<div class="sidebar">
<ul class="sideNav">
<?php
$query_sub = "SELECT * FROM subjects";
$subject_set = mysqli_query($connection, $query_sub);
if(!$subject_set){
die("Database Query Failed1");
}
while($subject = mysqli_fetch_array($subject_set)){
?>
<li><?php echo $subject["menu_name"]; ?>
<?php
$query_page = "SELECT * FROM pages WHERE subject_id = {$subject["id"]}";
$page_set = mysqli_query($connection, $query_page);
if(!$page_set){
die("Database Query Failed2");
} ?>
<ul>
<?php
while($page = mysqli_fetch_array($page_set))
{
?><li><?php echo $page["menu_name"]; ?></li><?php
} ?>
</ul>
</li>
<?php } ?>
</ul>
<br>
+ Add a new Subject
</div><!-- end of sidebar -->
<h2>Edit Subject: <?php echo $sel_subject['menu_name']; ?></h2>
<div class="main">
<br/> <br/>
<?php if(!empty($message)) { ?> <p><?php echo $message; ?></p> <?php } ?>
<form action="edit_subject.php?subj=<?php $sel_subject['id']; ?>" method="post">
<fieldset>
<legend>Edit Subject:</legend>
<p>Subject Name:
<input type="text" name="menu_name" value="<?php echo $sel_subject['menu_name']; ?>">
</p>
<p>Position:
<select name="position">
<?php
$query_opt = "SELECT * FROM subjects ORDER BY position ASC";
$subject_opt = mysqli_query($connection, $query_opt);
if(!$subject_opt){
die("Database Query Failed3");
}
$subject_count = mysqli_num_rows($subject_opt);
for($count=1; $count <= $subject_count+1; $count++){
echo "<option value=\"{$count}\"";
if($sel_subject['position'] == $count){
echo " selected";
}
echo ">{$count}</option>";
}
?>
</select>
</p>
<p>Visible:
<input type="radio" name="visible" value="0"<?php if($sel_subject['visible'] == 0){ echo " checked"; } ?>> No
<input type="radio" name="visible" value="1"<?php if($sel_subject['visible'] == 1){ echo " checked"; } ?>> Yes
</p>
<p>Content:<br/>
<textarea name="content" rows="20" cols="150"><?php echo $sel_subject['content']; ?></textarea>
</p>
<p>
<input type="submit" name="submit" value="Add Subject" class="button-submit">
</p>
</fieldset>
</form>
<br /><br />
</div><!-- end of main -->
<?php include('includes/footer.php'); ?>
</code>
Are you sure that $subject_id in function get_subject_by_id() is not empty ? Try the below code after $query = "SELECT * FROM subjects WHERE id = {$subject_id} LIMIT 1"; in functions.php (only for testing) and make sure that your query is correct.
echo $query;
die();
you've missed the single quotes in the query string.
$query = "SELECT * FROM subjects WHERE id = {$subject_id} LIMIT 1";
should be
$query = "SELECT * FROM subjects WHERE id = '{$subject_id}' LIMIT 1";
youve also done it here.
$query = "UPDATE subjects SET menu_name = '{$menu_name}', position = {$position}, visible = {$visible}, content = '{$content}' WHERE id = {$id}";
should be
$query = "UPDATE subjects SET menu_name = '{$menu_name}', position = '{$position}', visible = '{$visible}', content = '{$content}' WHERE id = '{$id}'";
a note you dont need to wrap in {} for simple variables. You only need them if calling complex variables such as {$array['test']} as expained in this answer

Categories