I’ve got a little problem while writing my Website. I want to insert a Post into a Database with this Code. Database connection works! The other part of the Website that reads these posts works.
[php]<?php
session_start();
include(‘includes/db_connect.php’);
if(!isset($_SESSION[‘user_id’])){
header(‘Location: login.php’);
exit();
}
if(isset($_POST[‘submit’])){
$title = $_POST[‘title’];
$body = $_POST[‘body’];
$category = $_POST[‘category’];
$title = $db->real_escape_string($title);
$body = $db->real_escape_string($body);
$user_id = $_SESSION[‘user_id’];
$date = date(‘Y-m-d G:i:s’);
$body = htmlentities($body);
if($title && $body){
$query = $db->query("INSERT INTO posts (user_id, title, body, category_id, posted) VALUES('$user_id', '$title', '$body', '$category', '$date')");
if($query){
echo "posted!";
}else{
echo "Something went wrong";
}
}else{
echo "Not enougth information";
}
}
?>[/php]
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
<label>Titel:</labeö><input type="text" name="title" />
<label for="body">Post:</label>
<textarea name="body" cols="50" rows="10"></textarea>
<label> Category:</label>
<select name="category">
<?php
$query = $db->query("SELECT * FROM categories");
while($row = $query->fetch_object()){
echo "<option value'".$row->category_id."'>".$row->category."</option>";
}
?>
</select>
<br />
<input type="submit" name="submit" value="Post this" />
</form>
Maybe it’s just a little problem maybe not… i can’t find the bug.
Thanks