在MySQL中更新图像

时间:2011-11-06 16:50:06

标签: php mysql

我花了最后3个小时试图让这段代码正常运行。我不明白为什么它告诉我'id'是未定义的。

我的目标是能够在我的小桌子上更新图片。到目前为止,由于我遇到的错误,我无法更新它。

以下是整个代码:

    <?php

    ?>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>Edit Details</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-5">

    </head>
    <body>

        <div id="content">
          <div class="about">
             <h2>Modify Entry</h2>

          <div class="main">                         

                <?php
                 global $record;
                 global $oldPic2;
                 global $oldPic3;
                 global $id;

                 $con = mysql_connect('localhost', 'root', '') or die(mysql_error()) ; 

                if (!$con){
                   die('Could not connect: ' . mysql_error());
                }
                  mysql_select_db("peopledb", $con);

                 if(!isset($_POST['submit'])){
                     $q = "SELECT * from image_table WHERE id = $_GET[id]";
                     $result = mysql_query($q) or die(mysql_error());
                     $record = mysql_fetch_array($result);

                     $oldPic2 =  $record['img2'];
                     $oldPic3 =  $record['img3'];

                }
           ?>

   <form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
   <div style="font-weight:bold; margin-top:5px; border: 1px solid #C6CFE1; padding:5px 0 5px 5px;background-color: #F5F2EE;">

    <table cellspacing="6">
        <tr>    
           <td>
             <b>Name:</b>
          </td>
            <td>
            <input type="text" name="name" id="name" value ="<?php echo $record['name']; ?>" /> 
            </td>

            <td>
               <b>Image 1:</b>
           </td>
           <td>
            <input type="text" name="breed" id="breed" value ="<?php echo $record['img']; ?>" /> 
           </td>

           <td>
             <b>Image 2:</b>
           </td>
           <td>
             <input type="text" name="img2" id="img2" size="16" value ="<?php echo $record['img2']; ?>" /> 
           </td>
           </tr>


           </table>

        <table>
            <tr>
             <td style="padding-right: 15px;">
            <img src="test/<?php echo $record['img']; ?>" width="45" height="40" />  
             </td>
            <td>
            <input type="file" name="pic" value="Browse">
            </td>
        </tr>
        <tr>
        <td>
        <img src="test/<?php echo $record['img2']; ?>" width="45" height="40" /> 
         </td>
        <td>
        <input type="file" name="pic2" value="Browse">
        </td>
        </tr>


        <tr>
         <td>
                            <img src="test/<?php echo $record['img3']; ?>" width="45" height="40" /> 
                       </td>
                       <td>
                            <input type="file" name="pic3" value="Browse">
                       </td>
                    </tr>

                    </table>

                            <p />
                            <b>Description:</b> <br />
                            <script>edToolbar('description'); </script>
                            <textarea rows="10" cols="60" id="description" name="description" align="center"><?php echo $record['description']; ?></textarea><br /><br />                                               
                            <br /><br />                         
                            <input type="hidden" name="id" value="<?php echo $_GET['id']; ?>" />
                            <input type="submit" name="submit" value="Submit Update" />
                </div>
            </form>
      <?php
                    if(isset($_POST['submit'])){                


                            $pic =($_FILES['pic']['name']); 

                            $pic2 =($_FILES['pic2']['name']);  
                            $pic3 =($_FILES['pic3']['name']);  

                            if( $pic == ""){
                                    $pic = $record['img']; 
                            }   
                            if( $pic2 == ""){
                                    $pic2 = $oldPic2; 

                            }    

                            if( $pic3 == ""){
                                    $pic3 = $oldPic3; 

                            }    


                            $id = $_GET[id];
                            $q = "UPDATE image_table SET `name`='$_POST[name]', 
                            `description`='$_POST[description]',
                            `img`= '$pic',
                            `img2`= '$pic2',
                            `img3`='$pic3',
                             WHERE id = $_POST[id]";
                             mysql_query($q) or die(mysql_error());                             

                            $target = "test/"; 
                            //$target1 = $target."/".$record['id'];

                            $target1 = $target . basename( $_FILES['pic1']['name']); 
                            $target2 = $target . basename( $_FILES['pic2']['name']); 
                            $target3 = $target . basename( $_FILES['pic3']['name']); 

                            if(move_uploaded_file($_FILES['pic']['tmp_name'], $target1)) { 

                            }
                            if(move_uploaded_file($_FILES['pic2']['tmp_name'], $target2)) { 

                            }if(move_uploaded_file($_FILES['pic3']['tmp_name'], $target3)) { 

                            echo "Image updated successfully";  
                    }else{
                            echo"There was an error updating your picture";
                    }

                }
            ?>                                  
                    </div>


                    </div>
                    <div class="list">

                    </div>

            </div>

        </body>

    </html>

任何帮助都将受到高度赞赏。

注意: select语句有效,但是当我尝试更新时它会抛出错误消息。 Id underfined :(

2 个答案:

答案 0 :(得分:2)

用$ _POST [id]

替换$ _GET [id]

答案 1 :(得分:0)

我终于开始工作了。将id声明为全局就可以了。以前,在分配值的块之外,id不可用。

学习新事物的好方法:)