无法使用$ id更新mysql表

时间:2012-07-31 16:14:03

标签: php mysql content-management-system get sql-update

我正在尝试编辑功能来编辑产品, 就像一个小型CMS。

问题是我无法更新我的mysql表 $ id = $ _GET ['id'];然后$ query =“UPDATE producten SET naam ='$ naam'WHERE id ='$ id'”; 我搜索了整个互联网,但找不到任何帮助。

当我尝试$ id = '50'时;例如,我不会更新ID为50的产品。

如果我尝试$ id = $ _GET ['id']; echo $ id;我确实得到了合适的身份。

这是我的完整代码:

        <?php           
        include "../php_includes/config.php";

        //Conect to the server/database
        if($connect){
            $errors = array();

        // check if the 'id' variable is set in URL, and check that it is valid
        if (isset($_GET['id']) && is_numeric($_GET['id']))
        {
        $id = $_GET['id'];
        }
        $query = mysql_query("SELECT * FROM producten WHERE id='$id'");
        while($query_row = mysql_fetch_assoc($query)){
                $naam_oud = $query_row['naam'];
                $naam_de_oud = $query_row['naam_de'];
                $prijs_oud = $query_row['prijs'];
                $image_oud = $query_row['image'];
        }
        //Display Posts
        if(isset($_POST['naam'],$_POST['naam_de'], $_POST['prijs'])){
            $naam = mysql_real_escape_string(htmlentities($_POST['naam']));
            $naam_de = mysql_real_escape_string(htmlentities($_POST['naam_de']));
            $prijs = mysql_real_escape_string(htmlentities($_POST['prijs']));
            $image = $_FILES['file']['name'];
            $tmp_name = $_FILES['file']['tmp_name'];

            if($image){
                $location = "product_foto/.$image";
                move_uploaded_file($tmp_name,$location);

            }else{
                $location = $image_oud;
            }
    //Errors
            if(empty($naam)){
                $errors[] = '<p class="error">Vul een nederlandse product naam in.</p>';
            }
            if(empty($naam_de)){
                $errors[] = '<p class="error">Vul een duitse product naam in.</p>';
            }

            if(empty($prijs)){
                $errors[] = '<p class="error">Vul een prijs in</p>';
            }
    //Insert Into Database
            if (empty($errors)){
                $query = "UPDATE producten SET naam='$naam',naam_de='$naam_de',prijs='$prijs', image='$location' WHERE id='$id'";
                    if (mysql_query($query)){
                        header('Location: ../admin/admin.php');die();               
                    } else{
                        $errors[] = '<p class="error">Oeps.. Er is iets verkeerd gegaan. Probeer later opnieuw.</p>';
                    }
    //Errors
            } else{
                foreach($errors as $error){
                echo $error;
                }
            }
        }
        }else{ 
            echo'<p class="error">Kan geen verbinding maken.<br/> Probeer later opnieuw.</p>';
        }
    ?>


            <form class="margin" action="<?php echo htmlentities($_SERVER['PHP_SELF']);?>" method="POST" enctype='multipart/form-data'>
                <strong>Product naam Nederlands</strong> <br/>  <input value="<?php echo $naam_oud;?> " id="focus" type="text" class="input_field" name="naam" maxlength="150" style="width:600px;"><br/><br/>
                <strong>Product naam Duits</strong> <br/>  <input id="focus" type="text" class="input_field" value="<?php echo $naam_de_oud;?>"name="naam_de" maxlength="150" style="width:600px;"><br/><br/>
                <strong> Prijs</strong> <br/> <input id="focus" type="text" class="input_field" name="prijs" value="<?php echo $prijs_oud;?> "maxlength="150" style="width:600px;"><br/>
                Vorm: euros,centen. zonder euro tekens. (Toevoegingen kunnen na het bedrag geplaatst worden.)<br/><br/>
                <strong> Foto</strong> <br/><input type="file" name="file"/><br/><br/>
                <br/><input type="submit" class="button" value="Bewerken" id="submit"/><a href="../admin/admin">Annuleren</a>
            </form>

2 个答案:

答案 0 :(得分:0)

    // check if the 'id' variable is set in URL, and check that it is valid
    if (isset($_GET['id']) && is_numeric($_GET['id']))
    {
        $id = $_GET['id'];
    }

您是否验证此if语句是否为true且$ id是否已设置?

答案 1 :(得分:0)

试试这个

"UPDATE producten SET naam='".$naam."', naam_de='".$naam_de."', prijs='".$prijs."', image='".$location."' WHERE id='".$id."'";