Al opgelost!
Hallo allemaal,
Ik heb een script geschreven welke bepaalde data moet opslaan in de database en ook een bestand op de server moet uploaden.
De gegevens die worden opgeslagen zijn: city_name, city_slug en city_plan. Nu slaat hij de eerste twee wel op, maar de laatste maar half. Hij zet namelijk enkel city-plans/ in de table.
Code:
add-city.php
PHP
<?php
include_once 'core/init.php';
$general->logged_out_protect();
$id = $user['id'];
$users->is_owner($id);
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css" href="css/style.css" >
<title>Add city</title>
</head>
<body>
<div id="container">
<?php include 'includes/menu.php'; ?>
<h1>Add a city</h1>
<?php
if (isset($_GET['success']) && empty($_GET['success'])) {
echo '<h3>City has been added!</h3>';
exit();
}
else {
if(empty($_POST) === false) {
if (isset($_POST['city_name']) && empty($_POST['city_name'])){
$errors[] = 'Enter a city name';
}
if (isset($_POST['city_slug']) && empty($_POST['city_slug'])){
$errors[] = 'Enter a city slug';
}
}
if(!empty($_POST) && empty($errors) === true) {
$name = $_FILES['city_plan']['name'];
$tmp_name = $_FILES['city_plan']['tmp_name'];
$path = "city-plans";
move_uploaded_file($tmp_name, "$path/$name");
$city_name = htmlentities(trim($_POST['city_name']));
$city_slug = htmlentities(trim($_POST['city_slug']));
$city_plan = 'city-plans/'.$name;
$cities->add_city($city_name,$city_slug,$city_plan);
header('Location: add-city.php?success');
exit();
}
else if (empty($errors) === false) {
echo '<p>' . implode('</p><p>', $errors) . '</p>';
}
}
?>
<form action="" method="post">
<ul>
<li>
<h4>City name:</h4>
<input type="text" name="city_name" id="city_name" value="">
</li>
<li>
<h4>City slug:</h4>
<input type="text" name="city_slug" id="city_slug" value="">
</li>
<li>
<h4>City plan:</h4>
<input type="file" name="city_plan" id="city_plan">
</li>
</ul>
<span>Save city:</span>
<input type="submit" value="save">
</form>
</div>
</body>
Toon Meer
Class:
PHP
public function add_city($city_name,$city_slug,$city_plan) {
$query = $this->db->prepare("INSERT INTO `cities` (`name`,`slug`,`background_plan`) VALUES (?,?,?)");
$query->bindValue(1, $city_name);
$query->bindValue(2, $city_slug);
$query->bindValue(3, $city_plan);
try {
$query->execute();
}
catch(PDOExeption $e) {
die($e->getMessage());
}
}
Toon Meer
Waarschijnlijk zie ik door de bomen het bos niet meer... Maar wie weet wat ik fout doe?
EDIT: Ik zag inderdaad door het bomen het bos niet meer! Ik was vergeten deze line toe te voegen aan de <form>