Jump to content

Save username into DB


Kaboom

Recommended Posts

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']);
}


?>

Link to comment
Share on other sites

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? :D

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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 (==)

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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?

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.