Kaboom Posted October 20, 2009 Share Posted October 20, 2009 OkaY so I got my blog to actually save the posts and whatever, all I need now is for it to keep the right posted. That database is set up to keep post id, title, subject, date, and author. The only problem is author. It won't post the right author, i need it to put the username of the poster in that spot but it either puts nothing or if I put it to ["user"] it puts Array in the field.... so please help, how do I save the username? Here is my code in post.php <?php include "../config.php"; include "../func.php"; include "../english.php"; $subject=clean($_POST["subject"]); $name=($_SESSION["name"]); $inhoud=clean($_POST["inhoud"]); if (($_POST['post'])&&($_SESSION["code"]==$_POST["code"])) { $query="insert into `babys` (`name`, `subject`, `content`, `date`) values('".$name."', '".$subject."', '".$inhoud."', now())"; $result = mysql_query($query); if ($result) header('Location: index.php'); else msg("Failed.".mysql_error()); } else { msg($lang['incorCode']); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/178360-save-username-into-db/ Share on other sites More sharing options...
redarrow Posted October 20, 2009 Share Posted October 20, 2009 where id=userid what you mean.... the id will be the current id of the user? Quote Link to comment https://forums.phpfreaks.com/topic/178360-save-username-into-db/#findComment-940537 Share on other sites More sharing options...
Kaboom Posted October 20, 2009 Author Share Posted October 20, 2009 Okay so for name it has to get the users name from the tables users and then post that into the field name of that post so you can tell who the author is... heres the structure of the table its posting in; CREATE TABLE IF NOT EXISTS `babys` ( `id` int(10) unsigned NOT NULL auto_increment, `name` varbinary(45) NOT NULL, `subject` varbinary(45) NOT NULL, `content` varbinary(10240) NOT NULL, `date` datetime NOT NULL default '0000-00-00 00:00:00', PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=9 ; Now where it says `name` i need it the put the posters name which is stored in a different table called users ... how? Quote Link to comment https://forums.phpfreaks.com/topic/178360-save-username-into-db/#findComment-940545 Share on other sites More sharing options...
redarrow Posted October 20, 2009 Share Posted October 20, 2009 create table names; user_name_id id name update set name='$name',id='$id' where id='$id' somethink like that innit. Quote Link to comment https://forums.phpfreaks.com/topic/178360-save-username-into-db/#findComment-940565 Share on other sites More sharing options...
mikesta707 Posted October 20, 2009 Share Posted October 20, 2009 one thing i noticed is you don't clean the Session name variable. it looks like you want to $name=($_SESSION["name"]); perhaps that should be $name=clear($_SESSION["name"]); I don't know if that would be causing the error though. try echoing session['name'] and see if it has what you expect it to have Quote Link to comment https://forums.phpfreaks.com/topic/178360-save-username-into-db/#findComment-940566 Share on other sites More sharing options...
Kaboom Posted October 20, 2009 Author Share Posted October 20, 2009 Okay ... well I used the clean feature to get rid of the junk but then I have this problem .. Its not registering then as a user. I am working on a string to define them as users .. looks kinda like this function is_user2($name) { global $db_id; $query="select count(*) from users where name='".$name."'"; $result=mysql_query($query, $db_id); $row=mysql_fetch_row($result); return $row[0]; } Now how would I do it like if (($_POST['post'])&&($_SESSION["code"]==$_POST["code"])&&(!is_user)) { so that it forces them to be a user so they can post? Quote Link to comment https://forums.phpfreaks.com/topic/178360-save-username-into-db/#findComment-940589 Share on other sites More sharing options...
mikesta707 Posted October 20, 2009 Share Posted October 20, 2009 I don't really get what you mean when you say "force them to be a user" but as I understand from your first post you are having a problem with the session variable. make sure you have session_start() at the beginning of the page. try doing a print_r on the session variable to see what it holds. also, try echoing the query to make sure it looks like it should. im not sure what you are trying to do with that function, but if you should return a boolean value, rather than a number function is_user2($name) { global $db_id; $query="select count(*) from users where name='".$name."'"; $result=mysql_query($query, $db_id); $row=mysql_fetch_row($result); return (bool)$row[0]; } PHP gets kind of freaky with boolean coercion and sometimes you have to use the identical operator (===) rather than the equality operator (==) Quote Link to comment https://forums.phpfreaks.com/topic/178360-save-username-into-db/#findComment-940685 Share on other sites More sharing options...
Kaboom Posted October 20, 2009 Author Share Posted October 20, 2009 I don't really get what you mean when you say "force them to be a user" im not sure what you are trying to do with that function, Okay so for some reason it doesn';t know what their username is or something ... What I am trying to do is get it to do this insert into `babys` (`name`, `subject`, `content`, `date`) values('THEIRUSERNAMEHASTOBEHERE', 'SUBJECTOFTHEPOST', 'CONTENTOF THEPOST', now()) I have it saving the subject and the content but It won't save their username in the name spot ... idk how to make it load that so it can save there... Heres my new post.php that works except the name part <?php include "../config.php"; include "../func.php"; include "../english.php"; function name($name) { global $db_id; $query="select count(*) from users where name='".$name."'"; $result=mysql_query($query, $db_id); $row=mysql_fetch_row($result); return (bool)$row[0];; } $usr=user($_GET["id"]); $subject=clean($_POST["subject"]); $name=clean($_SESSION["name"]); $inhoud=clean($_POST["inhoud"]); if (($_POST['post'])&&($_SESSION["code"]==$_POST["code"])) { $query="insert into `babys` (`name`, `subject`, `content`, `date`) values('".$user."', '".$subject."', '".$inhoud."', now())"; $result = mysql_query($query); if ($result) header('Location: index.php'); else msg("Failed.".mysql_error()); } else { msg($lang['incorCode']); } ?> How do i make that save their username into the name spot for THAT post? Quote Link to comment https://forums.phpfreaks.com/topic/178360-save-username-into-db/#findComment-940729 Share on other sites More sharing options...
mikesta707 Posted October 20, 2009 Share Posted October 20, 2009 first of all, this $usr=user($_GET["id"]); is invalid, because user() isn't even a function. it seems you wanted to make a function to get the username with this function function name($name) { global $db_id; $query="select count(*) from users where name='".$name."'"; $result=mysql_query($query, $db_id); $row=mysql_fetch_row($result); return (bool)$row[0];; } but there are alot things wrong with this... firstly, if you want to get the username from a query, you have to select the username. secondly, you should probably return the username. What is $_GET['id']? is name the column in your users table with the username? your function should probably look something like this function name($name) { global $db_id; $query="select username from users where name='".$name."'"; $result=mysql_query($query, $db_id); $row=mysql_fetch_assoc($result); return $row['username']; } but if you are using the username in your site in enough places to warrant making a function to get the username, why not just store the username in a session variable? Quote Link to comment https://forums.phpfreaks.com/topic/178360-save-username-into-db/#findComment-940744 Share on other sites More sharing options...
Kaboom Posted October 21, 2009 Author Share Posted October 21, 2009 that makes sence.. so ($_SESSION["'.name.'"])==($_SESSION["name"]) or what? Quote Link to comment https://forums.phpfreaks.com/topic/178360-save-username-into-db/#findComment-940919 Share on other sites More sharing options...
Kaboom Posted October 21, 2009 Author Share Posted October 21, 2009 Okay the only thing I need now is to make it save the usernames.... any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/178360-save-username-into-db/#findComment-941285 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.