可以';t使用$id更新mysql表


Can't update mysql table with $id

我正在尝试制作一个编辑功能来编辑产品,像小型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>
    // 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语句为真,并且$id确实已设置?

试试这个

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