Jump to content

[SOLVED] how do i make the fields value update with a user id


berry05

Recommended Posts

i made a script that every time someone buys a item it makes another field... is there a way to make it so when a user buys a item the item goes to that username and doesn't make another field?

 

heres the script ...

<?php
//connect
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("register") or die(mysql_error());

// start adding data
mysql_query("INSERT INTO users 
(hoe) VALUES('1') ") 
or die(mysql_error());  

?>

relational db would be better.

have user info in the users table and item relationships in another.

table users:

user_id || username || pass ||

12 || joe farmer || 123likes_hoes ||

table items:

item_id || item_name ||

26 || hoe ||

table inventory:

inv_id || item_id || user_id ||

1 || 26 || 12||

 

Easer mate..


user_id || username  || pass ||
   12    || joe farmer || 123likes_hoes ||

table items:
item_id  || user_id || item_name ||
   26     ||     12    ||      hoe     ||

table price

price_id || user_id || price

  45      ||   12     || 100.00

 

select * from what_ever where user_id='$user_id'

i sort of get the thing you sent me but what do i do with this code.

select * from what_ever where user_id='$user_id'

 

that thing you sent me doesn't help me out...I'm sorry.. and why do i have to have user id and item id in the items table?

why do i have to have user id and item id in the items table?

 

So you can relate a user to an item.

 

I'll give you a simple example.

 

CREATE TABLE users (
  user_id INT,
  name VARCHAR(80)
);

CREATE TABLE items (
  item_id INT,
  name VARCHAR
);

CREATE TABLE users_items (
  user_id INT,
  item_id
);

 

Now add some users.

 

INSERT INTO users (user_id, name) VALUES (1, 'foo');
INSERT INTO users (user_id, name) VALUES (2, 'bar');
INSERT INTO users (user_id, name) VALUES (3, 'bob');

 

Add some items...

INSERT INTO items (item_id, name) VALUES (1, 'car');
INSERT INTO items (item_id, name) VALUES (2, 'boat');
INSERT INTO items (item_id, name) VALUES (3, 'trailer');

 

Now, if I want to give the user bar a car and trailer it would be....

INSERT INTO users_items (user_id, item_id) VALUES (2, 1);
INSERT INTO users_items (user_id, item_id) VALUES (2, 3);

 

Can you see the relation?

 

Now to give foo a car....

INSERT INTO users_items (user_id, item_id) VALUES (1, 1);

nvm i figured it out..heres the code i inserted..

mysql_query("INSERT INTO users_items 
(user_id, item_id) VALUES('1', '1' ) ") 
or die(mysql_error());  

 

how do i do it though for logged in users..so it updates that user thats logged in..i'd have to edit the left 1 because its user_id but what would i change it too?

nevermind i figured it out..heres the code i inserted..

mysql_query("INSERT INTO users_items (user_id, item_id) VALUES('1', '1' ) ")  or die(mysql_error());  

 

how do i do it though for logged in users..so it updates that user thats logged in..i'd have to edit the left 1 because its user_id but what would i change it too?

 

when the user logs in, make sure you have a session for his ID. then use it like this

<?php
$userid = $_SESSION['user_id'];
mysql_query("INSERT INTO user_items (user_id, item_id) VALUES('$userid', '1' ) ") 
or die(mysql_error());
?>

 

edit: do you want to add another field for a logged in user, or update a certain item for that user? dont understando

im making a shop and when a user buys something from the shop it goes to that php file and updates the user with that item

:)

 

I guess use the $_SESSION to get the current user, and then update his shopping cart from a query by providing the user's id there.

 

edit:what do you mean it goes to the file

im making a shop and when a user buys something from the shop it goes to that php file and updates the user with that item

:)

 

I guess use the $_SESSION to get the current user, and then update his shopping cart from a query by providing the user's id there.

 

edit:what do you mean it goes to the file

 

its a php file that runs the script and adds the item to the user inventory..

 

and when i run hoe.php to add a hoe to the user...  i check my database and it has the item_id but doesnt have the username...its blank...what do i do...do i have my session wrong? heres my hoe.php file..

<?php
//connect
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("textgame") or die(mysql_error());

// start adding data
$active_users = $_SESSION['active_users'];
mysql_query("INSERT INTO users_items (username, item_id) VALUES('$active_users', '4' ) ") 
or die(mysql_error());


?>

Try this:

 

<?php
session_start();
//connect
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("textgame") or die(mysql_error());

// start adding data
$active_users = $_SESSION['active_users'];
mysql_query("INSERT INTO users_items (username, item_id) VALUES('$active_users', '4' ) ") 
or die(mysql_error());


?>

nope username is still blank in the database...think i need my sessions setup or something?

 

Yes your sessions are blank, and you forget session_start at the top of your page, meaning you would never get any sessions whatsoever.

 

Whats your login script look like?

INDEX.PHP

 

<?
/**
* Main.php
*
* This is an example of the main page of a website. Here
* users will be able to login. However, like on most sites
* the login form doesn't just have to be on the main page,
* but re-appear on subsequent pages, depending on whether
* the user has logged in or not.
*
* Written by: Jpmaster77 a.k.a. The Grandmaster of C++ (GMC)
* Last Updated: August 26, 2004
*/
include("include/session.php");
?>

<html>
<title>Jpmaster77's Login Script</title>
<body>

<table>
<tr><td>


<?
/**
* User has already logged in, so display relevant links, including
* a link to the admin center if the user is an administrator.
*/
if($session->logged_in){
   echo "<h1>Logged In</h1>";
   echo "Welcome <b>$session->username</b>, you are logged in. <br><br>"
       ."[<a href=\"userinfo.php?user=$session->username\">My Account</a>]   "
       ."[<a href=\"useredit.php\">Edit Account</a>]   ";
   if($session->isAdmin()){
      echo "[<a href=\"admin/admin.php\">Admin Center</a>]   ";
   }
   echo "[<a href=\"process.php\">Logout</a>]";
}
else{
?>

<h1>Login</h1>
<?
/**
* User not logged in, display the login form.
* If user has already tried to login, but errors were
* found, display the total number of errors.
* If errors occurred, they will be displayed.
*/
if($form->num_errors > 0){
   echo "<font size=\"2\" color=\"#ff0000\">".$form->num_errors." error(s) found</font>";
}
?>
<form action="process.php" method="POST">
<table align="left" border="0" cellspacing="0" cellpadding="3">
<tr><td>Username:</td><td><input type="text" name="user" maxlength="30" value="<? echo $form->value("user"); ?>"></td><td><? echo $form->error("user"); ?></td></tr>
<tr><td>Password:</td><td><input type="password" name="pass" maxlength="30" value="<? echo $form->value("pass"); ?>"></td><td><? echo $form->error("pass"); ?></td></tr>
<tr><td colspan="2" align="left"><input type="checkbox" name="remember" <? if($form->value("remember") != ""){ echo "checked"; } ?>>
<font size="2">Remember me next time     
<input type="hidden" name="sublogin" value="1">
<input type="submit" value="Login"></td></tr>
<tr><td colspan="2" align="left"><br><font size="2">[<a href="forgotpass.php">Forgot Password?</a>]</font></td><td align="right"></td></tr>
<tr><td colspan="2" align="left"><br>Not registered? <a href="register.php">Sign-Up!</a></td></tr>
</table>
</form>

<?
}

/**
* Just a little page footer, tells how many registered members
* there are, how many users currently logged in and viewing site,
* and how many guests viewing site. Active users are displayed,
* with link to their user information.
*/
echo "</td></tr><tr><td align=\"center\"><br><br>";
echo "<b>Member Total:</b> ".$database->getNumMembers()."<br>";
echo "There are $database->num_active_users registered members and ";
echo "$database->num_active_guests guests viewing the site.<br><br>";

include("include/view_active.php");

?>


</td></tr>
</table>


</body>
</html>

 

MAIN.PHP

 

<?
/**
* Main.php
*
* This is an example of the main page of a website. Here
* users will be able to login. However, like on most sites
* the login form doesn't just have to be on the main page,
* but re-appear on subsequent pages, depending on whether
* the user has logged in or not.
*
* Written by: Jpmaster77 a.k.a. The Grandmaster of C++ (GMC)
* Last Updated: August 26, 2004
*/
include("include/session.php");
?>

<html>
<title>Jpmaster77's Login Script</title>
<body>

<table>
<tr><td>


<?
/**
* User has already logged in, so display relavent links, including
* a link to the admin center if the user is an administrator.
*/
if($session->logged_in){
   echo "<h1>Logged In</h1>";
   echo "Welcome <b>$session->username</b>, you are logged in. <br><br>"
       ."[<a href=\"userinfo.php?user=$session->username\">My Account</a>]   "
       ."[<a href=\"useredit.php\">Edit Account</a>]   ";
   if($session->isAdmin()){
      echo "[<a href=\"admin/admin.php\">Admin Center</a>]   ";
   }
   echo "[<a href=\"process.php\">Logout</a>]";
}
else{
?>

<h1>Login</h1>
<?
/**
* User not logged in, display the login form.
* If user has already tried to login, but errors were
* found, display the total number of errors.
* If errors occurred, they will be displayed.
*/
if($form->num_errors > 0){
   echo "<font size=\"2\" color=\"#ff0000\">".$form->num_errors." error(s) found</font>";
}
?>
<form action="process.php" method="POST">
<table align="left" border="0" cellspacing="0" cellpadding="3">
<tr><td>Username:</td><td><input type="text" name="user" maxlength="30" value="<? echo $form->value("user"); ?>"></td><td><? echo $form->error("user"); ?></td></tr>
<tr><td>Password:</td><td><input type="password" name="pass" maxlength="30" value="<? echo $form->value("pass"); ?>"></td><td><? echo $form->error("pass"); ?></td></tr>
<tr><td colspan="2" align="left"><input type="checkbox" name="remember" <? if($form->value("remember") != ""){ echo "checked"; } ?>>
<font size="2">Remember me next time     
<input type="hidden" name="sublogin" value="1">
<input type="submit" value="Login"></td></tr>
<tr><td colspan="2" align="left"><br><font size="2">[<a href="forgotpass.php">Forgot Password?</a>]</font></td><td align="right"></td></tr>
<tr><td colspan="2" align="left"><br>Not registered? <a href="register.php">Sign-Up!</a></td></tr>
</table>
</form>

<?
}

/**
* Just a little page footer, tells how many registered members
* there are, how many users currently logged in and viewing site,
* and how many guests viewing site. Active users are displayed,
* with link to their user information.
*/
echo "</td></tr><tr><td align=\"center\"><br><br>";
echo "<b>Member Total:</b> ".$database->getNumMembers()."<br>";
echo "There are $database->num_active_users registered members and ";
echo "$database->num_active_guests guests viewing the site.<br><br>";

include("include/view_active.php");
?>


</td></tr>
</table>


</body>
</html>

try

 

<?php
include('include/session.php');
//connect
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("textgame") or die(mysql_error());

// start adding data
$active_users = $session->username;
mysql_query("INSERT INTO users_items (username, item_id) VALUES('$active_users', '4' ) ") 
or die(mysql_error());


?>

i see you added sessions.php into the code...i tried that too xD

i get this error

 

Warning: include(include/session.php) [function.include]: failed to open stream: No such file or directory in C:\wamp\www\test\game\items\hoe.php on line 2

 

Warning: include() [function.include]: Failed opening 'include/session.php' for inclusion (include_path='.;C:\php5\pear') in C:\wamp\www\test\game\items\hoe.php on line 2

i see you added sessions.php into the code...i tried that too xD

i get this error

 

Warning: include(include/session.php) [function.include]: failed to open stream: No such file or directory in C:\wamp\www\test\game\items\hoe.php on line 2

 

Warning: include() [function.include]: Failed opening 'include/session.php' for inclusion (include_path='.;C:\php5\pear') in C:\wamp\www\test\game\items\hoe.php on line 2

 

Thats because your in another folder and your trying to include it somewhere outside of the folder.

 

Whats the Root directory to your hoe.php files, and the include folder?

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.