在php中定义数据时,更新数据不起作用,未定义变量错误

时间:2015-03-29 16:36:09

标签: php mysql

我创建了两个文件。一个名为index.php,另一个名为edit.php。我已将index.php包含在edit.php中。 edit.php用于编辑帖子。帖子编辑很好,但问题是,每当我尝试更新它时都会给出我的错误。“注意,第10行的未定义变量编辑”这里是行$edit_id = $_GET['edit'];但是我在index.php {{1}中声明了我还声明了一个查询来更新数据。但每当我试图更新数据。它不会更新其他删除。 这是我的index.php文件。任何帮助,将不胜感激。谢谢和问候,

<td><a href="edit.php?edit=<?php echo $id; ?>">Edit</a></td>

edit.php代码在这里:

<!DOCTYPE html>

    <?php
    session_start();
    if(!isset($_SESSION['user_name'])) {
    header("location: login.php");
    } else {
            ?>

    <html>
    <head>
    <link rel="stylesheet" href="admin_style.css"> 
    <title>Admin Panel</title>
    </head>
    <body>
    <header>
    <h1><a href="index.php"> Welcome to Admin Panel</a> </h1>
    </header>
    <h3 align="center">This is Admin Area</h3>
    <aside>
    <h3>Welcome <?php echo $_SESSION['user_name']; ?></h3>
    <h2>Manage Content</h2>
    <p><a href="index.php?view=view">View Posts</a></p>
    <p><a href="index.php?insert=insert">Insert Posts</a></p>
    <p><a href="logout.php">Logout</a></p>
    </aside>

    <?php
    if(isset($_GET['insert'])){
    include("Post.php");
    }
    ?>
    <?php if(isset($_GET['view'])) { ?>
    <table width="1000" align="center" border="1">
    <tr>
    <td align="center" colspan="9"><h1>View all Posts</h1></td>
    </tr>
    <tr align="center">
    <th>Post No</th>
    <th>Post Title</th>
    <th>Post Date</th>
    <th>Post Author</th>
    <th>Post Image</th>
    <th>Post Content</th>
    <th>Edit</th>
    <th>Delete</th>
    </tr>
    <?php
    include("connect.php");
    if(isset($_GET['view'])) {
    $query = "SELECT * FROM posts order by 1 DESC";
    $run = mysqli_query($con, $query);
    $i=1;
    while($row=mysqli_fetch_array($run)) {
    $id = $row['Post_id'];
    $title = $row['Post_title'];
    $date = $row['Post_date'];
    $author = $row['Post_author'];
    $image = $row['Post_image'];
    $content = substr($row['Post_content'],0,50);
    ?>

    <tr align="center">
    <td><?php echo $i++; ?></td>
    <td><?php echo $title; ?></td>
    <td><?php echo $date; ?></td>
    <td><?php echo $author; ?></td>
    <td><img src="../images/<?php echo $image; ?>" width="50" height="50" /> </td>
    <td><?php echo $content; ?></td>
    <td><a href="edit.php?edit=<?php echo $id; ?>">Edit</a></td>
    <td><a href="delete.php?del=<?php echo $id; ?>">Delete</a></td>
    </tr>
    <?php
    } 
    } 
    }
    ?>
    </table>
    </body>
    </html>     
    <?php } ?>

1 个答案:

答案 0 :(得分:0)

问题出在你的表格上。提交此表单时,您的上述$edit_id = $_GET['edit'];未定义。

您应该将其更新为

if(!empty($_GET['edit'])) {
    //next trust your users; http://php.net/manual/en/mysqli.real-escape-string.php
    $edit_id = mysqli_real_escape_string($con, $_GET['edit']);
    $query = "SELECT * FROM posts where Post_id = '$edit_id'";
    $run = mysqli_query($con, $query);
    while($row=mysqli_fetch_array($run)) {
        $edit_id1 = $row['Post_id'];
        $title = $row['Post_title'];
        $date = $row['Post_date'];
        $author = $row['Post_author'];
        $image = $row['Post_image'];
        $content = $row['Post_content'];
}

然后在您的表单中,您的名称属性是标题,但在PHP中,您将它们缩小了。您需要选择一个命名约定并坚持使用它。所以要么......

<form method="post" action="edit.php?edit_form=<?php echo $edit_id1;?>" enctype="multipart/form-data">
    Post Title: &nbsp &nbsp <input type="text" name="title" size="50" value="<?php echo $title; ?>" required /> <br /> 
    Post Author: <input type="text" name="author" size="50" value="<?php echo $author; ?>" required /> <br />
    Post Image: <input type="file" name="image" /><img src="../images/<?php echo $image; ?>" width="60" height="60"/> <br /> 
    Post Content: <textarea name="content" cols="70" rows="20" >
        <?php echo $content; ?>
    </textarea> <br />
    <input type="submit" name="update" value="update" /> <br />         
</form>

...或....

$update_id = $_GET['edit_form'];
            $post_title = $_POST['Title'];
            $post_date = date('y-m-d');
            $post_author = $_POST['Author'];
            $post_content = $_POST['Content'];
            $post_image = $_FILES['Image']['name'];
            $post_image_type = $_FILES['Image']['type'];
            $post_image_size = $_FILES['Image']['size'];
            $post_image_tmp = $_FILES['Image']['tmp_name'];