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]))
Related
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.
I have this problem updating content into database. error is :
Query FailedYou have an error in your SQL syntax; check the
manual that corresponds to your MariaDB server version for the right
syntax to use near 'post_content = 'kjgljkkjhklj ', post_image =
'45739895_2381062595269282_8123898' at line 1
this is my whole code:
if (isset($_GET['p_id'])) {
$the_post_id = $_GET['p_id'];
}
$query = "SELECT * FROM posts WHERE post_id = $the_post_id";
$select_posts_by_id = mysqli_query($connection, $query);
while ($row = mysqli_fetch_assoc($select_posts_by_id)) {
$post_id = $row['post_id'];
$post_author = $row['post_author'];
$post_title = $row['post_title'];
$post_category_id = $row['post_category_id'];
$post_status = $row['post_status'];
$post_image = $row['post_image'];
$post_content = $row['post_content'];
$post_tags = $row['post_tags'];
$post_comment = $row['post_comment_count'];
$post_date = $row['post_date'];
}
// if update post button is clicked
if (isset($_POST['update_post'])) {
$post_author = $_POST['post_author'];
$post_title = $_POST['post_title'];
$post_category_id = $_POST['post_category'];
$post_status = $_POST['post_status'];
$post_image = $_FILES['image']['name'];
$post_image_temp = $_FILES['image']['tmp_name'];
$post_content = $_POST['post_content'];
$post_tags = $_POST['post_tags'];
move_uploaded_file($post_image_temp, "../images/{$post_image}");
if (empty($post_image)) {
$query = "SELECT * FROM posts WHERE post_id = $the_post_id ";
}
$query = "UPDATE posts SET ";
$query .= "post_title = '{$post_title}', ";
$query .= "post_category_id = '{$post_category_id}', ";
$query .= "post_date = now(), ";
$query .= "post_author = '{$post_author}', ";
$query .= "post_status = '{$post_status}', ";
$query .= "post_tags = '{$post_tags}' ";
$query .= "post_content = '{$post_content}', ";
$query .= "post_image = '{$post_image}' ";
$query .= "WHERE post_id = {$the_post_id}"; // this is from the get request
$update_post = mysqli_query($connection, $query);
confirmQuery($update_post);
}
this is the form below:
<form action="" method="POST" enctype="multipart/form-data">
<div class="form-group">
<label for="title">Post Title</label>
<input value="<?php echo $post_title; ?>" type="text" name="post_title" class="form-control" required="true">
</div>
<div class="form-group">
<label for="post_category">Post Categories</label>
<select name="post_category" id="" class="form-control form-control-md">
<?php
$query = "SELECT * FROM categories";
$select_categories = mysqli_query($connection, $query);
confirmQuery($select_categories); // this is from functions.php
while ($row = mysqli_fetch_assoc($select_categories)) {
$cat_id = $row['cat_id'];
$cat_title = $row['cat_title'];
echo "<option value='{$cat_id}'>{$cat_title}</option>";
}
?>
</select>
</div>
<div class="form-group">
<label for="author">Post Author</label>
<input value="<?php echo $post_author; ?>" type="text" name="post_author" class="form-control" required="true">
</div>
<div class="form-group">
<label for="post_status">Post Status</label>
<input value="<?php echo $post_status; ?>" type="text" name="post_status" class="form-control" required="true">
</div>
<div class="form-group">
<label for="image">Post Image</label>
<img width="100px" src="../images/<?php echo $post_image; ?>">
<input type="file" name="image" required="true">
</div>
<div class="form-group">
<label for="post_tags">Post Tags</label>
<input value="<?php echo $post_tags; ?>" type="text" name="post_tags" class="form-control" required="true">
</div>
<div class="form-group">
<label for="post_content">Post Content</label>
<textarea name="post_content" class="form-control" id="" cols="30" rows="10" required="true">
<?php echo $post_content; ?>
</textarea>
</div>
<div class="form-group">
<input type="submit" name="update_post" class="btn btn-primary" value="Update Post">
</div>
</form>
FWIW, I find this easier to read:
$query = "
UPDATE posts
SET post_title = '$post_title'
, post_category_id = '$post_category_id'
, post_date = now()
, post_author = '$post_author'
, post_status = '$post_status'
, post_tags = '$post_tags'
, post_content = '$post_content'
, post_image = '$post_image'
WHERE post_id = $the_post_id;
";
...but it's vital also to replace those strings with a properly parametrised query
try replace
$query .= "WHERE post_id = {$the_post_id}";
with
$query .= "WHERE post_id = {$the_post_id} ";
That should do the magic.
Edit the post in CMS.
i am getting the error.
(QUERY FAILED .You have an error in your SQL syntax; check the manual
that corresponds to your MariaDB server version for the right syntax
to use near 'post_tags = 'thing, about, learning', post_content = 'The
beautiful thing abou' at line 1 )
<?php
if (isset($_GET['p_id'])) {
$the_post_id = $_GET['p_id'];
}
$query = "SELECT * FROM posts WHERE post_id = $the_post_id ";
$select_posts_by_id = mysqli_query($connection,$query);
while($row = mysqli_fetch_assoc($select_posts_by_id)) {
$post_id = $row['post_id'];
$post_title = $row['post_title'];
$post_category_id = $row['post_category_id'];
$post_image = $row['post_image'];
$post_content = $row['post_content'];
$post_date = $row['post_date'];
$post_tags = $row['post_tags'];
$post_comment_count = $row['post_comment_count'];
$post_status = $row['post_status'];
}
if(isset($_POST['update_post'])) {
$post_title = $_POST['post_title'];
$post_category_id = $_POST['post_category'];
$post_tags = $_POST['post_tags'];
$post_status = $_POST['post_status'];
$post_image = $_FILES['image']['name'];
$post_image_temp = $_FILES['image']['tmp_name'];
$post_content = $_POST['post_content'];
move_uploaded_file($post_image_temp, "../images/$post_image");
$query = "UPDATE posts SET ";
$query .="post_title = '{$post_title}', ";
$query .="post_category_id = '{$post_category_id}', ";
$query .="post_tags = '{$post_tags }', ";
$query .="post_status = '{$post_status }', ";
$query .="post_image = '{$post_image}' ";
$query .="post_tags = '{$post_tags }', ";
$query .="post_content = '{$post_content }', ";
$query .="post_comment_count = '{$post_comment_count }', ";
$query .="WHERE post_id = {$the_post_id} ";
$update_post = mysqli_query($connection,$query);
confirmQuery($update_post);
}
?>
<form action="" method="post" enctype="multipart/form-data">
<div class="form-group">
<label for="title">Post Title</label>
<input value="<?php echo $post_title; ?>" type="text" class="form-control" name="post_title">
</div>
<div class="form-group">
<select name="post_category" id="">
<?php
$query = "SELECT * FROM categories";
$select_categories = mysqli_query($connection,$query);
confirmQuery($select_categories);
while($row = mysqli_fetch_assoc($select_categories )) {
$cat_id = $row['cat_id'];
$cat_title = $row['cat_title'];
echo "<option value='$cat_id'>{$cat_title}</option>";
}
?>
</select>
</div>
<div class="form-group">
<label for="post_tags">Post tags </label>
<input value="<?php echo $post_tags; ?>" type="text" class="form-control" name="post_tags">
</div>
<div class="form-group">
<label for="post_status">Post status</label>
<input value="<?php echo $post_status; ?>" type="text" class="form-control" name="post_status">
</div>
<div class="form-group">
<img width="100" src="../images/<?php echo $post_image; ?>" alt="">
<input type="file" name="image">
</div>
<div class="form-group">
<label for="post_content">Post Content</label>
<textarea class="form-control "name="post_content" id"" cols="30" rows="10"><?php echo $post_content; ?></textarea>
</div>
<div class="form-group">
<input class="btn btn-primary" type="submit" name="update_post" value="Update Post">
</div>
</form>
The update statement is missing a comma
$query = "UPDATE posts SET ";
$query .="post_title = '{$post_title}', ";
$query .="post_category_id = '{$post_category_id}', ";
$query .="post_tags = '{$post_tags}', ";
$query .="post_status = '{$post_status}', ";
$query .="post_image = '{$post_image}', "; /* MISSING COMMA */
$query .="post_tags = '{$post_tags}', ";
$query .="post_content = '{$post_content}', ";
$query .="post_comment_count = '{$post_comment_count}' "; /* EXTRA, uneeded comma */
$query .="WHERE post_id = {$the_post_id}";
and an unneeded extra comma before the where clause. Additionally extra spaces before closing curly braces might cause issues. If you were to write the sql like this you can spot issues easier imo.
$query = "UPDATE posts SET
post_title = '{$post_title}',
post_tags = '{$post_tags }',
post_status = '{$post_status}',
post_image = '{$post_image}',
post_tags = '{$post_tags}',
post_content = '{$post_content}',
post_comment_count = '{$post_comment_count}'
WHERE post_id = {$the_post_id}";
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);
I try to make online quiz with images questions, and i need your help/advice.
My images is stored on database where have an id "image". My upload works fine, image is stored on database...but i can't show image in questions.
Here is my structure from database: http://imageshack.com/a/img923/8746/Kf16xl.jpg
And that's my code from show questions with image:
<?php
session_start();
require_once("scripts/connect_db.php");
$arrCount = "";
if(isset($_GET['question'])){
$question = preg_replace('/[^0-9]/', "", $_GET['question']);
$output = "";
$answers = "";
$q = "";
$sql = mysqli_query($connection, "SELECT id FROM questions");
$numQuestions = mysqli_num_rows($sql);
if(!isset($_SESSION['answer_array']) || $_SESSION['answer_array'] < 1){
$currQuestion = "1";
}else{
$arrCount = count($_SESSION['answer_array']);
}
if($arrCount > $numQuestions){
unset($_SESSION['answer_array']);
header("location: index.php");
exit();
}
if($arrCount >= $numQuestions){
echo 'finished|<p>There are no more questions. Please enter your first and last name and click next</p>
<form action="userAnswers.php" method="post">
<input type="hidden" name="complete" value="true">
<input type="text" name="username">
<input type="submit" value="Finish">
</form>';
exit();
}
if (!empty($image)) {
$sqlimage = mysqli_query($connection, "SELECT * FROM questions where 'image' = $image");
$imageresult = mysqli_query($connection, $sqlimage);
while($row=mysqli_fetch_assoc($imageresult))
{
echo '<img height="300" width="300" src="data:image;base64,'.$row[2].' "> ';
}
}
$singleSQL = mysqli_query($connection, "SELECT * FROM questions WHERE id='$question' LIMIT 1");
while($row = mysqli_fetch_array($singleSQL)){
$id = $row['id'];
$thisQuestion = $row['question'];
$type = $row['type'];
$question_id = $row['question_id'];
$q = '<h2>'.$thisQuestion.'</h2>';
$sql2 = mysqli_query($connection, "SELECT * FROM answers WHERE question_id='$question' ORDER BY rand()");
while($row2 = mysqli_fetch_array($sql2)){
$answer = $row2['answer'];
$correct = $row2['correct'];
$answers .= '<label style="cursor:pointer;"><input type="radio" name="rads" value="'.$correct.'">'.$answer.'</label>
<input type="hidden" id="qid" value="'.$id.'" name="qid"><br /><br />
';
}
$output = ''.$q.','.$answers.',<span id="btnSpan"><button onclick="post_answer()">Submit</button></span>';
echo $output;
}
}
?>
The part with show images is:
if (!empty($image)) {
$sqlimage = mysqli_query($connection, "SELECT * FROM questions where 'image' = $image");
$imageresult = mysqli_query($connection, $sqlimage);
while($row=mysqli_fetch_assoc($imageresult))
{
echo '<img height="300" width="300" src="data:image;base64,'.$row[2].' "> ';
}
}
Thank you very much for allocating your time!