我的编辑页面无效

时间:2014-02-06 21:39:35

标签: php mysql

我的编辑页面无效。我一直在 注意:未定义的变量。如果我确实将表单放在关闭的窗口中,那么当我单击更新按钮

时表单就会消失

注意:未定义的变量:第57行的C:\ wamp \ www \ Persuasion \ admin \ edit_posts.php中的post_title

0down voteaccept

我必须说实话,我对php很新。实际上,这段代码来自我关注的http://www.youtube.com/watch?v=oZ2KZRHASdQ教程。不幸的是,视频在前6分钟冻结,这就是我寻求帮助的原因

我做的是

<?php } } ?>

现在,当我编辑帖子

时,它似乎正常工作

但是现在我收到了这个错误

注意:未定义的索引:第96行的C:\ wamp \ www \ Persuasion \ admin \ edit_posts.php中的edit_form

我再次感谢所有帮助的人

 <html>
                <head>
                <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
                <link rel="stylesheet" href="admin_style.css" media="all"/>
                <title>Admin Panel</title>
                </head>

                <body>
                <div id="header">
                <a href="index.php">
                <h1>Welcome To Admin Panel</h1></div></a>

                </div>

                <div id id="sidebar">
                <h2><a href="#">Logout</a></h2>
                <h2><a href="view_posts.php">View Posts</a></h2>
                <h2><a href="insert_post.php">Insert New Posts</a></h2>
                <h2><a href="#">View Comments</a></h2>

                </div>
                <?php
                include("includes/connect.php");

                if (isset($_GET['edit'])) {

                    $edit_id = $_GET['edit'];

                    $edit_query = "select * from posts where post_id = '$edit_id'";

                    $run_edit = mysql_query($edit_query);

                    while ($edit_row = mysql_fetch_array($run_edit)) {

                        $post_id = $edit_row['post_id'];
                        $post_title = $edit_row['post_title'];
                        $post_author = $edit_row['post_author'];
                        $post_keywords = $edit_row['post_keywords'];
                        $post_image = $edit_row['post_image'];
                        $post_content = $edit_row['post_content'];

                        ?>

                <form method="post" action="edit_posts.php" enctype="multipart/form-data">

                              <table width="600" align="center" border="10" bgcolor="brown">

                                 <tr>
                                   <td align="center" bgcolor="yellow" colspan="6"><h1>Edit The Post Here</h1></td>
                                 </tr>

                                 <tr>
                                    <td align="right">Post Title:</td>
                                    <td><input type="text" name="title" size="35" value="<?php echo $post_title; ?>"></td>
                                 </tr>

                                  <tr>
                                    <td align="right">Post Author:</td>
                                    <td><input type="text" name="author" size="35" value="<?php echo $post_author; ?>"></td>
                                 </tr>

                                  <tr>
                                    <td align="right" >Post Keywords:</td>
                                    <td><input type="text" name="keywords" size="35" value="<?php echo $post_keywords; ?>"></td>
                                 </tr>

                                  <tr>
                                    <td align="right">Post Image:</td>
                                    <td>
                                    <input type="file" name="image">
                                    <img src = "../images/<?php echo $post_image; ?>" height="56.25" width="75"></td>
                                 </tr>

                                  <tr>
                                    <td align="right" >Post Content:</td>
                                    <td><textarea name="content" cols="30" rows="15"/><?php echo $post_content; ?></textarea></td>
                                 </tr>

                                  <tr>
                                    <td align="center" colspan="6"><input type="submit" name="update" value="Update Now"/></td>
                                 </tr>

                              </table>

                </form>

<?php }} ?>

                </body>
                </html>

                <?php

                if (isset($_POST['update'])){ 

                    $update_id = $_GET['edit_form']; 
                    $post_title1 = $_POST['title']; 
                          $post_date1 = date('m-d-y'); 
                          $post_author1 = $_POST['author'];
                          $post_keywords1 = $_POST['keywords'];
                          $post_content1 = $_POST['content'];
                          $post_image1 = $_FILES['image']['name'];
                          $image_tmp = $_FILES['image']['tmp_name'];

                    if ($post_title1=='' or $post_author1=='' or
                    $post_keywords1=='' or $post_content1=='' or
                    $post_image1=='') {

                    echo "<script>alert ('Any of the fields are empty')</script>";
                    exit(); 
                    }

                    else {

                    move_uploaded_file($image_tmp,"../images/$post_image1");

                    $update_query = "update posts set post_title='
                    $post_title1', post_date='$post_date1',post_author='$post_author1', post_image='$post_image1', post_keywords='$post_keywords1', post_content='$post_content1' where post_id='$update_id'";

                    if(mysql_query($update_query)) {

                    echo "<script>alert('Post has been updated')</script>"; 

                echo "<script>window.open('view_posts.php','_self')</script>";  
                    }
                }
                }


                ?>

4 个答案:

答案 0 :(得分:1)

post_title在您达到此目的时设置:

isset($_GET['edit'])

检查你如何访问那里和你的查询

编辑:

 $edit_query = "select * from posts where post_id = '$edit_id'";
你的意思是:

 $edit_query = "select * from posts where post_id = '".$edit_id."'";

答案 1 :(得分:0)

您的数据库查询似乎没有返回任何结果。因此$ post_title变量未设置,您将获得未定义的变量错误。检查查询是否按预期工作。

答案 2 :(得分:0)

您应该在表单后关闭if (isset($_GET['edit'])) {,因为如果没有该信息,表单将无法运行。我还会将POST处理放在顶部,以便您可以显示带有预填充值的相同表单,如果无法处理表单,则会显示错误消息。

除此之外,如果没有找到行编辑,你还应该添加一些错误处理。

最后你应该切换到PDO或mysqli ans prepared语句,因为mysql_*函数已被弃用,你现在有一个严重的SQL注入问题。

答案 3 :(得分:0)

<form method="post" action="edit_posts.php?edit_forms=<?php echo $post_id; ?>" enctype="multipart/form-data">

我认为这会有所帮助