Erratic Problems Adding User ID to MySQL Table of User Lists -


i have site users can log-in , add items list.

the user logs in , session stores e-mail, use identify them in user table.

then, can input list item , add list table contains id , list items.

sometimes id added , comes null (however, list item text added). seems erratic because of time id included.

any ideas? table type myisam. i'm new programming, btw.

here's example of code:

<?php  session_start(); $item = $_request['item']; $email = $_session['email'];  if ($item) {  mysql_connect("localhost","root","") or die("we couldn't connect!");  mysql_select_db("table");  $query = mysql_query("select id users email='".$_session['email']."'"); $result = mysql_result($query,0); $user_id = $result;  mysql_query("insert items (user_id,item_name) values('$user_id','$item')"); 

so every time test logging site myself, no problems. increasingly, have users try add items , creates record item_name shows correctly user_id set 0 (default).

first off, read said sql injection attacks in comments question.

it idea store user_id in $_session wouldn't have query every time based on email... if insist on having email in $_session, need 1 query. adjusted code:

<?php  session_start(); $item = mysql_real_escape_string($_request['item']);  if (!empty($item) && isset($_session['email'])) {     mysql_connect("localhost","root","") or die("we couldn't connect!");     mysql_select_db("table");      mysql_query("insert items (user_id, item_name) values ((select id users email='{$_session['email']}'), '$item')"); } 

like jeff watkins said, session timed out, idea first check if it's set using isset().

otherwise if stored userid in session, reference directly $_session['user_id'] instead of doing subquery in insert.


Comments

Popular posts from this blog

django - How can I change user group without delete record -

java - Need to add SOAP security token -

java - EclipseLink JPA Object is not a known entity type -