PHP Database INSERT problem

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

echo "category_id."'>".$row->category."";
i believe it is because you are sending the value of an id across as the value but you are not calling that value when you are wanting it to post into the database i will do more checking

dump the post vars see what they are and if they are correct and what you expected them to be.

[php]print_r($_POST)[/php]

also try adding
see if it is correct or has an error inserting
[php]die(mysql_query());[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service