簡體   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