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”;
}
?>
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.