在PHP中查看/编辑/删除

时间:2011-10-18 21:18:52

标签: php session view edit

我有一个系统,用户必须注册并登录我的网站才能添加非注册用户和明显注册用户可以从前端查看的配方。 到目前为止我所做的是,我已经完成了注册页面,登录页面和“我的帐户”页面,供用户登录和提交食谱。一切正常但现在我正在尝试在我的系统中添加另一个功能,用户可以编辑/删除他们自己的食谱。我完成登录的方式是创建一个包含用户名的会话,然后将其输出到网址中,如下所示:www.cooking.com/my-account.php?user_id = 26.

我想要同样的东西,但这次我希望将食谱存储在会话中,而不是在URL上显示食谱ID。我对如何做到这一点毫无头绪。我在mysql中有一个'starters'表,其中包含以下字段:

username ()
recipename
ingredients
method
time
id

一旦您登录并想要编辑/删除您上传的食谱,就会显示一个包含您上传的所有食谱的表格。我想要的是让用户点击任何食谱,它会将用户带到另一个页面,允许用户编辑他们的东西。

我试过这个但没有成功。以下是我在编辑后点击错误时使用的代码:

EDIT STARTERS PAGE(editstarters.php)

<?php
session_start();
require_once '../database.php';
if (isset($_SESSION['myusername'])){
echo "Welcome ". $_SESSION['myusername'];
}
?>
<br /><br />You have uploaded the following starters:
<br /><BR />
<?php
include '../database.php';

$userid = $_SESSION["myusername"];

$result = mysql_query("SELECT * FROM starters WHERE username = '". $_SESSION['myusername']."' ");

echo "<table border='1'><table border width=65%><tr><th>Recipie Name</th><th>Ingredients</th><th>Method</th><th>Time</th></tr>";
while($getrecipie = mysql_fetch_array($result))
{
        echo "<tr>";
        echo "<td>" . $recipiename = $getrecipie['recipename']. "</td>";
        echo "<td>" . $ingredients = $getrecipie['ingredients']. "</td>";
        echo "<td>" . $method = $getrecipie['method']. "</td>";
        echo "<td>" . $time = $getrecipie['time']. 'minutes'."</td>";

        ?>
      <td><a href = "startersedited.php?rec=<?php echo $getrecipie['id'] ?>" >Edit</a></td>
      <td><a href = "DELETE1.php?rec=<?php echo $getrecipie['Recipie_ID'] ?>&id=<?php echo $user_id?>" >Delete</a></td>
      <!--using the stu_id value in the URL to select the correct data when wego to the relevant pages -->
      <?php
}
echo "</tr>";
echo "</table>";
?>

STARTERS编辑页面(startersedited.php)

<?php
session_start();
require_once '../database.php';
if (isset($_SESSION['myusername'])){
echo "Welcome ". $_SESSION['myusername'];
}
?>
<br /><br />EDIT/DELETE YOUR STARTERS
<br /><BR />
<?php
include '../database.php';

$userid = $_SESSION["myusername"];

$result = mysql_query("SELECT * FROM starters WHERE username = '". $_SESSION['myusername']."' AND recipie_id='{$_GET['rec']}'");

$getrecipie = mysql_fetch_array($result);
$recipie = $getrecipie['recipename'];
$ingredients = $getrecipie['ingredients'];
$method = $getrecipie['method'];
$time = $getrecipie['time'];
?>
 <h1>Edit Recipies</h1>
      <p>&nbsp;</p>
      <form name="form1" method="post" action="startereditsuccess.php?rec=<?php echo $_GET['id']?>">
        <table width="609" height="250" border="0">
          <tr> 
            <td width="155">Recipie Name</td>
            <td width="347"><label for="recipiename"></label> <input type="text" name="recipename"  value="<? echo $recipe ?>" id="recipename" > 
            </td>
          </tr>
          <tr> 
            <td>Ingredients</td>
            <td><label for="ingredients"></label> <textarea name="ingredients" cols="50" rows="5" id="ingredients"><? echo $ingredients ?></textarea></td>
          </tr>
          <tr> 
            <td>Method</td>
            <td><label for="method"></label> <textarea name="method" cols="50" rows="5" id="method"><? echo $method ?></textarea></td>
          </tr>
          <tr> 
            <td>Time</td>
            <td><label for="time"></label> <input type="text" name="time" value="<? echo $time ?>" id="time"></td>
          </tr>
        </table>
        <p> 
          <input type="submit" name="update" id="update" value="Update">
        </p>
      </form>

这是我得到的错误:

  

警告:mysql_fetch_array()要求参数1为资源,布尔值在第55行的/home/jahedhus/public_html/cook/editdelete/startersedited.php中给出

请帮助我,我很难过!

2 个答案:

答案 0 :(得分:2)

首先,请不要大喊你的帖子。没有必要。

其次,当实际的唯一相关位是您的错误消息时,我们不需要显示所有内容的代码墙。该特定错误消息意味着您的查询失败(可能是由于语法错误),这意味着mysql_query()已返回其通常的布尔值FALSE,并且您没有检查该错误。您使用此false作为语句句柄并尝试从中获取一行,这导致了实际的错误消息。

作为一般规则,绝不假设数据库查询成功。即使查询字符串本身在语法上100%有效,也有许多其他原因导致它失败。

您的基本MySQL查询代码结构应为:

$sql = "...";
$result = mysql_query($sql) or die(mysql_error());

这适用于调试/开发:如果查询失败,它将立即停止脚本并告诉您原因。对于生产代码,您需要更强大的功能,而不是向用户发送长SQL错误消息。

答案 1 :(得分:0)

您在此行的startersedited.php中致电mysql_query()

$result = mysql_query("SELECT * FROM starters WHERE username = '". $_SESSION['myusername']."' AND recipie_id='{$_GET['rec']}'");

返回布尔值FALSE,因为发生了错误。每当您致电mysql_query()时,都应添加一些错误处理代码来处理此问题,例如:

$result = mysql_query("SELECT * FROM starters WHERE username = '". $_SESSION['myusername']."' AND recipie_id='{$_GET['rec']}'");
if($result === FALSE) {
    echo "Database Error: ".mysql_error() ;
    exit ;
}
$getrecipie = mysql_fetch_array($result);

以上内容对于开发错误检查可能更有用,在生产站点中,您可能希望捕获错误并显示更优雅的内容。

此外,我注意到您正在呼叫require_once '../database.php';include '../database.php';。你不需要两者,只是第一个会这样做。