我正在尝试使用编辑功能来编辑产品,例如小型 CMS。
问题是我不能用 $id = $_GET['id']; 更新我的 mysql 表。然后 $query = "UPDATE producten SET naam='$naam' WHERE id='$id'"; 我搜索了整个互联网,但找不到任何帮助。
当我尝试 $id = '50'; 例如,我不会更新 id 为 50 的产品。
如果我尝试 $id = $_GET['id']; 回声 $id; 我确实得到了正确的 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>