简体   繁体   中英

update post using php mysql

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"><a href="http://<?php echo $_SERVER['SERVER_NAME']; ?>/admin/remove-job-post.php?value=<?=$image["id"] ?>">Delete</a></h4>

<h4 class="btn-green"><a href="http://<?php echo $_SERVER['SERVER_NAME']; ?>/admin/update-employment.php?value=<?=$image["id"] ?>">Update</a></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"><a href="http://<?php echo $_SERVER['SERVER_NAME']; ?>/admin/update-employment.php?value=<?=$image["id"] ?>">Update</a></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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM