繁体   English   中英

使用php mysql更新帖子

[英]update post using php mysql

我在我的客户网站上创建了一个位置,以便他们为业务发布职位空缺。 我想给他们提供编辑工作岗位的选项,如果他们发布后犯了错误。

我的问题是如何实现这一目标。 或者更好的是我如何从数据库中提取信息以便我可以编辑/保存它?

这是我尝试过的:

在这里开始的是我的数据库的屏幕截图。

phpMyAdmin数据库该表名为“hire”,没有引号。 在此输入图像描述

我正在使用3个文件/页面来尝试更新。 第一个叫做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
}
?>

以下是上述内容的屏幕截图: 在此输入图像描述

所以通过使用这行代码:

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

我单击更新按钮,它会找到帖子ID号。 通过单击更新按钮,我需要提交#2 “update-emploment.php”我希望此页面从原始作业发布数据,以便我可以编辑和保存信息。 这是我迷路的地方。 这是我的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>

得到回应的职位的唯一部分是标题,而不是我数据库中的标题,而是页面的标题。 见图:

在此输入图像描述

然后对于表单操作,我使用文件#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();
?>

我是非常新的PHP,并试图自己学习,但我花了一天时间试图让这个工作,所以我想我会看到如果有人一个堆栈可以帮助我,因为我找到了一些善意的人在这里过去。 如果有人可以提供帮助,我会很感激。 谢谢!

在update-employment.php中,您需要定义变量。 在文件中的某处添加它

$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;

}

然后将此行更改为:

Position Title: <input type="text" name="ud_title" value="<?php echo "$post_title"; ?>"/>

访问该页面时,添加?id = 1以选择帖子ID。 将1替换为任何所需的ID。

我不能因为朋友帮助我解决这个问题而对此表示赞赏,但我不想让这个问题得不到回答。 希望它可以帮助其他人。

我的第一个文件是modify-employment.php

这很好。


第二个文件update-emploment.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");
?>

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>

第3个文件,动作文件: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'";

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();
?>

而已。 像我想的那样工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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