I have two tables,from that am fetching data with left join - php

post id is the common field in both the tables.If I want to select the comments under same post_id,only 1 time the post should display and all its comments under it..
this is my code....
home.php
<?php
$sql="SELECT family_news.post_id,family_news.username,family_news.post,
family_news.fileToUpload,family_news.description,
family_comments.post_id,family_comments.comment,
family_comments.commenter
FROM family_news
LEFT JOIN family_comments ON family_news.post_id = family_comments.post_id
ORDER BY family_news.post_id DESC";
if($result = mysqli_query($con, $sql))
{
while($row = mysqli_fetch_array($result))
{
?>
<div class="form-group">
<b style="font-size:30px;color:brown;">
<?php
echo $row['username'];
?>
</b>
<br>
<?php
if($row['fileToUpload'] != NULL)
{
?>
<form action="post_comment.php" method="POST">
<input class="form-control" type="hidden" name="post_id" value="<?php echo $row['post_id']; ?>">
<br>
<img class="img-responsive" src="../admin_panel/uploads/<?php echo $row['fileToUpload']; ?>" alt="Image Loading" height='80' width='120'>
<div class="form-group" style="font-size:15px;color:purple;">
<?php
echo $row['description'];
?>
<br>
<h3 style="color:black">Comments</h3>
<h3 style="color:red">
<?php
echo $row['commenter'];
?>
</h3>
<h4 style="color:saddlebrown">
<?php
echo $row['comment'];
?>
</h4>
<br>
<input class="form-control" type="text" name="comment" placeholder="Write a comment">
<br>
<input class="form-control" type="hidden" name="commenter" value="<?php echo $_SESSION['uname']; ?>">
<input type="submit" class="btn btn-primary" value="Post">
</div>
</form>
<?php
}
if($row['post'] != NULL)
{
?>
<br>
<div class="form-group" style="font-size:15px;color:purple;">
<form action="post_comment.php" method="POST">
<input class="form-control" type="hidden" name="post_id" value="<?php echo $row['post_id']; ?>">
<?php
echo $row['post'];
?>
<br>
<h3 style="color:black">Comments</h3>
<h3 style="color:red">
<?php
echo $row['commenter'];
?>
</h3>
<h4 style="color:saddlebrown">
<?php
echo $row['comment'];
?>
</h4>
<br>
<input class="form-control" type="text" name="comment" placeholder="Write a comment">
<br>
<input class="form-control" type="hidden" name="commenter" value="<?php echo $_SESSION['uname']; ?>">
<input type="submit" class="btn btn-primary" value="Post">
</form>
<?php
}
?>
</div>
<?php
}
}
?>
</div>
</section>
post_comment.php
<section class="col-lg-6 connectedSortable">
<?php
$post_id=$_POST['post_id'];
$comment=$_POST['comment'];
$commenter=$_POST['commenter'];
if($comment != NULL) {
$sql="INSERT INTO family_comments
(post_id, comment, commenter)
VALUES ('$post_id', '$comment', '$commenter')";
if($con ->query($sql) == TRUE)
{
echo 'Comment saved Successfully...';
}
} else {
echo 'Please Write Something...';
}
?>
</section>

Related

php script change when submitting a form data

I built a script named add_post.php that submit form data in database.
The script is here:
<?php include ("includes/header.php"); ?>
<div class="content" style="text-align: center;">
<header>
<h1 style="color: rgb(245, 106, 106);">ADMIN AREA</h1><br/>
<p>Movie Post Area</p>
</header>
</div>
</section>
<?php
include("includes/startsession.php");
if(isset($_SESSION['username'])){
?>
<Section>
<?php
if(isset($_POST['submit'])){
$name =mysqli_real_escape_string($con, trim($_POST['name']));
$description = mysqli_real_escape_string($con, trim($_POST['description']));
$year = mysqli_real_escape_string($con, trim($_POST['year']));
$popularity = mysqli_real_escape_string($con, trim($_POST['popularity']));
$download_link = mysqli_real_escape_string($con, trim($_POST['download']));
$tags = mysqli_real_escape_string($con, trim($_POST['tags']));
//upload the name to the temporary folder
$images = mysqli_real_escape_string($con, trim($_FILES['image']['name']));
//select the image from the temporary folder
$images_tmp = $_FILES['image']['tmp_name'];
//move the uploaded image to the `images` folder
move_uploaded_file($images_tmp, "../images/$images");
$output_form = false;
if( empty($name) || empty($description) || empty($year) || empty($popularity) || empty($download_link) || empty($tags) || empty($images)){
echo '<h2>Oops! You seem to forget writing something</h2>';
$output_form = true;
}
if( (!empty($name)) && (!empty($description)) && (!empty($year)) && (!empty($popularity)) && (!empty($download_link)) && (!empty($tags)) && (!empty($images))){
$insert = "INSERT INTO movies (name, description, year, image, popularity, download_link, tags)
VALUES ('$name', '$description', '$year', '$images', '$popularity', '$download_link', '$tags') " or die(mysqli_error);
$run = mysqli_query($con, $insert);
echo "<h2>The Movie has been added.<h2>";
echo 'RESET';
}
} else{
$output_form = true;
$name = '';
$description = '';
$year = '';
$popularity = '';
$download_link = '';
$tags = '';
$images = '';
}
?>
<?php if($output_form = true){ ?>
<form enctype="multipart/form-data" method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
<div class="row uniform">
<div class="6u 12u$(xsmall)" style="float:none">
<label>Movie Name</label>
<input type="text" name="name" value="<?php echo $name; ?>" id="demo-name" value="" placeholder="Enter Movie Name" />
</div>
<br/>
<div class="6u 12u$(xsmall)" style="float:none">
<label>Description</label>
<textarea name="description" id="demo-message" placeholder="Enter Description" rows="6"><?php echo $description; ?></textarea>
</div>
<br/>
<div class="6u 12u$(xsmall)" style="float:none">
<label>Year</label>
<input type="text" name="year" value="<?php echo $year; ?>" id="demo-name" placeholder="Enter Year" />
</div>
<br/>
<div class="6u 12u$(xsmall)" style="float:none">
<label>Image</label>
<input type="file" name="image" value="<?php echo $images; ?>" id="demo-name" class="special" />
</div>
<div class="12u$" style="float:none">
<div class="select-wrapper">
<label>Popularity</label>
<select name="popularity" id="demo-category">
<option <?php if ($popularity == 'Yes'){echo 'selected';} ?> >Yes</option>
<option <?php if ($popularity == 'No'){echo 'selected';} ?> >No</option>
</select>
</div>
</div>
<div class="6u 12u$(xsmall)" style="float:none">
<label>Download Link</label>
<input type="text" name="download" value="<?php echo $download_link; ?>" id="demo-name" value="" placeholder="Enter Download Link" />
</div>
<div class="6u 12u$(xsmall)" style="float:none">
<label>Tags</label>
<input type="text" name="tags" value="<?php echo $tags; ?>" id="demo-name" value="" placeholder="Enter Tags seperated by commas(,)" />
</div>
<!-- Break -->
<div class="12u$" style="float:none">
<ul class="actions">
<li><input type="submit" name="submit" value="ADD MOVIE" class="special" /></li>
</ul>
</div>
</div>
</form>
<?php } ?>
<?php
} else{
header('Location: login.php');
}
?>
when I submit the form the page returns the script of index.php. When i refresh the page then the page shows the content of add_post.php and above all the data content of the form did not add to the database.
I don't know whats causing this problem and how to fix it.

Adding outputted table row to another table using codeigniter

Outputted a row from a table ,am now trying to add some of the contents to another table using a form which is inside the outputted row but its not inserted to the database. am not getting an error.
Here is my view
<?php
foreach ($h->result() as $row)
{?>
<div class="row invoice-info">
<div class="col-sm-1 invoice-col">
</div>
<div class="col-sm-3 invoice-col">
<img src="<?php echo base_url()?>/res/images/goods/1.png">
</div>
<div class="col-sm-4 invoice-col">
<address>
Description: <?php echo $row->description;?><br>
Location Address: <?php echo $row->l_area;?><br>
Destination Address: <?php echo $row->d_area;?><br>
Date: <?php echo $row->dom;?><br>
Time: <?php echo $row->tom;?>
</address>
</div>
<div class="col-sm-2 invoice-col">
<address>
</address>
</div><!-- /.col -->
<div class="col-sm-2 invoice-col">
<form action="<?php echo site_url('truckeraccount_ctrl/bid'); ?>" method="post">
<input type="hidden" class="form-control" name="truckerid" value="<?php
$truckerid = $this->session->userdata('truckerid');
echo $truckerid; ?>" required>
<input type="hidden" class="form-control" name="luggage_id" value="<?php echo $row->luggage_id;?>" placeholder="Bid">
<input type="text" class="form-control" name="bid_amount" placeholder="Bid">
<button type="submit" class="btn bg-orange btn-flat margin">Place Bid</button>
</div>
</div>
My model
function bid($data){
$query=$this->db->update('bids',$data);
return $query;
}
My controller
public function bid(){
$this->load->database();
$this->load->model('Truckeraccount_model');
$data['a']=$this->Truckeraccount_model->accepted_bid();
$data['b']=$this->Truckeraccount_model->picked_loads();
$data['h']=$this->Truckeraccount_model->loads();
$data['g']=$this->Truckeraccount_model->notification();
$data['i']=$this->Truckeraccount_model->return_loads();
$data['accepted_return_loads']=$this->Truckeraccount_model->accepted_return_loads();
$data['bid_amount']=$this->Truckeraccount_model->bid_amount(); $this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
$this->form_validation->set_rules('bid_amount', 'bid_amount', 'required|min_length[1]|max_length[50]');
if ($this->form_validation->run() == FALSE) {
$this->load->view('header');
$this->load->view('truckeraccount_view',$data);
$this->load->view('footer');
} else {
$data = array(
'truckerid' => $this->input->post('truckerid'),
'luggage_id' => $this->input->post('luggage_id'),
'bid_amount' => $this->input->post('bid_amount'),
);
$this->Truckeraccount_model->bid($data);
$data['message'] = '';
redirect('truckeraccount_ctrl/');
}
}
<form action="<?php echo site_url('truckeraccount_ctrl/bid'); ?>" method="post">
<input type="hidden" class="form-control" name="truckerid" value="<?php
$truckerid = $this->session->userdata('truckerid');
echo $truckerid; ?>" required>
<input type="hidden" class="form-control" name="luggage_id" value="<?php echo $row->luggage_id;?>" placeholder="Bid">
<input type="text" class="form-control" name="bid_amount" placeholder="Bid">
<button type="submit" class="btn bg-orange btn-flat margin">Place Bid</button>
</form>

PHP : Login in to the admin page

I am trying to login into the admin and lecturer page in the following code.But it is not working properly.When i login after entering loginid and password, click on submit , no error occured.
admin.php
<?php
session_start();
if(isset($_SESSION["userid"]))
{
if($_SESSION["type"]=="admin")
{
header("Location: dashboard.php");
}
else
{
header("Location: lectureaccount.php");
}
}
include("header.php");
include("conection.php");
if(isset($_POST["uid"]) && isset($_POST["pwd"]) )
{
// echo "sdfsd". $_POST[uid];
$result = mysql_query("SELECT * FROM administrator WHERE adminid='$_POST[uid]'");
while($row = mysql_fetch_array($result))
{
$pwdmd5 = $row["password"];
}
if(md5($_POST["pwd"])=='$pwdmd5')
{
$_SESSION["userid"] = $_POST["uid"];
$_SESSION["type"]="admin";
header("Location: dashboard.php");
}
else
{
$log = "Login failed.. Please try again..";
}
}
if(isset($_POST["luid"]) && isset($_POST["lpwd"]))
{
$result = mysql_query("SELECT * FROM lectures WHERE lecid='$_POST[luid]'");
while($row = mysql_fetch_array($result))
{
$pwdm= $row["password"];
$_SESSION["lecname"] = $row["lecname"];
$_SESSION["coid"] = $row["courseid"];
}
//echo"pwd". md5($_POST["lpwd"]);
if(md5($_POST["lpwd"])==$pwdm)
{
//echo $_POST["lpwd"];
$_SESSION["userid"] = $_POST["luid"];
$_SESSION["type"]=="lecturer";
header("Location: lectureaccount.php");
}
else
{
$log12 = "Login failed.. Please try again..";
}
}
?>
<section id="page">
<header id="pageheader" class="normalheader">
<h2 class="sitedescription">
</h2>
</header>
<section id="contents">
<article class="post">
<header class="postheader">
<h2><u>Admin Login</u></h2>
<?php $log = isset($_POST['log']) ?>
<h2><?php echo $log;?></h2>
</header>
<section class="entry">
<form action="admin.php" method="post" class="form">
<p class="textfield">
<label for="author">
<small>Admin Login ID (required)</small>
</label>
<input name="uid" id="uid" value="" size="22" tabindex="1" type="text">
</p>
<p class="textfield">
<label for="email">
<small>Password (required)</small>
</label>
<input name="pwd" id="pwd" value="" size="22" tabindex="2" type="password">
</p>
<p>
<input name="submit" id="submit" tabindex="5" type="image" src="images/submit.png">
<input name="comment_post_ID" value="1" type="hidden">
</p>
<div class="clear"></div>
</form>
<form action="admin.php" method="post" class="form">
<div class="clear">
<hr />
<header class="postheader">
<h2><u>Lectures Login</u></h2>
<?php $log12 = isset($_POST['log12']) ?>
<h2><?php echo $log12;?></h2>
</header>
<section class="entry">
<p class="textfield">
<label for="author2"> <small><br />
Lecture Login ID (required)</small> </label>
<input name="luid" id="luid" value="" size="22" tabindex="3" type="text" />
</p>
<p class="textfield">
<label for="email2"> <small>Password (required)</small> </label>
<input name="lpwd" id="lpwd" size="22" tabindex="4" type="password" />
</p>
<p>
<input name="submit2" id="submit2" tabindex="5" type="image" src="images/submit.png" />
<input name="comment_post_ID2" value="1" type="hidden" />
</p>
<div class="clear"></div>
</form>
<div class="clear"></div>
</section>
</div>
</section>
</article>
</section>
<?php
include("adminmenu.php");
include("footer.php"); ?>
Database:
table fields in administrator are: adminid,password,adminname,address,contactno
table fields in lectures are:
lecid,password,courseid,lecname,gender,address,contactno.
Please provide solution for this issue.
instead of using this code
if(md5($_POST["pwd"])=='$pwdmd5')
use this
if(md5($_POST["pwd"])==$pwdmd5)
In the first case, the hashed pasword is compared to the string $pwdmd5, in the second to the content of $pwdmd5

Comments on the page wont show up

Hello i have a problem i cant see the comments on the page.
I dont get any error on the page so i am stuck on this moment
can somebody help me??
This is the php code:
<div id="container">
<?php include('includes/menu.php');?>
<div id="post">
<?php
$row = $query->fetch_object();
echo "<h2>".$row->title."</h1>";
echo "<p>".$row->body."</p>";
?>
</div>
<hr />
<div id="add-comments">
<form action="<?php echo $_SERVER['PHP_SELF']."?id=$id"?>" method="post">
<div>
<label>Email Adres</label><input type="text" name="email" />
</div>
<div>
<label>Naam</label><input type="text" name="name" />
</div>
<div>
<label>Commentaar</label><textarea name="comment"></textarea>
</div>
<input type="hidden" name="post_id" value="<?php echo $id?>" />
<input type="submit" name="submit" value="Toevoegen"/>
</form>
</div>
<hr />
<div id="comments">
<?php
$query = $db->query("SELECT * FROM comments WHERE post_id='$id' ORDER BY comment_id DESC");
while($row = $query->fetch_object()):
?>
<div>
<h5><?php echo $row->name?></h5>
<blockquote><?php echo $row->comment?></blockquote>
<?php endwhile;?>
</div>
</div>
</div>
and the rest of the page is:
<div id="container">
<?php include('includes/menu.php');?>
<div id="post">
<?php
$row = $query->fetch_object();
echo "<h2>".$row->title."</h1>";
echo "<p>".$row->body."</p>";
?>
</div>
<hr />
<div id="add-comments">
<form action="<?php echo $_SERVER['PHP_SELF']."?id=$id"?>" method="post">
<div>
<label>Email Adres</label><input type="text" name="email" />
</div>
<div>
<label>Naam</label><input type="text" name="name" />
</div>
<div>
<label>Commentaar</label><textarea name="comment"></textarea>
</div>
<input type="hidden" name="post_id" value="<?php echo $id?>" />
<input type="submit" name="submit" value="Toevoegen"/>
</form>
</div>
<hr />
<div id="comments">
<?php
$query = $db->query("SELECT * FROM comments WHERE post_id='$id' ORDER BY comment_id DESC");
while($row = $query->fetch_object()):
?>
<div>
<h5><?php echo $row->name?></h5>
<blockquote><?php echo $row->comment?></blockquote>
<?php endwhile;?>
</div>
</div>
</div>
Hope somebody see the problem.
try like this for printing comments..
<div id="comments">
<?php
$query = $db->query("SELECT * FROM comments WHERE post_id='$id' ORDER BY comment_id DESC");
while($row = $query->fetch_object()){ //opening while block
?>
<div>
<h5><?php echo $row->name;?></h5>
<blockquote><?php echo $row->comment;?></blockquote>
<?php }// ending while block ?>
</div>
</div>
<div id="comments">
<?PHP
$query = $db->query("SELECT name, comment FROM comments WHERE post_id='$id' ORDER BY comment_id DESC");
while($row = $query->fetch_object()) {
?>
<div>
<h5><?= $row->name ?></h5>
<blockquote><?= $row->comment ?></blockquote>
</div>
<?PHP } ?>
</div>
If that does not work, i would suggest dumping out the SQL query and making sure it is working.

PHP form processing and validation is not working

I have the following code for my form, and it doesn't seem to work, the query does not insert after all the fields have been filled. I'll really appreciate it if someone can look at it and help me correct any mistake. The method for the form is post and the action is <?php echo $_SERVER['PHP_SELF'];?>. This is my first code in PHP so kind of new here Thanks.
<?php
$counter=1;
if (isset($_POST["post"])) {
$gender= $_POST['gender'];
$fname= $_POST['fname'];
$lname= $_POST['lname'];
$email= $_POST['email'];
$fone= $_POST['fone'];
$school= $_POST['skuul'];
$other= $_POST['other'];
$output_form=false;
$is_error = false;
if (empty($email)) {
echo "Please enter your email or contact info. \n";
$output_form = $is_error = true;
}
if (empty($lname)) {
echo "Please enter your email or contact info. \n";
$output_form= $is_error = true;
}
if (empty($fone)) {
echo "Please enter your email or contact info. \n";
$output_form= $is_error = true;
}
if (empty($fname)) {
echo "Please enter your name. \n";
$output_form= $is_error = true;
}
if ($output_form) {
$query= "INSERT INTO 'elect_conference' ('gender', 'fname', 'lname', 'phone', 'email', 'School', 'course', 'other')".
"values ('$gender', '$fname', '$lname', '$fone', '$email', '$school', '$course', '$other');";
$result = execute($query);
echo '<article class="extra-wrap">'."Hello $name congratulations, you are the number $counter member registered for the ELECT Conference.
A confirmation will be sent to your email ($email) within five(5) hours.\n
Thank you. And have a nice day.</artice>";
$fname="";
$lname="";
$fone="";
$email="";
$fone="";
$school="";
$subject="";
$counter++;
} else {
echo "Please ensure that all the fields are approprietly filled";}
} else {
$output_form=true;
}
if ($output_form) {
?>
<body id="page2">
<!--==============================header=================================-->
<section id="content">
<div class="pad">
<div class="main">
<article class="grid_9 suffix_1">
<div class="form1">
<div class="padding">
<h4 class="border-bot2 img-indent-bot" align="center" style="color:#00C !important">
ELECT Conference <br/>Registration
</h4>
<form id="form" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"enctype="multipart/form-data">
<fieldset>
<div style="margin-left:90px !important;"><?php echo "$counter/500"; ?></div>
<div class="rowElem0">
<span class="radio">Personal Infomation:</span>
<span class="radio2">
<input type="radio" name="gender" value="Mr" checked>
<label class="ratio">Mr</label>
<input type="radio" name="gender" value="Mrs">
<label class="ratio">Ms</label>
<input type="radio" name="gender" value="Mrs">
<label class="ratio">Mrs</label>
</span>
</div>
<div class="rowElem">
<label><span class="input">First Name:</span></label>
<input type="text" name="fname" value="<?php echo $fname; ?>"/>
</div>
<div class="rowElem">
<label><span class="input">Last Name:</span></label>
<input type="text" name="lname" value="<?php echo $lname; ?>"/>
</div>
<div class="rowElem">
<label><span class="input">Phone:</span></label>
<input type="text" name="fone" value="<?php echo $fone; ?>"/>
</div>
<div class="rowElem">
<label><span class="input">E-mail:</span></label>
<input type="text" name="email" value="<?php echo $email; ?>"/>
</div>
<div class="rowElem1">
<input type="radio" name="graduate" id="chbox4" checked>
<strong><label class="check2">Graduate?</label></strong>
</div>
<div class="rowElem">
<label><span class="input">School:</span></label>
<input type="text" name="skuul" value="<?php echo $school; ?>"/>
</div>
<div class="rowElem">
<label><span class="input">Course:</span></label>
<input type="text" name="course" value="<?php echo $school; ?>"/>
</div>
<div class="rowElem1">
<input type="radio" name="other" id="chbox5">
<strong><label class="check2">Other</label></strong>
<label><span class="input">Specify:</span></label>
<input type="text" name="other" value="<?php echo $other; ?>"/>
</div>
<div class="buttons">
<input class="button" type="submit" value="Submit" />
</div>
</fieldset>
</form>
</div>
</div>
</article>
</div>
</div>
</div>
</section>
<script type="text/javascript"> Cufon.now(); </script>
</body>
<?php
}
?>
</html>
Please show the form code, and be specific in the outcome of the form processing. What exactly didn't work?
In the mean time here are a couple of observations:
Instead of assigning each $_POST element at a time you can do this:
foreach ($_POST as $key=>$value) $$key = $value;
*$output_form* and *$is_error* always have the same value, why the duplication?

Categories