2014-01-06 40 views
0

我在我的客户网站上创建一个位置,让他们发布业务空缺职位。如果他们发布后出错,我想给他们编辑这个职位的选项。更新后使用php mysql

我的问题是我该如何做到这一点。或者更好的是,我怎样才能从我的数据库中提取信息,以便我可以编辑/保存它?

这是我曾尝试:

为了让事情开始,这里是我的数据库的截图。

phpMyAdmin数据库该表被称为“聘用”没有引号。 enter image description here

我使用3个文件/页面,试图得到这个更新。 首先是所谓的修改 - 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 
} 
?> 

下面是截图什么上面的样子: enter image description here

因此,通过使用这行代码:

<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” 我希望此页面可以从原始作业帖子中引入数据,以便我可以编辑和保存信息。 这是我迷路的地方。 这里是我的更新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> 

是获取呼应了作业后的唯一部分是标题,没有称号,我的数据库,但页面的标题。见图片:

enter image description here

然后为形式的行动我使用的文件#3: 行动=“更新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和努力我自己学习,但我花了一天的时间试图让这个工作,所以我想我会看到如果有人一个堆栈可以帮助我,因为我在过去发现了一些有帮助的人在这里,如果有人可以帮助我将不胜感激。谢谢!

+0

当您尝试更新时收到的错误是什么? – Roger

回答

1

我不能赞扬这个,因为朋友帮我弄清楚了这一点,但我不想留下这个问题没有答案。希望它可以帮助别人。

我的第一次修改文件,employment.php

这是罚款。


第2个文件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文件,操作文件:更新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(); 
?> 

就是这样。像我想要的那样工作。

1

在你的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?用任何想要的ID替换1。

+0

我会在这里放什么?这里的张贴ID?我知道身份证,但我怎么会要求呢? – cwd

+0

已更新的答案。 –

+0

在我的修改工作页面上添加这个到我的更新按钮:

Update

' 这是正确的吗? 刚刚尝试过,它只是选择ID 1 ..? – cwd