# php insertion problem

**URL:** <https://www.phphelp.com/t/php-insertion-problem/20149>\
**Category:** General PHP Help\
**Created:** [April 27, 2013, 7:39pm UTC](https://www.phphelp.com/t/php-insertion-problem/20149 "2013-04-27T19:39:05Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://www.phphelp.com/uploads/default/original/2X/2/243e9ed5d93c5653ce3186f18df16e019ce38445.png) [@system](https://www.phphelp.com/u/system)\
**Post date:** [April 27, 2013, 7:39pm UTC](https://www.phphelp.com/t/php-insertion-problem/20149/1 "2013-04-27T19:39:05Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![richei](https://www.phphelp.com/letter_avatar_proxy/v4/letter/r/e5b9ba/32.png) [@richei](https://www.phphelp.com/u/richei)\
**Post date:** [April 28, 2013, 9:22am UTC](https://www.phphelp.com/t/php-insertion-problem/20149/2 "2013-04-28T09:22:20Z")

</div>

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.
