final60 Posted August 5, 2011 Share Posted August 5, 2011 Hi, Im having a problem with inserting my forum category id into the datase when a new forum post is submitted. Heres part of the markup $cat_id = $_GET['cat_id']; if(isset($submit) && ($title != 'Title') && strlen($title)>5) { if(!empty($title) ) { if(!empty($content) && ($content != 'Enter your post content here..') && strlen($content)>9) { mysql_query("INSERT INTO urbex_forum_posts (user_id,title,content,date,sticky,category_id) VALUES('$_SESSION[urbex_user_id]','$title','$content',NOW(),'0','$_GET[cat_id]')"); }else{ echo "<p>Could not process post: Please make sure the content is atleast 10 characters!</p>"; } }else{ echo "<p>Could not process post: Please make sure the title is atleast 6 characters!</p>"; } } When I use both $cat_id or just $_GET['cat_id'] both submit 0 into the cell in the database table. When I echo both out on the page the both echo the correct category name. Quote Link to comment https://forums.phpfreaks.com/topic/243954-strange-issue-variable-insert-into-database/ Share on other sites More sharing options...
Pikachu2000 Posted August 5, 2011 Share Posted August 5, 2011 What is the data type of the `category_type` field set as? Quote Link to comment https://forums.phpfreaks.com/topic/243954-strange-issue-variable-insert-into-database/#findComment-1252633 Share on other sites More sharing options...
final60 Posted August 5, 2011 Author Share Posted August 5, 2011 it is set as int(11) when echo'd on the page it displays 1. Quote Link to comment https://forums.phpfreaks.com/topic/243954-strange-issue-variable-insert-into-database/#findComment-1252636 Share on other sites More sharing options...
Pikachu2000 Posted August 5, 2011 Share Posted August 5, 2011 You say when you echo the variable, it echos the correct name. Are you trying to insert string data into an integer field, by chance? Quote Link to comment https://forums.phpfreaks.com/topic/243954-strange-issue-variable-insert-into-database/#findComment-1252638 Share on other sites More sharing options...
final60 Posted August 5, 2011 Author Share Posted August 5, 2011 No, sorry I shoul dhave been clearer (edited previous post too late) it echos 1, but when i try to insert that in the database it always enters 0 Quote Link to comment https://forums.phpfreaks.com/topic/243954-strange-issue-variable-insert-into-database/#findComment-1252642 Share on other sites More sharing options...
Pikachu2000 Posted August 5, 2011 Share Posted August 5, 2011 For the time being, echo any errors, and some debugging info and see what it shows, if anything. $cat_id = $_GET['cat_id']; if(isset($submit) && ($title != 'Title') && strlen($title)>5) { if(!empty($title) ) { if( !empty($content) && ($content != 'Enter your post content here..') && strlen($content)>9 ) { $query = "INSERT INTO urbex_forum_posts (user_id,title,content,date,sticky,category_id) VALUES('$_SESSION[urbex_user_id]','$title','$content',NOW(),'0','$_GET[cat_id]')"; if( $result = mysql_query($query) ) { if( mysql_affected_rows($result) !== 1 ) { echo "<br>Query executed without inserting record.<br>Query string: $query<br>"; } } else { echo "<br>Query: $query<br>Failed with error: " . mysql_error() . '<br>'; } } else { echo "<p>Could not process post: Please make sure the content is atleast 10 characters!</p>"; } } else { echo "<p>Could not process post: Please make sure the title is atleast 6 characters!</p>"; } } Quote Link to comment https://forums.phpfreaks.com/topic/243954-strange-issue-variable-insert-into-database/#findComment-1252648 Share on other sites More sharing options...
final60 Posted August 5, 2011 Author Share Posted August 5, 2011 Warning: mysql_affected_rows(): supplied argument is not a valid MySQL-Link resource in //create_forum_post.php on line 90 Query executed without inserting record. Query string: INSERT INTO urbex_forum_posts (user_id,title,content,date,sticky,category_id) VALUES('14','llllllllllllllllllllll','llllllllllllllllllllllllllllllllllll',NOW(),'0','') Quote Link to comment https://forums.phpfreaks.com/topic/243954-strange-issue-variable-insert-into-database/#findComment-1252654 Share on other sites More sharing options...
Pikachu2000 Posted August 5, 2011 Share Posted August 5, 2011 Oh, duh. Remove $result from the call to mysql_affected_rows() . . . At any rate, it doesn't appear $_GET['cat_id'] has a value at that point in the script for some reason. Now the thing is to figure out why that's the case. Quote Link to comment https://forums.phpfreaks.com/topic/243954-strange-issue-variable-insert-into-database/#findComment-1252656 Share on other sites More sharing options...
final60 Posted August 5, 2011 Author Share Posted August 5, 2011 yeah its frustrating. I just used a hidden input type with the cat_id echo into it and it submits the data fine that way, but that doesnt solve the issue really :/ I Appreciate your efforts.. Quote Link to comment https://forums.phpfreaks.com/topic/243954-strange-issue-variable-insert-into-database/#findComment-1252659 Share on other sites More sharing options...
kickstart Posted August 5, 2011 Share Posted August 5, 2011 Hi Err, just realised. You mention a hidden field but that suggests a form POST variable rather than a GET one. All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/243954-strange-issue-variable-insert-into-database/#findComment-1252660 Share on other sites More sharing options...
Pikachu2000 Posted August 5, 2011 Share Posted August 5, 2011 If you're submitting the form multiple times and the data is getting lost at that point, using a hidden field can work. You could also use a $_SESSION var, and unset it after the query executes successfully if needed. Quote Link to comment https://forums.phpfreaks.com/topic/243954-strange-issue-variable-insert-into-database/#findComment-1252661 Share on other sites More sharing options...
final60 Posted August 5, 2011 Author Share Posted August 5, 2011 It is just the single form. the cat_id is passed to the page in the url which i was trying to input directly using GET in the mysql_query which wouldnt work, I also tried putting the cat_id in a variable first but nada. Ive changed it to using a hidden input field with the cat_id echoed into its value which works fine, but would have rather not done that. Quote Link to comment https://forums.phpfreaks.com/topic/243954-strange-issue-variable-insert-into-database/#findComment-1252668 Share on other sites More sharing options...
fenway Posted August 5, 2011 Share Posted August 5, 2011 Prove that your variable is making it to your script. Quote Link to comment https://forums.phpfreaks.com/topic/243954-strange-issue-variable-insert-into-database/#findComment-1252727 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.