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.
Related
So I am wanting the user to be able to search by either keyword or ID number. If they search "test" right now for example it will pull all the entries with test which is what I want it to do for the keyword part of the search. However, I also want the user to be able to search my specific a specific ID# and just pulling that specific entry. I am unsure how I would go about doing this. I tried doing some sort of OR statement but it did not pull any entries.
Search box form
<div class ="search" id="browse">
<p> Find your appointment below or search by keyword</p>
<form id="" class="searchbar" action="searchAppt.php" method="get">
<input type="text" name="terms" size="40" class = "sbar" placeholder="Search by issue keyword or ID" oninput="validity.valid||(value='');"
onblur="if (this.value == '') {
this.value = 'Enter keyword or ID';
}"
onfocus="if (this.value == 'Enter keyword or ID') {
this.value = '';
}"/>
<button type="submit" class = "btn">Search</button>
</form>
</div>
searchAppt.php
if (filter_has_var(INPUT_GET, "terms")) {
$terms_str = filter_input(INPUT_GET, 'terms', FILTER_SANITIZE_STRING);
} else {
echo "There were no appointments found.";
include ('includes/footer.php');
exit;
}
//explode the search terms into an array
$terms = explode(" ", $terms_str);
$sql = "SELECT * FROM appointments WHERE 1";
foreach ($terms as $term) {
$sql .= " AND email = '". $_SESSION['email'] ."' AND issue LIKE '%$term%' OR id ='%term%'
";
}
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<br /><br /><center><h1>My Ticket(s)</h1><br />
<div class='table'>
<div class='tr'>
<div class='td'><b>Ticket #</b></div>
<div class='td'><b>Issue</b></div>
<div class='td'><b>Date</b></div>
<div class='td'><b>Ticket Details</b></div>
</div>";
// output data of each row
while($row = $result->fetch_assoc()) {
$starttimepast = strtotime($row["start_time"]); //converts date time received from MySQL into a string
$datepast = date("m/d/y", $starttimepast);
echo "<div class='tr'>
<div class='td'>".$row["id"]."</div>
<div class='td'>".$row["issue"]."</div>
<div class='td'>".$datepast."</div>
<div class='td'><form action='ticketdetails.php' method='post'>
<input type='hidden' name='id' value='".$row["id"]."'>
<input type='submit' value='Ticket Details'></form>
</div>
</div>";
}
echo "</div>";
echo "<br /><center><a href='myProfile.php'><h4>Go back to my profile</h4></a></center>";
include ('includes/footer.php');
} else {
echo "<br /> <br /><center><h3>Your search <i>'$terms_str'</i> did not match any appointments</h3></center>";
echo "<center><a href='myProfile.php'><h4>Go back to my profile</h4></a></center>";
echo "<br />";
exit;
}
?>
<?php
// clean up resultsets when we're done with them!
$query->close();
// close the connection.
$conn->close();
Perhaps it will help to explicitly group the terms:
$sql = "SELECT * FROM appointments WHERE email = '" . S_SESSION['email'] . "'";
$exprs = array();
foreach ($terms as $term) {
$exprs[] = "(issue LIKE '%$term%' OR id LIKE '%$term%')";
}
if (!empty($exprs)) {
$sql .= ' AND (' . join(' OR ', $exprs) . ')';
}
The result in this case will include records that matched any of the terms.
Note: It would be good to use a DB API like laravel/PDO/mysqli to simplify the query building and properly escape the values.
<?php include("config.php");
error_reporting(E_ALL); ini_set('display_errors', 1);
$results = mysqli_query($con,"SELECT COUNT(name) FROM user_tbl ");
$get_total_rows = mysqli_fetch_array($results); //total records
//break total records into pages
$pages = ceil($get_total_rows[0]/$item_per_page);
?>
<script type="text/javascript">
$(document).ready(function() {
$("#results").load("fetch_pages.php"); //initial page number to load
$(".pagination").bootpag({
total: <?php echo $pages; ?>,
page: 1,
maxVisible: 5
}).on("page", function(e, num){
e.preventDefault();
$("#results").prepend('<div class="loading-indication"><img src="ajax-loader.gif" /> Loading...</div>');
$("#results").load("fetch_pages.php", {'page':num});
});
});
</script>
<body>
<div id="serch">
<form method="post" enctype="multipart/form-data">
<label>SEARCH BY NAME:</label>
<input type="text" name="name" class="name" >
<input type="submit" name="submit" class="sbmtt" value="SEARCH" />
</form>
</div>
</table>
<div class="res">
<div class="navi">
<div id="results"></div>
<div class="pagination"></div>
</div>
</div>
</body>
fetch_pages.php:
<?php
include("config.php"); //include config file
if(isset($_POST["page"])) {
$page_number = filter_var($_POST["page"], FILTER_SANITIZE_NUMBER_INT, FILTER_FLAG_STRIP_HIGH);
if(!is_numeric($page_number)) {
//incase of invalid page number
die('Invalid page number!');
}
} else {
$page_number = 1;
}
//get current starting point of records
$position = (($page_number-1) * $item_per_page);
//Limit our results within a specified range.
$results = mysqli_query($con, "SELECT * FROM user_tbl WHERE name = 'aruna' ORDER BY id ASC LIMIT $position, $item_per_page");
//output results from database
echo '<ul class="page_result">';
while($row = mysqli_fetch_array($results)) {
echo 'Name: ' .$row['name'];
echo '<br /><br />Contact Number: ' .$row['cont'];
echo '<br /><br />Email ID: ' .$row['email'];
echo '<br /><br />View details';
echo "<br /><hr />";
}
echo '</ul>';
?>
Config.php:
<?php
$con = mysqli_connect("localhost","root","","mydb");
$item_per_page = 3;
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
Here the result just shows in the div.. i want to fetch the result after the form get submitted .. i tried if(isset($_POST['submit'])) but not working.. and the search should be done by fetching the name from the html form not the way i ve given in fetch_pages.php like 'aruna' .. it should get fetched by the form data like $_POST['name'] .. i m really stuck here.. any help plzzz .. thanks in advance
You need to pass search form value. Below is the full updated code.
<?php include("config.php");
error_reporting(E_ALL); ini_set('display_errors', 1);
$name = "";
$where = "";
if(isset($_POST["name"])){
$name = $_POST["name"];
$where = " WHERE name = '".$name."' ";
}
$results = mysqli_query($con,"SELECT COUNT(name) FROM user_tbl $where");
$get_total_rows = mysqli_fetch_array($results); //total records
//break total records into pages
$pages = ceil($get_total_rows[0]/$item_per_page);
?>
<script type="text/javascript">
$(document).ready(function() {
var name = '<?php echo $name; ?>';
$("#results").load("fetch_pages.php", {'name':name}); //initial page number to load
$(".pagination").bootpag({
total: <?php echo $pages; ?>,
page: 1,
maxVisible: 5
}).on("page", function(e, num){
e.preventDefault();
$("#results").prepend('<div class="loading-indication"><img src="ajax-loader.gif" /> Loading...</div>');
$("#results").load("fetch_pages.php", {'page':num, 'name':name});
});
});
</script>
<body>
<div id="serch">
<form method="post" enctype="multipart/form-data">
<label>SEARCH BY NAME:</label>
<input type="text" name="name" class="name" value="<?php echo $name; ?>">
<input type="submit" name="submit" class="sbmtt" value="SEARCH" />
</form>
</div>
</table><div class="res">
<div class="navi">
<div id="results"></div>
<div class="pagination"></div>
</div> </div>
</body>
fetch_pages.php:
<?php
include("config.php"); //include config file
if(isset($_POST["page"])){
$page_number = filter_var($_POST["page"], FILTER_SANITIZE_NUMBER_INT, FILTER_FLAG_STRIP_HIGH);
if(!is_numeric($page_number)){die('Invalid page number!');} //incase of invalid page number
}else{
$page_number = 1;
}
//get current starting point of records
$position = (($page_number-1) * $item_per_page);
$name = "";
$where = "";
if(isset($_POST["name"])){
$name = $_POST["name"];
$where = " WHERE name = '".$name."' ";
}
//Limit our results within a specified range.
$results = mysqli_query($con, "SELECT * FROM user_tbl $where ORDER BY id ASC LIMIT $position, $item_per_page");
//output results from database
echo '<ul class="page_result">';
while($row = mysqli_fetch_array($results))
{
echo 'Name: ' .$row['name'];
echo '<br /><br />Contact Number: ' .$row['cont'];
echo '<br /><br />Email ID: ' .$row['email'];
echo '<br /><br />View details';
echo "<br /><hr />";
}
echo '</ul>';
?>
Hope it will help.
Im trying to update a field in my database by adding to the original number value that is already in there.
i have a system where staff are able to log in and update a the balance of a normal user. Currently i have a test user and staff. the users balance is set to 100. i have the following code:
<?php
if(isset($_POST['search'])){
$searchq = $_POST['search'];
$searchq = preg_replace("#[^0-9a-z]#i","",$searchq);
$result = $mysqli->query( "SELECT * FROM Users WHERE Username ='$searchq'");
if ($result){
//fetch result set as object and output HTML
if($obj = $result->fetch_object())
{
echo '<div class="booksearched">';
echo '<form method="POST" id = "books" action="">';
echo '<div class="book-content"><h3>Student Username: '.$obj->Username.'</h3>';
echo '<br>';
echo '<div class="book-content"><i>First Name: <b>'.$obj->FirstName.'</b></i></div>';
echo '<div class="book-desc"><i>Last Name:<b> '.$obj->LastName.'</b></i></div>';
echo '<br>';
echo '<div class="book-qty"> Current Balance<b> '.$obj->Balance.'</b></div>';
echo 'New Balance: <input type="number" name="newBalance" value = "1" min = "1" />';
echo '<br><br>';
echo '<button name="submit_btn" class="save_order">Top Up</button>';
echo '</div>';
echo '</form>';
echo '</div>';
}
}
}
$newBalance="";
$newBalance = $_POST['newBalance'];
if(isset($_POST['submit_btn']) ){
$upsql = "UPDATE users SET Balance = Balance + '$newBalance' WHERE Username='" . $obj->Username . "'";
$stmt = $mysqli->prepare($upsql);
$stmt->execute();
}
?>
Ive tried a few things however i kept getting an error saying:
( ! ) Notice: Undefined index: newBalance
Im not sure what ive done wrong.
Any idea how to fix it?
Edit: Full code
<?php
session_start();
include_once("config.php");
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Search</title>
<link href="style/style.css" rel="stylesheet" type="text/css">
</head>
<body>
<br>
<div id="books-wrapper">
<!-- #content to center the menu -->
<div id="content">
<!-- This is the actual menu -->
<ul id="darkmenu">
<li>Home</li>
<li>New Books</li>
<li>Search</li>
<li>Update Balance</li>
</ul>
</div>
<div id = "welcome" >
Welcome, <?=$_SESSION['Username'];?>! <br> Logout
</div>
<br><br>
<h1 id = "mainHeader" >Update a Students Balance</h1>
<br>
<div id = "balanceupdate">
<form id = "adsearch" action="updateBalance.php" method="post">
<input type="text" name ="search" placeholder="Search For a Student">
<button name="submit" value="search">Search</button>
</form>
<br>
</div>
<?php
if(isset($_POST['search'])){
$searchq = $_POST['search'];
$searchq = preg_replace("#[^0-9a-z]#i","",$searchq);
$result = $mysqli->query( "SELECT * FROM Users WHERE Username ='$searchq'");
if ($result){
//fetch result set as object and output HTML
if($obj = $result->fetch_object())
{
echo '<div class="booksearched">';
echo '<form method="POST" id = "books" action="">';
echo '<div class="book-content"><h3>Student Username: '.$obj->Username.'</h3>';
echo '<br>';
echo '<div class="book-content"><i>First Name: <b>'.$obj->FirstName.'</b></i></div>';
echo '<div class="book-desc"><i>Last Name:<b> '.$obj->LastName.'</b></i></div>';
echo '<br>';
echo '<div class="book-qty"> Current Balance<b> '.$obj->Balance.'</b></div>';
echo 'New Balance: <input type="number" name="newBalance" value = "1" min = "1" />';
echo '<br><br>';
echo '<button name="submit_btn" class="save_order">Top Up</button>';
echo '</div>';
echo '</form>';
echo '</div>';
}
}
}
$newBalance="";
if(isset($_POST['submit_btn']) && !empty($_POST['newBalance']) ){
$newBalance = $_POST['newBalance'];
$upsql = "UPDATE users SET Balance = Balance + '$newBalance' WHERE Username='" . $obj->Username . "'";
$stmt = $mysqli->prepare($upsql);
$stmt->execute();
}
?>
</body>
</html>
It's throwing that notice because you need to place $newBalance = $_POST['newBalance']; inside if(isset($_POST['submit_btn'])){...} and verify that it is not empty (or set).
$newBalance="";
if(isset($_POST['submit_btn']) && !empty($_POST['newBalance']) ){
$newBalance = $_POST['newBalance'];
$upsql = "UPDATE users SET Balance = Balance + '$newBalance'
WHERE Username='" . $obj->Username . "'";
$stmt = $mysqli->prepare($upsql);
$stmt->execute();
}
You can also use isset($_POST['newBalance']) instead of !empty($_POST['newBalance'])
Sidenote: You may want to add a submit type for your button.
echo '<button type="submit" name="submit_btn" class="save_order">Top Up</button>';
Yet, it may not be required; do try it if you're still experiencing problems.
Edit:
Under
echo '<div class="book-content"><h3>Student Username: '.$obj->Username.'</h3>';
add
echo '<input type="hidden" name="username" value = "'.$obj->Username.'" />';
then under
$newBalance = $_POST['newBalance'];
add
$username = $_POST['username'];
and modify your query to read as
$upsql = "UPDATE users SET Balance = Balance + '$newBalance'
WHERE Username='".$username ."'";
My quoting may be a bit off for
echo '<input type="hidden" name="username" value = "'.$obj->Username.'" />';
where you may have to change it to
echo '<input type="hidden" name="username" value = '".$obj->Username."' />';
Edit #2:
Another way to do this since you're already using sessions <?=$_SESSION['Username'];?> would be to assign a variable to it and pass it in your query.
$username = $_SESSION['Username'];
$upsql = "UPDATE users SET Balance = Balance + '$newBalance'
WHERE Username='".$username ."'";
Edit #3:
Where you have
if(isset($_POST['search'])){
$searchq = $_POST['search'];
$searchq = preg_replace("#[^0-9a-z]#i","",$searchq);
replace it with
if(isset($_POST['search'])){
$searchq = $_POST['search'];
$searchq = preg_replace("#[^0-9a-z]#i","",$searchq);
$student = $_POST['search'];
$_SESSION['student'] = $student;
echo $_SESSION['student']; // see what echos here
then in your query, do:
$upsql = "UPDATE users SET Balance = Balance + '$newBalance'
WHERE Username='".$student ."'";
If that doesn't work, I don't know what else to do that will be of further help. My tests were conclusive and worked. Your query may be failing, I have no more ideas at this point.
Base yourself on this scenario:
$_POST['search'] = "student1";
$student = $_POST['search'];
$_SESSION['student'] = $student;
// echo $_SESSION['student'];
$student2 = $student;
echo $student2; // will echo student1
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.
I'm trying to integrate CKEditor into my simple CMS. I got it to show up on the page, but it's just above everything. I'm wondering how to get it into the correct spot, below my title textbox? Here is my code:
require_once 'conn.php';
include_once 'ckeditor/ckeditor.php';
$CKEditor = new CKEditor();
$CKEditor->editor('body');
$title= '';
$body= '';
$article= '';
$author_id= '';
if (isset($_GET['a'])
and $_GET['a'] == 'edit'
and isset($_GET['article'])
and $_GET['article']) {
$sql = "SELECT title, body, author_id FROM cms_articles " .
"WHERE article_id=" . $_GET['article'];
$result = mysql_query($sql, $conn) or
die ('Could not retrieve article data: ' . mysql_error());
$row = mysql_fetch_array($result);
$title = $row['title'];
$body = $row['body'];
$article = $_GET['article'];
$author_id = $row['author_id'];
}
require_once 'header.php';
?>
<form method="post" action="transact-article.php">
<h2>Compose Article</h2>
<p>
Title: <br />
<input type="text" class="title" name="title" maxlength="255" value="<?php echo htmlspecialchars($title); ?>" />
</p>
<p>
Body: <br />
<textarea class="body" name="body" id="body" rows="10" cols="60"><?php echo htmlspecialchars($body); ?></textarea>
</p>
<p>
<?php
echo '<input type="hidden" name="article" value="' .
$article . "\" />\n";
if ($_SESSION['access_lvl'] < 2) {
echo '<input type="hidden" name="author_id" value="' .
$author_id . "\" />\n";
}
if ($article) {
echo '<input type="submit" class="submit" name="action" ' .
"value=\"Save Changes\" />";
} else {
echo '<input type="submit" class="submit" name="action" ' .
"value=\"Submit New Article\" />";
}
?>
</p>
</form>
Personally I don't think you need the PHP library. Just add
<div contenteditable="true">
Editable text
</div>
as your editable and then just the script to get it running:
<script type="text/javascript" src="/path/to/ckeditor/ckeditor.js"></script>
That said, you may be able to pass the id of your textarea to the PHP library. To avoid confusion with the body tag, rename the id and name of this control to editable_content or similar. And as I mention above, try using a div instead.