Jump to content

Newbiephper

Members
  • Posts

    21
  • Joined

  • Last visited

    Never

Everything posted by Newbiephper

  1. ok thx for that i made the chagne like u said but nothing happened. still nothing appears. Total land: 200Free land: 200Number of Farms: Number of Homes: Number of Mines: is what it looks like as u can see no values appear for number of farms e.t.c. also total land is defined in script so maybe it is the table. im using the phpfreaks user script, just like at registration script it does this: [code] $sql = mysql_query("INSERT INTO users (first_name, last_name, email_address, username, password, info, signup_date) VALUES('$first_name', '$last_name', '$email_address', '$username', '$db_password', '$info2', now())") or die (mysql_error()); [/code] do i also need to add insert records for the other tables i.e. so when the user gets created it also creates 1st records for buildings table, research table.  i think then this will make use of the user id i have added to other tables like buildings e.t.c. as the user id then in all should be same??  am i right here.  only been doing php 2-3 days so its all new terrain.
  2. ahh good thx i fixed that error that came up a spelling mistake in my database, many thx. new problem now is that using code seen above. the number of buildings constructed should refresh so when user says build 30 farms (nfarms) it will say Number of Farms 30, but nothing is showing up. i know this is php help part but since i dont know if the problem is this or my databse ill post both. script above table used to create below [code] CREATE TABLE buildings (   userid int(25) NOT NULL auto_increment,   nfarms int(10) NOT NULL default '',   nmines int(10) NOT NULL default '',   nhomes int(10) NOT NULL default '',   PRIMARY KEY  (userid) ) TYPE=MyISAM COMMENT='buildings'; [/code] userid i was intending this to be same userid as in my users table.  otherwise how does my database store data for each individual user.  this is made primary key.  others are just number of farms, mines, e.t.c. for that user.  any help with data not showing appreciated.
  3. can any1 tell me why this comes up for my code Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in XXX/buildings.php on line 23. [code] <?php session_start(); //buildings.php //open database connections require_once('db.php'); if ($_POST['farm'] || $_POST['house'] || $_POST['mine']) { $farm = $_POST['farm'] > 0 ? $_POST['farm'] : 0; $house = $_POST['house'] > 0 ? $_POST['house'] : 0; $mine = $_POST['mine'] > 0 ? $_POST['mine'] : 0; $qry = "update buildings set nfarms = nfarms + " . $farm . " where userid='$userid', nhouse = nhouse + " . $house . " where userid='$userid', nfarm  = nfarm +  " . $farm . " where userid='$userid'"; $qry = mysql_query($qry); } //define planet size define("TLAND", 200); //get current number of buildings $qry="select nfarms,nhomes,nmines from buildings where userid='$userid'"; $qry = mysql_query($qry); $row = mysql_fetch_assoc($qry);  //line23 $nf = $row[nfarms]; $nh = $row[nhomes]; $nm = $row[nmines]; //check for free land sapce $fland = ((TLAND) - ($nf+$nh+$nm)); //output current data echo "Total land: " . TLAND; echo "Free land: " . $fland; echo "Number of Farms: " . $nf; echo "Number of Homes: " . $nh; echo "Number of Mines: " . $nm; //allow building form if ($fland>0) { ?> <form action="<?=$_SERVER['PHP_SELF']?>" method="post"> <input size="4" type="text" name="farm"> <input size="4" type="text" name="house"> <input size="4" type="text" name='mine'> <input type="submit" name="submit" value="Build"> <?php } else { echo "No free land to build upon"; } ?> [/code]
  4. ok i read the page 2x over and say cautions talk of globals but wasnt understanding it just as i havent understood any of that manual.  i read that since php 4 globals are off by default and will be completely removed  in php 6. so maybe im missing something but what do i replace register session with/how do i solve my problem? wow im more confused now than before :( [quote] If your script uses session_register(), it will not work in environments where the PHP directive register_globals  is disabled. [/quote] [quote] If you want your script to work regardless of register_globals, you need to instead use the $_SESSION array as $_SESSION entries are automatically registered. [/quote] does this mean its automatic? i.e. i dont need to do anything other than delete the waste code (i.e. register session)? i origianlly though the problem was with my code, particuarly bits like this [code] where userid='$userid' [/code]
  5. ok ill show the process first is Check User; [code] <? /* Check User Script */ session_start();  // Start Session include 'db.php'; // Conver to simple variables $username = $_POST['username']; $password = $_POST['password']; if((!$username) || (!$password)){ echo "Please enter ALL of the information! <br />"; include 'login_form.html'; exit(); } // Convert password to md5 hash $password = md5($password); // check if the user info validates the db $sql = mysql_query("SELECT * FROM users WHERE username='$username' AND password='$password' AND activated='1'"); $login_check = mysql_num_rows($sql); if($login_check > 0){ while($row = mysql_fetch_array($sql)){ foreach( $row AS $key => $val ){ $$key = stripslashes( $val ); } // Register some session variables! session_register('first_name'); $_SESSION['first_name'] = $first_name; session_register('last_name'); $_SESSION['last_name'] = $last_name; session_register('email_address'); $_SESSION['email_address'] = $email_address; session_register('special_user'); $_SESSION['user_level'] = $user_level; session_register('userid'); $_SESSION['userid'] = $userid; session_register('username'); $_SESSION['username'] = $username; mysql_query("UPDATE users SET last_login=now() WHERE userid='$userid'"); header("Location: header.php"); } } else { echo "You could not be logged in! Either the username and password do not match or you have not validated your membership!<br /> Please try again!<br />"; include 'login_form.html'; } ?> [/code] this is from phpfreaks i added two additional register session handles userid and username. this takes u to header.php : simple menu selection [code] <?php session_start(); echo "Welcome Commander". $_SESSION['username'] ." to you empire."; echo "Manage your kingdom:"; echo"<a href='buildings.php>Buildings</a>"; echo"<a href='research.php>Research</a>"; echo"<a href='shipyard.php>Shipyard</a>"; ?> [/code] so to the main part of site: the session should follow and maintain userid and username right? [code] <?php session_start(); //buildings.php //open database connections require_once('db.php'); if ($_POST['farm'] || $_POST['house'] || $_POST['mine']) { $farm = $_POST['farm'] > 0 ? $_POST['farm'] : 0; $house = $_POST['house'] > 0 ? $_POST['house'] : 0; $mine = $_POST['mine'] > 0 ? $_POST['mine'] : 0; $qry = "update buildings set nfarms = nfarms + " . $farm . " where userid='$userid', nhouse = nhouse + " . $house . " where userid='$userid', nfarm  = nfarm +  " . $farm . " where userid='$userid'"; $qry = mysql_query($qry); } //define planet size define("TLAND", 200); //get current number of buildings $qry="select nfarms,nhomes,nmines from buildings where userid='$userid'"; $qry = mysql_query($qry); $row = mysql_fetch_assoc($qry); $nf = $row[nfarms]; $nh = $row[nhomes]; $nm = $row[nmines]; //check for free land sapce $fland = ((TLAND) - ($nf+$nh+$nm)); //output current data echo "Total land: " . TLAND; echo "Free land: " . $fland; echo "Number of Farms: " . $nf; echo "Number of Homes: " . $nh; echo "Number of Mines: " . $nm; //allow building form if ($fland>0) { ?> <form action="<?=$_SERVER['PHP_SELF']?>" method="post"> <input size="4" type="text" name="farm"> <input size="4" type="text" name="house"> <input size="4" type="text" name='mine'> <input type="submit" name="submit" value="Build"> <?php } else { echo "No free land to build upon"; } ?> [/code] until finally they can logout (this again from php freaks) [code] <? session_start(); if(!isset($_REQUEST['logmeout'])){ echo "<center>Are you sure you want to logout?</center><br />"; echo "<center><a href=logout.php?logmeout>Yes</a> | <a href=javascript:history.back()>No</a>"; } else { session_destroy(); if(!session_is_registered('first_name')){ echo "<center><font color=red><strong>You are now logged out!</strong></font></center><br />"; echo "<center><strong>Login:</strong></center><br />"; include 'login_form.html'; } } ?> [/code] so why do my sessions not work for keeping userid and username for main section of site i.e. buildings.php.  is there some clash or am i going wrong somewhere.
  6. sry to use this post but it will get my point across $query = "UPDATE photos SET comments='$comment_tmp' WHERE id='91'; what would happen if you wanted to use the 'id' from one table to update data in tanother table. i.e. u have 2 tables user data (this contains id) table x ( you want to update data in this table for that particular user) thx any help
  7. thx toonmariner for the script changes and explanations ^^  off i go to code more now, hopefully ill manage a little longer before getting stuck :)
  8. reply thx ill try that with the little fix that GR made too.  just a few things: why is that if statement relating to the form at the top? the '?' is shorthand for an if statement? what does teh colon mean (:)? [quote] $farm = $_POST['farm'] > 0 ? $_POST['farm'] : 0; $qry = "update buildings set nfarms = nfarms + " . $farm . " [/quote] so in english what happens here is that if user submits a value posted to variable $farm then $qry transfers this value to databse.  im guessing " . $variable . " is standard convention for posted variables. thx for help, and it would be grate if you could answer my questions as whilst the script is great and im sure works, im trying to learn as i make so need to try and understand stuff as well.  everything else i understand, a little rusty on arrays but understand basic principle.  thx a lot again.
  9. hello again i was trying to write my first code with no aids and hit a dead end. the code basically consists of a display feature to display values (total land, free land and the 3 building types); then the form action(build new buildings), then when build submit it should send data to server for storage and also update page. my code at the moment looks like: [code] <?php //buildings.php //open database connections include 'db.php' //define planet size define("TLAND, 200") //get current number of buildings $nfarms="select nfarms from buildings"; $nhomes="select nhomes from buildings"; $nmines="select nmines from buildings"; $nf = mysql_query($nfarms) $nh = mysql_query($nhomes) $nm = mysql_query($nmines) //check for free land sapce $fland = ((TLAND) - ($nfarms+$nhomes+$nmines)) //output current data echo "Total land: " . TLAND; echo "Free land: " . $fland; echo "Number of Farms: " . $nf; echo "Number of Homes: " . $nh; echo "Number of Mines: " . $nm; //allow building form if ($fland>0) { <form action="<?=$_SERVER['PHP_SELF']?>" method="post"> echo "<input size=4 type=text name='farm'>" echo "<input size=4 type=text name='house'>" echo "<input size=4 type=text name='mine'>" <input type="submit" name="submit" value="Build"> } else { "echo "No free land to build upon"; } if ($_POST['farm'] || $_POST['house'] || $_POST['mine']) { if ($_POST['farm']) { $update1 = "update buildings set nfarms = nfarms + 1 "; [/code] my problem is how to proceed because i dont wanna just add a fixed value [quote] $update1 = "update buildings set nfarms = nfarms + 1 "; [/quote] where that 1 is i need the value that was submitted by the user.  this is why im stuck at moment.  any help greatly appreciated.  also if ur good u might just wanna check rest of code since its first try and probably has a zillion errors. thx a lot
  10. thx a million Crayon Violent.  this has provided me with tonnes of information and topics to read over.  eternally grateful.  the comments are a great help.  its an amazing thing to see it working.  next few days ill experiment with it and read over some things that came up in topic discussiona nd hopefully i'll be that little wiser for it. thx again to you and redarrow.
  11. my code is what u see above.  so yeah i think so. i tried it ur way, btw thx for your pm (im sorry about that if u like i will copy ur work over to forum for other people to view and learn as it was very informative). anyways based on a 1 page idea with submit button have a look at this and see how many problems i got :) [code] <html> <head> <title>Research Test</title> </head> <body> <h1>Carry out More Research</h1> <?php $db=mysql_connect("hostname","username","password"); mysql_select_db("dbname",$db);   if ($_POST['submit']) { $query="UPDATE research SET points='$points+1'"; $result=mysql_query($query);   } ?> <form action = '<?php $_SERVER['PHP_SELF']; ?>' method = 'post'>   <input type = 'submit' value='research' name='research'> </form> <p></p> <h2>Current Research level</h2> $query2="SELECT * from research"; $result=mysql_query($query2); while($record=mysql_fetch_assoc($result)) { while(list($key,$value)=each($record)) { echo "<br> $key : $value <br>"; } } ?> </body> </html> [/code] thx to any1 thats helped out on this problem.  i feel if i can get one workable example i can learn and modify it to fulfil many ideas i have.
  12. well page loads with no errors now and when i click to research it says database updated but still doesnt show current value on main page.
  13. Parse error: parse error, unexpected '{' for while($record=mysql_fetch_assoc($result) { wish i understood this last bit i might be able to work out problem myself.  cant wait for my php book to get here lol.  i wonder if ill get this example working before it does =). who said php was easy ^^
  14. thx for update.. new error comes up Warning: Variable passed to each() is not an array or object in XXX on line 23 while(list($key,$value)=each($result)) { maybe this is to do with my not having a key in the research table i only have 1 field which is points.  also i dont have userid since i dont have members area.  im just testing basic so i always delete [code] where userid='$userid [/code] thx for help so far.  if only it will display then that will be great.
  15. k ive tested them out and have 2 problems first being mysql_select_db("whatever",$db); -- what is whatever relating to the name of the table/field?? second is on page 1 i get Parse error: parse error, unexpected '{' for [code] { echo "<br> $key : $val <br>"; } [/code] and also what is that last bit trying to do?
  16. Ok the whole code from page 1: [code]<a href='research2.php?&cmd=update'>update</a> <?php @mysql_connect('localhost', 'username','password') or die('Could not connect to mysql'); @mysql_select_db(dbname); $result = mysql_query("SELECT * FROM research", dbname); printf("Research Level:", mysql_result($result)); ?> [/code] from page 2: [code] <?php @mysql_connect('localhost', 'username','password') or die('Could not connect to mysql'); @mysql_select_db(dbname); $points=($_POST['points']); if($_GET['cmd']=='update'){ $sql="update players set points=points+$points"; }else{ echo "sorry problem no update"; } ?> [/code] thats the whole code i took it down to bare essentials until i fully understand how everything works.
  17. Crayon Violent: yes i wanted a submit button that carried out the action.  But this was because this was the only thing my knowledge would understand.  So when a new way was shown im going to try that because that uses a few features i havent seen/used before.  appreciate your reply and will try that too and see what works best for me.  im guessing it will as this is what i originally set out for, it just seems slightly mroe complex than Redarrows. Redarrow:  thx i like your idea a lot and im trying it now.  no errors this way yet it seems to send data to my sql table.  but clearly im too much of a noob because im not sure if its updating correct.  maybe to do with my sql.  also i cant get my display function working so can see in real time if it updates properly. table:research field:points type:mediumint value: i originally had 0 -> its now 9 so im guessing it must be updating. not null default 0 No index defined! shows up.  Im guessing this is because i didnt set points to primary. if i could get my data to display properly the new value i could if it updates correct. currently on page1 i have <a href='research2.php?&cmd=update'>update</a> and then below this i tried to get current data value displaying: Errors: Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in "my address"/research.php on line 19 Warning: Wrong parameter count for mysql_result() in "my address"/research.php on line 21 line 19: $result = mysql_query("SELECT * FROM research", dbname); line 21: printf("Research Level:", mysql_result($result)); <--im thinking in here should be ($result,X,X) although i dont know what X is :) appreciate your time and effort.  any help appreciated.  currently testing both methods.  but problems above are limiting progress.
  18. [quote] <? $points=($_POST['points']); if($_POST['submit']){ $sql="update players set points=points+$points where userid='$userid'"; } ?> [/quote] this post submit thing.  im a lil confused sry.  i understand the $sql line.  how would i implement this with a submit button.  also based on this can all this code be placed on 1 page.  i.e. after submitting it updates database and refreshs displayed value. how i was trying to display data: @mysql_connect('localhost', 'username','password') or die('Could not connect to mysql'); @mysql_select_db(dbname); $result = mysql_query("SELECT * FROM research", dbname); printf("Research Level:", mysql_result($result)); --------- i understand that post is a function and the if statement occurs when submit is pressed.  How do i link the submit button to that code, i was trying with form actions but to no use. Appreciate the feedback as already ive learned a lot about updating fields.  just got a few creases to smooth out with my understanding thx all so far. 
  19. many thx i do believe thats exactly what im after.  ty both for helping.  hopefully as i learn more my posts will become slightly clearer thanks for fast reply and help.
  20. thx for reply but will this format take into account data in the field before. so say like research value is 0. it will look and see 0 and update process will add +1. And so it will do basic math and update 0+1=new value =1 e.t.c. next update goes new value 1+1 =2 and so forth thats what im trying to do.  maybe it requires something else other than update i dunno.  but thx for reply.
  21. Hi im trying to teach myself php in the hope sof been able to make my own php game someday. how many people have said that i know but i know i will b able to. at present i know how to create tables in mysql, and write simple php prgrams. have read tutorials for all php/mysql basics. i have read tutorials on how to send data to a database and how to retrieve it. The problem i have is that im trying to make my learning practical. So i am trying to designa simple research page for a game. How process should work. (as i think based on basic knowledge) data from table of database is accessed user Clicks research button (part of html form) -> runs php function on seperate php page (i.e. researchprocess.php)-> this finds teh records that contains research level number(i.e. 0) ->updates the record by +1 because of submit button action -> this new value is then displayed in main research page sry if im not clear , i try to be, but any help on this would be greatly appreciated. At the moment im just sitting here with a lot of script examples and trying to work one out but i cant work out how to update a numerical record so that it takes into account what was there before and adds +1 to it. i know how to update a record if i wanted to say completely wipe the record and replace it but that isnt my aim. Tried searching for info on web and found this forum so hopefully someone can help. BTW nice forum and hello, i might be on here a lot from now on.
×
×
  • 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.