I create a spot on my clients website for them to post job openings for there business. I want to give them the option to edit the job post if they make a mistake after they post.
My question is how can I achieve this. Or better yet how can I pull up the info from my database so I can edit / save it?
This is what I have tried:
To get things started here is a screenshot of my database.
phpMyAdmin Database The table is called "hire" without the quotes.
I'm using 3 files/pages to try and get this to update.
The first is called modify-employment.php
<?php
$title = 'Modify Employment Opportunities';
$page_description = "We offer a complete dock service, rip rap installations, barge service, and custom built breakwaters.";
$thisPage="employment";
include_once($_SERVER['DOCUMENT_ROOT']."/admin/adminheader.php");
include($_SERVER['DOCUMENT_ROOT'] . "/connections/dbconnect.php");
/* be safe, not sorry */
foreach ($_REQUEST as $k => $v) {
$_REQUEST[$k] = mysql_real_escape_string($v);
}
/* take cat from url if exists */
$category = #$_REQUEST["category"] ? $_REQUEST["category"] : null;
$images = mysql_query(
$category ?
sprintf(
"SELECT * FROM hire WHERE data_type = '%s'",
$category
) :
"SELECT * FROM hire"
);
if ($images) {
$total = mysql_num_rows($images);
if ($total) {
$per = 4;
$page = #$_REQUEST["page"] ? $_REQUEST["page"] : 1;
$pages = ceil($total/$per);
}
mysql_free_result($images);
}
?>
misc code...
<section class="grid_8">
<ul class="clearfix">
<?php
if ($category) {
$images = mysql_query(sprintf(
"SELECT * FROM hire WHERE data_type = '%s' ORDER BY id DESC LIMIT %d, %d",
$category, ($page - 1) * $per, $per
));
} else $images = mysql_query(sprintf(
"SELECT * FROM hire ORDER BY id DESC LIMIT %d, %d",
($page - 1) * $per, $per
));
while ($image=mysql_fetch_array($images))
{
?>
<li data-id="id-<?=$image["id"] ?>">
<article class="box white-bg">
<h2 class="red3-tx"><?=$image["title"] ?> <span class="date-posted blue2-tx"><?=$image["date"] ?></span></h2>
<div class="dotline"></div>
<p class="blue3-tx"><?=$image["description"] ?><br />
<br />
For more information please call ###-###-####.</p>
<h4 class="btn-green">Delete</h4>
<h4 class="btn-green">Update</h4>
</article>
</li>
<?php
}
?>
Here is a screenshot of what the above looks like:
So by using this line of code:
<h4 class="btn-green">Update</h4>
I click the update button and it locates the posts id number.
By clicking the update button it takes me to file #2 "update-emploment.php"
I want this page to bring in the data from the original job post so I can edit and save the info.
This is where I'm getting lost.
Here is my code for update-employment.php
<?php
$title = 'Admin Employment Opportunities';
$page_description = "Add description in here.";
$thisPage="employment";
include_once($_SERVER['DOCUMENT_ROOT']."/admin/adminheader.php");
include($_SERVER['DOCUMENT_ROOT'] . "/connections/dbconnect.php");
/* be safe, not sorry */
foreach ($_REQUEST as $k => $v) {
$_REQUEST[$k] = mysql_real_escape_string($v);
}
/* take cat from url if exists */
$category = #$_REQUEST["category"] ? $_REQUEST["category"] : null;
$images = mysql_query(
$category ?
sprintf(
"SELECT * FROM hire WHERE data_type = '%s'",
$category
) :
"SELECT * FROM hire"
);
if ($images) {
$total = mysql_num_rows($images);
if ($total) {
$per = 4;
$page = #$_REQUEST["page"] ? $_REQUEST["page"] : 1;
$pages = ceil($total/$per);
}
mysql_free_result($images);
}
?>
misc code....
<form method="post" action="update-hire.php">
<input type="hidden" name="ud_id" style="width: 100%" value="<? echo "$id"; ?>" />
<div class="grid_6 botspacer60 ">
Position Title: <input type="text" name="ud_title" value="<?php echo "$title"; ?>"/>
<br /><br />
Date Posted: <input type="text" name="ud_date" value="<? echo "$date"; ?>"/>
<br /><br />
Position Details:<br />
<textarea name="ud_description" rows="8"><?php echo str_replace("<br />",chr(13),$des); ?><? echo "$description"; ?></textarea>
</div>
<div class="grid_12">
<input type="submit" value="Update" class="button orange" /> <div class="float-right"><input type="button" name="Cancel" value="Cancel" class="button orange" onclick="window.location = '/admin' " /></div>
</div></form>
The only part of the job post that gets echoed in is the title, not the title in my database, but the title of the page. See Image:
Then for the forms action I use file #3:
action="update-hire.php
<?php
include($_SERVER['DOCUMENT_ROOT'] . "/connections/dbconnect.php");
$ud_id = $_POST['ud_id'];
$ud_date = $_POST['ud_date'];
$ud_title = $_POST['ud_title'];
$ud_description = $_POST['ud_description'];
$ud_description = nl2br(htmlspecialchars($_POST['description']));
// Insert record into database by executing the following query:
$query="UPDATE hire SET title='$ud_title', description='$ud_description', date='$ud_date' "."WHERE id='$ud_id'";
$retval = mysql_query($sql);
echo "The position has been updated.<br />
<a href='modify-employment.php'>Update another position.</a><br />";
mysql_close();
?>
I'm very new to php and trying to learn on my own, but I spent a day trying to get this to work so I thought I would see If someone one Stack could help me out, as I've found some kind helpful people on here in the past. If someone could help I would appreciate it. Thanks!
In your update-employment.php, you need to define your variables. Add this somewhere in the file
$date = "";
$post_title = "";
$description = "";
$id = $_GET['id'];
$result = mysql_query("SELECT * FROM hire WHERE id=".$id."");
while($row = mysql_fetch_assoc($result)) {
$date = $row->date;
$post_title = $row->title;
$description = $row->description;
}
And then change this line to be like so:
Position Title: <input type="text" name="ud_title" value="<?php echo "$post_title"; ?>"/>
When visiting the page, add ?id=1 to select a post id. Replace 1 with any desired id.
I cannot take credit for this as a friend helped me figure this out, but I didn't want to leave this question unanswered. Hopefully it could help someone else out there.
My 1st file modify-employment.php
This was fine.
2nd file update-emploment.php needed some work.
I commented out the original code and removed much code from the top of the file.
<?php
$title = 'Admin Employment Opportunities';
$page_description = "Add description in here.";
$thisPage="employment";
include_once($_SERVER['DOCUMENT_ROOT']."/admin/adminheader.php");
include($_SERVER['DOCUMENT_ROOT'] . "/connections/dbconnect.php");
?>
Notice there was much code removed here.
misc code....
<div class="row">
<?php
$date = "";
$post_title = "";
$description = "";
$id = $_GET['value'];
/* $query = "SELECT * FROM hire WHERE id='$id'"; */
/* $result = mysql_query("SELECT * FROM hire WHERE id=".$id.""); */
$result = mysql_query("SELECT * FROM hire WHERE id='$id'");
$date = mysql_result($result,$i,"date");
$post_title = mysql_result($result,$i,"title");
$description = mysql_result($result,$i,"description");
/* while($row = mysql_fetch_assoc($result)) {
$date = $row->date;
$post_title = $row->title;
$description = $row->description;
} */
?>
<form method="post" action="update-hire.php">
<input type="hidden" name="ud_id" style="width: 100%" value="<? echo "$id"; ?>" />
<div class="grid_6 botspacer60 ">
Position Title: <input type="text" name="ud_title" value="<?php echo "$post_title"; ?>"/>
<br /><br />
Date Posted: <input type="text" name="ud_date" value="<? echo "$date"; ?>"/>
<br /><br />
Position Details:<br />
<textarea name="ud_description" rows="8"><?php echo str_replace("<br />",chr(13),$des); ?><? echo "$description"; ?></textarea>
</div>
<div class="grid_12">
<input type="submit" value="Update" class="button orange" /> <div class="float-right"><input type="button" name="Cancel" value="Cancel" class="button orange" onclick="window.location = '/admin' " /></div>
</div></form>
3rd file, action file: update-hire.php
I commented out the original code.
<?php
include($_SERVER['DOCUMENT_ROOT'] . "/connections/dbconnect.php");
$ud_id = $_POST['ud_id'];
$ud_date = $_POST['ud_date'];
$ud_title = $_POST['ud_title'];
$ud_description = $_POST['ud_description'];
/* $ud_description = nl2br(htmlspecialchars($_POST['description'])); */
// Insert record into database by executing the following query:
$query="UPDATE hire SET title='$ud_title', description='$ud_description', date='$ud_date' "."WHERE id='$ud_id'";
mysql_query($query);
/* $retval = mysql_query($sql); */
echo "The position has been updated.<br />
<a href='modify-employment.php'>Update another position.</a><br />";
mysql_close();
?>
That's it. Works like I wanted.
Related
I have a Wordpress page I'm working on with a search function that pulls from a database hosted on Amazon AWS.
When searching, I can pull the data just fine, but the results display below the search bar.
I'd like the results to display on its own separate page.
I've tried several suggestions using previously asked questions, though nothing worked.
Here is my code:
<?php
/* Template Name: Database */
?>
<?php
global $query_string;
$query_args = explode("&", $query_string);
$search_query = array();
if( strlen($query_string) > 0 ) {
foreach($query_args as $key => $string) {
$query_split = explode("=", $string);
$search_query[$query_split[0]] = urldecode($query_split[1]);
} // foreach
} //if
$search = new WP_Query($search_query);
?>
<?php get_header(); ?>
<?php get_footer(); ?>
<?php
$db_hostname = 'xxxxxxxxxxxxxxx';
$db_username = 'xxxxxxxxxxxxxxx';
$db_password = 'xxxxxxxxxxxxxxx';
$db_database = 'xxxxxxxxxxxxxxx';
$con = mysql_connect($db_hostname,$db_username,$db_password);
if (!$con){
die('404 Could not connect to server: ' . mysql_error());
}
mysql_select_db($db_database, $con);
global $sf_options;
$sidebar_config = $sf_options['archive_sidebar_config'];
$left_sidebar = $sf_options['archive_sidebar_left'];
$right_sidebar = $sf_options['archive_sidebar_right'];
$blog_type = $sf_options['archive_display_type'];
if ( $blog_type == "masonry" || $blog_type == "masonry-fw" ) {
global $sf_include_imagesLoaded;
$sf_include_imagesLoaded = true;
}
?>
<div class="container-fluid">
<center>
<form role="" method="get" >
<p style="color: #fff;">Search products and services for your business. We make it easier<br />to manage your business operations and make informed purchasing decisions.</p>
<input type="text" name="term" placeholder="Start typing to find a product or business"/>
<span><input type="submit" value="" class="btn btn-default" /></span>
</form>
</center>
</div>
<?php
if (!empty($_REQUEST['term'])) {
$term = mysql_real_escape_string($_REQUEST['term']);
$sql = "SELECT * FROM wpfr_listing WHERE Description LIKE '%".$term."%'";
$r_query = mysql_query($sql);
while ($row = mysql_fetch_array($r_query)){
echo '<br /> Company: ' .$row['title'];
echo '<br /> Certs: '.$row['certs'];
echo '<br /> Certifier: '.$row['certifier'];
echo '<br /> Address: '.$row['address'];
echo '<br /> Country: '.$row['country'];
echo '<br /> State: '.$row['state'];
echo '<br /> City: '.$row['city'];
}
}
?>
Any help provided is much appreciated.
Write on this page
<?php if (!empty($_REQUEST['term'])) {
header("location: page_url?term=".$_REQUEST['term']);
}
?>
And on serach result page
<?php
$term = mysql_real_escape_string($_GET['term']);
$sql = "SELECT * FROM wpfr_listing WHERE Description LIKE '%".$term."%'";
$r_query = mysql_query($sql);
while ($row = mysql_fetch_array($r_query)){
echo '<br /> Company: ' .$row['title'];
echo '<br /> Certs: '.$row['certs'];
echo '<br /> Certifier: '.$row['certifier'];
echo '<br /> Address: '.$row['address'];
echo '<br /> Country: '.$row['country']; echo '<br /> State: '.$row['state'];
echo '<br /> City: '.$row['city'];
}
?>
Since, you want this done, via PHP, here you go.
1)create a new page.
2)Have it handle a GET data of the query you have used here.
3) Print your search results as you have done in this page.
Now, in this page, you need to do a header redirect to that page The syntax for a header redirect is like this.
header('Location: http://www.example.com/?sql = <Your Query above>');
This should do it for you. Hope this helps. Feel free to comment, if you need any further info.
i have this Interface where the user can add a flower
The user need to add a flower to the database and he can make them in different occasions a categories (the check boxes). Now to implode the data i made this code:
$occasions = implode( ';' , $_POST['reg_occassions'] );
$categories= implode( ';' , $_POST['reg_categories'] );
$query =
"INSERT INTO tbl_flower (flower_type, website_description,florist_description,name,image,enabled,florist_choice,categories,occasions)
VALUES ('$flowertype', '$websitedescription', '$floristdescription','$name', '$upfile', '$enabled','$floristchoice', '$categories','$occasions')";
In the database they save as follows :
id flower name occasions categories
1 Rose Birthday;Valentines Bouqet;flower arrangment
Now the user can edit a flower too.This is the interface
PROBLEM!:
i dont have an idea how can i make the checkboxes again and show him which one he checked and he can change the data. I need to know how can i show the checkboxes and which one he checked will be checked too .
Thank and I really appreaciate if someone can help me.
EDIT Answer: This is the interface of the checkboxes.
You can do something like:
$results = "Bouqet,flower arrangment"; //results from your database
$resultsArray = explode(",", $results); //split the comma
$checkValue = array("Bouqet", "flower arrangment", "other"); //your checkbox values
$html = "";
foreach($checkValue as $val)
{
if(in_array($val,$resultsArray ))
$html .= '<input type="checkbox" name="flower" value="'.$val.'" checked>'.$val.'<br>';
else
$html .= '<input type="checkbox" name="flower" value="'.$val.'">'.$val.'<br>';
}
echo $html;
Check Demo Here
EDIT: I am making this edit because of your comments, you need to do something like:(not tested)
<div class="table-responsive col-md-4">
<table>
<thead>Occasions</thead>
/**flower occassions **/
$flowerCategoriesQry "SELECT categories FROM tbl_flower where id = 1"; //you need to change the where clause to variable
$flowerResults = mysqli_query($conn, $flowerCategoriesQry)
$categoriesRow = $flowerResults->fetch_assoc();
$resultsArray = explode(",", $categoriesRow['categories']);
/**All Occassions **/
$query544="SELECT name FROM tbl_occasions";
$results544 = mysqli_query($conn, $query544);
while ($row = $results544->fetch_assoc()) { ?>
<div class="col-md-12">
<label class="checkbox" for="checkboxes">
<?php
if(in_array($row['name'],$resultsArray ))
{
?>
<input type="checkbox" name="flower" value="<?php echo $row['name'];?>" checked> <?php echo $row['name'];?> >
<?php }
else{
?>
<input type="checkbox" name="flower" value="<?php echo $row['name'];?>" ><?php echo $row['name'];?> >
<?php echo} $row['name']?> </label>
</div> <?php }?>
</table>
This is the answer. Thanks to the Flash!
session_start();
$conn = ConnectToSql();
?>
<div class="table-responsive col-md-6" style="border:1px solid blue">
<div class="col-md-6">Categories</div>
<div class="col-md-6">Occasions</div>
<hr>
<div class="col-md-6">
<?php
/**flower occassions **/
$flowerCategoriesQry= "SELECT categories FROM tbl_flower where id ='$_SESSION[flower]'";
$flowerResults = mysqli_query($conn, $flowerCategoriesQry) ;
$categoriesRow = $flowerResults->fetch_assoc();
$resultsArray = explode(";", $categoriesRow['categories']);
/**All Occassions **/
$query544="SELECT name FROM tbl_categories";
$results544 = mysqli_query($conn, $query544);
while ($row = $results544->fetch_assoc()) { ?>
<label class="checkbox" for="checkboxes">
<?php
if(in_array($row['name'],$resultsArray ))
{
?>
<input type="checkbox" name="edit_categories[]" value="<?php echo $row['name'];?>" checked> <?php echo $row['name'];?>
<?php
}
else{
?>
<input type="checkbox" name="edit_categories[]" value="<?php echo $row['name'];?>" ><?php echo $row['name']; } ?>
</label>
<?php
}
?>
</div>
click here for code
I am trying to update or edit the data from the database but I don't want the edit page to be in another page, I just want it to be in the same page where I can view the different news that the user has added. But the form won't show up when I click the edit link .
Help me find what's wrong or missing?
here's my edit_news.php
<?php
date_default_timezone_set('Asia/Manila');
include_once('db.php');
if($isset($_GET['id']))
{
$id=$_GET['id'];
if(isset($_POST['edit'])) {
$title = $_POST['title'];
$body = $_POST['body'];
$date = date('Y-m-d H:i:s');
$title = mysql_real_escape_string($title);
$body = mysql_real_escape_string($body);
$servername = "localhost";
$username="root";
$password = "";
$database = "zchs_alumni";
$connection = new mysqli($servername, $username, $password, $database);
if ($connection->connect_error) {
die("Connection failed: " . $connection->connect_error);
}
$sql = ("UPDATE news SET title = '$title', body = '$body', name = '$name', date = '$date' WHERE id='$id'")or die();
mysql_query($sql);
echo "<script type='text/javascript'>alert('Changes saved!'); window.location.assign('/zchs-alumni/news.php');</script>";
}
}?>
<?php
if($isset($_GET['id']))
{
$id=$_GET['id'];
$query=mysql_query("SELECT * FROM news WHERE id='$id'");
while($row = mysql_fetch_array($query)) {
$title=$row['title'];
$body=$row['body'];
?>
<form action="" method="post">
<p>
<label for="title" id="title">Title</label>
<input type="text" name="title" value="<?php echo $row['title']; ?>"/>
</p><br/>
<p>
<label for="body" id="body">Body</label>
<input type="text" name="body" value="<?php echo $row['body']; ?>"/>
</p><br/>
<p>
<input type="submit" name="update" value="Save Changes" style="float: right"/>
</p>
</form>
<?php
} }?>
And this is my news.php where the news show up and where I want the editing of the data to take place.
<?php
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("zchs_alumni") or die(mysql_error());
$query = mysql_query("SELECT * FROM news ORDER BY date DESC LIMIT $start, $limit");
while($row = mysql_fetch_array($query)) {
?>
<p> <span><h3><?php echo $row['title']; ?></h3></span></p>
<p> <span><?php
$img = $row['photo'];
if($img != ""){
$image = 'news/'.$img;
echo '<center><img src="'.$image.'" width="750" height="350" alt=""></center>';
}
?></span></p>
<br/>
<p> <span><?php echo $row['body']; ?></span></p>
<br/>
<p> <span><h6>Posted at
<?php
$row_date = strtotime($row['date']);
echo date("F j, Y, g:i a", $row_date);
?></h6></span></p>
<br/>
<p><span><span class="edit" title="Edit">EDIT</span></p>
<?php
}
?>
I have seen these guys do that. Simple check if the post variable is defined if(isset($_POST)) and if so then it is an update submission, if not then display the form!
I cannot foreach loop my comment & reply system. I read many article and example but can't apply these in my case.
This is my comment system with reply, here every think is working well. But reply form work for only last/new comment. So i need to loop each comment. But can't do, please help me.
global $dbh;
$results = mysqli_query($dbh,"SELECT * FROM comments_lite WHERE qazi_id='$tutid' ORDER BY id DESC LIMIT 5") or die(mysqli_error($dbh));
echo'<div class="comments"><div id="updates"><div class="content"><comment>';
// Show only main comment
while($row = mysqli_fetch_array($results))
{ $id = $row['id'];
$qazi_id = $row['qazi_id'];
$username = $row['username'];
$description = $row['description'];
$parent_id = $row['parent_id'];
$date = $row['date'];
//echo comment
echo'<div class="comment">
<div class="cdomment_cheder">';
echo'<p>'.$username.' Says:</p>';
echo'<span>'.$date.'</span><br/>
<div class="avatarcnt">
<img alt="" src="uploadprofile/'.$u_imgurl.'"/>
</div></div></div>
<div class="cdomment_text">';
if ($description=="") {echo '';}
else echo''.htmlentities($description).'<br>';
echo '</div>';
//reply
echo'<div class="reply_box">Reply</div>';
//Reply form
echo'<div id="loader"></div><div class="reply_here" id="reply_here-'.$id.'" style="display:none;">
<form action="" method="post" id="repfrm" enctype="multipart/form-data">
<fieldset id="cmntfs">
<input type="hidden" name="username" tabindex="1" id="author" value="'.$username.'"/>
<textarea name="replycom" rows="2" tabindex="4" id="replycom" value=""></textarea>
<input type="hidden" name="parent_id" id="parent_id" value="0" />
<input type="hidden" name="tutid2" id="tutid" value="'.$tutid2.'" />
<button type="submit" name="submit" value="" tabindex="5" id="submit" class="repfrm">Post Reply</button>
</fieldset>
</form>
</div>';
// Show Reply of each comment
$query = "SELECT * FROM comments_reply WHERE parent_id ='".$id."'";
$res = mysqli_query($dbh,$query);
while($row = mysqli_fetch_array($res))
{ $id = $row['id'];
$qazi_id = $row['qazi_id'];
$username = $row['username'];
$description = $row['description'];
$parent_id = $row['parent_id'];
$date = $row['date'];
//echo reply
echo' <div class="rcontent"><replycomment><ul>
<div class="comment">
<div class="cdomment_cheder">';
echo'<p class="name">'.$username.' Says:</p>';
echo'<span>'.$date.'</span><br/>
<div class="avatarcnt">
<img alt="" src="uploadprofile/'.$u_imgurl.'"/>
</div></div></div>
<div class="cdomment_text">';
if ($description=="") {echo '';}
else echo''.htmlentities($description).'<br>';
echo '</div>';
echo'</ul><replycomment></div>';
} //reply while close
} //comment while close
echo'</div><comment></div>';
I'm trying to get my post to update just in case I make a mistake the first time around posting an article to my website.
Not sure what I'm doing wrong here.
Here is my update code:
<div class="row">
<?php
$post_title = "";
$description = "";
$id = $_GET['id'];
$result = mysql_query("SELECT title, description FROM htp_news WHERE id='$id'");
$post_title = mysql_result($result,0,"title");
$description = mysql_result($result,0,"description");
?>
<div class="row">
<form method="post" action="update-news.php">
<input type="hidden" name="ud_id" style="width: 100%" value="<? echo "$id"; ?>">
<div class="grid_12 botspacer60">
Title: <input type="text" name="ud_title" value="<?php echo "$post_title"; ?>">
<br /><br />
News Details:<br />
<textarea id="tiny_mce" name="ud_description" rows="8"><?php echo "$description"; ?></textarea>
</div>
<div class="grid_12">
<input type="submit" value="Update">
<input type="button" value="Cancel" onclick="window.location = '/admin'">
</div>
</form>
</div>
</div>
And here is my action page:
<?php
include($_SERVER['DOCUMENT_ROOT'] . "/includes/database.php");
$ud_id = $_POST['ud_id'];
$ud_title = $_POST['ud_title'];
$ud_description = $_POST['ud_description'];
// Insert record into database by executing the following query:
$query="UPDATE htp_news SET title='$ud_title', description='$ud_description' "."WHERE id='$ud_id'";
mysql_query($query);
echo "The post has been updated.<br />
<a href='edit-delete-news.php'>Update another position.</a><br />";
mysql_close();
?>
I appreciate any guidance on the matter.
Add a space before of WHERE Clause in query.
Use below -
$query="UPDATE htp_news SET title='$ud_title', description='$ud_description' WHERE id='$ud_id'";
Try this you need quotes in query
$result = mysql_query("SELECT `title`, `description` FROM `htp_news` WHERE id='$id'");
$query="UPDATE htp_news SET `title`='".$ud_title."', `description`='".$ud_description."' "." WHERE `id`='".$ud_id."'";