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

?>

Link to comment
Share on other sites

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||

Link to comment
Share on other sites

 

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'

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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


?>

Link to comment
Share on other sites

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


?>

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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


?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

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.