php insertion problem

I have created 2 tables in php myadmin the first table is
member with rows
user_id - primary key
name
username
password

the second table is blogdata with rows
id - primary key
author_id -foreign key references user_id in member table
Title
Content
Category

i cant enter blog posts in the database for each user

here is my code for the insertion

[php]<?php
$connection = mysql_connect(“localhost”,“root”,"");
mysql_select_db(“blog1”);

?>

Admin - Post new entry <?php

$session_id =[‘user_id’];
session_start();
//echo $_SESSION[‘username’];
if(isset($_POST[‘submit’])){
$title = $_POST[‘Title’];
$category =$_POST[‘Category’];
$content = $_POST[‘Content’];

mysql_query(“INSERT INTO blogdata (Title,Category,content)VALUES(’$title’,’$category’,’$content’)”);
}else{

?>

Title:
Category:
Content:
<?php } if(isset($_SESSION['username'])){

echo “Logout”;
}else{
echo “Login”;
}

?>


<?php echo "click here to view blog"; ?> [/php]

Here is my code to show the blogs

[php]<?php
$connection = mysql_connect(“localhost”,“root”,"");
mysql_select_db(“blog1”);

?>

Show me my blog Here is My Blog
<?php session_start(); //echo $_SESSION['username']; $sql = mysql_query("SELECT * FROM blogdata ORDER by author_id"); while($row = mysql_fetch_array($sql)){ $author = $_GET['author_id']; $title = $row['Title']; $category =$row['Category']; $content =$row['Content']; $date =$row['date'];

?>

<?php echo $author; ?>
<?php echo $title; ?>
<?php echo $category; ?>
<?php echo $content; ?>
<?php echo date("Y/m/d") . "
"; ?>


<?php } echo "Write a blog"; echo '';

if(isset($_SESSION[‘username’])){
echo “Logout”;
}else{
echo “Login”;
}

?>

[/php]

Here is my login code

this is the form

[code]
Username:

Password:


Signup its free!

[/code]

Here is the login php

[php<?php
$connection = mysql_connect(“localhost”,“root”,"");
mysql_select_db(“blog1”);
session_start();

$username = $_POST[‘username’];
$password = $_POST[‘password’];

if($username==""||$password==""){
echo “Fill in form properly”;
}
else{
$result = mysql_query(“SELECT * FROM member WHERE username =’$username’ AND password=’$password’”);
if(mysql_num_rows($result)==1){
$_SESSION[‘username’] = $username;
echo "you have logged in! ";
echo $_SESSION[‘username’];
echo " Click here to go to blog ";
}
else{
echo "

Username or password incorrect!

";
}
}
?>
[/php]

and here is the logout

[php<?php
session_start();
$_SESSION[‘name’] =’’;
session_destroy();

?>
][/php]

My logout works fine and my sign up works it enters users data in the database.

i don’t no how to set the session variable so that only a user can post, as it is now when a user types a blog it does not enter it in the database. Im new at coding so any help would be great.

Unless i’m missing something, you’re not entering the user’s user info when you insert the post. All you’re inserting is the title, text and category, but not the member or author id. It doesn’t who it belongs too.

As for the insert not working, add or die(MySQL_error()); to the end of the query (all in lowercase lettering). That’ll give an error message if something is wrong. Also need to add in some injection protection.

Sponsor our Newsletter | Privacy Policy | Terms of Service