Jump to content

php help send/update/display


Newbiephper

Recommended Posts

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.
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

okay i think what you are asking here is how to do a sql query to increment a field by one?

update tablename set fieldname=fieldname+1 where whatever='whatever'

for instance, if i had a field called 'points' in a table called 'players' and i wanted to base this query off the user's id that i already had in a variable (and also assuming that user id column was called userid

$userid = '10'; //example
$sql="update players set points=points+1 where userid='$userid'";
Link to comment
Share on other sites

This will only work if the form contains the previous amount of points which would mean you would have to query the row and fill it in the form on the previous page, this seems redundant.
This should work fine:
[code]<?
$points=($_POST['points']);

if($_POST['submit']){
$sql="update players set points=points+$points where userid='$userid'";
}
?>[/code]

This will add the number of points in [b]$_POST['submit'][/b] to the selected row.

PS: unless a field is a string you shouldn't put single quotes around the value.
So, userid is probably a number which means you should have [b]userid=$userid[/b] with no single quotes.
Link to comment
Share on other sites

no redarrow, it's not.  your code assumes that the user is inputing the amount of points from a form, then for some reason adding 1 to it, and then replacing whatever was in the points field in the database with that new number.  so for example, if points was 10 in the database before, and the user enters in 20 in the form, that 10 would now be 21, whereas it should be 11.

Link to comment
Share on other sites

[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. 
Link to comment
Share on other sites

1 on page bla bla bla
[code]
<a href='bla.php?&cmd=update'>update</a>
[/code]
2.bla.php
[code]
<?
$points=($_POST['points']);

if(!$_GET['cmd']=='update'){

$sql="update players set points=points+$points where userid='$userid'";

header("location: to members_page.php");

}else{

header("location: to back to entery page.php");
}
?>
[/code]
Link to comment
Share on other sites

redarrow: your script has nothing to do with using a form. you simply made some link to click. and based it off the GET method. 

Newbiephper: you need to explain what you are doing here.  If you simply want to click on a link and it updates this one value, then redarrow's code would actually be useful. However, if you are using a form that the user is submitting some other variables, and you want this extra field to be incremented by 1 when they submit the other information, then you would do something like this:

[code]
<?php
  if ($_POST['submit']) {
      //sanitize your variables and do your sql query stuff here
  }
?>

<form action = '<?php $_SERVER['PHP_SELF']; ?>' method = 'post'>
  <input type = 'text' name='something'> enter something here
  <input type = 'submit' value='submit' name='submit'>
</form>
[/code]
Link to comment
Share on other sites

how is that a security risk? i haven't read anything about that being a security risk, but i'm not an expert nor  am i able to read everything out there all at once.  do you have a link to an article about that or something? but anyways, that's not the point. the point was that your code doesn't fit the problem.
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

Can you post the whole code please cheers.

page 2

[code]
<?
$db=mysql_connect("localhost","name","password");

mysql_select_db("whatever",$db);

if($_GET['cmd']=='update') {

$query="UPDATE research SET points='$points+1' ";

$result=mysql_query($query);

echo"Database Updated";

}else{

echo"Sorry no update";

}

?>
[/code]

Link to comment
Share on other sites

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.
Link to comment
Share on other sites

page 1
[code]
<?

echo"<a href='wahtever.php?&cmd=update'>update</a>";

$db=mysql_connect("localhost","name","password");

mysql_select_db("whatever",$db);

$query="SELECT * from research where userid='$userid";

$result=mysql_query($query);

while(list($key,$value)=each($result) {

echo "<br> $key : $val <br>";

}

?>
[/code]

page 2

[code]
<?
$db=mysql_connect("localhost","name","password");

mysql_select_db("whatever",$db);

if($_GET['cmd']=='update') {

$query="UPDATE research SET points=$points+1 where userid='$userid'";

$result=mysql_query($query);

echo"Database Updated";

}else{

echo"Sorry no update";

}

?>
[/code]
Link to comment
Share on other sites

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?
Link to comment
Share on other sites

1st problam was a typo the second is the echo of key value of all the research database ok.


page 1
[code]
<?

echo"<a href='wahtever.php?&cmd=update'>update</a>";

$db=mysql_connect("localhost","name","password");

mysql_select_db("database_name",$db);

$query="SELECT * from research where userid='$userid";

$result=mysql_query($query);

while(list($key,$value)=each($result)) {

echo "<br> $key : $value <br>";

}

?>
[/code]

page 2

[code]
<?
$db=mysql_connect("localhost","name","password");

mysql_select_db("database_name",$db);

if($_GET['cmd']=='update') {

$query="UPDATE research SET points=$points+1 where userid='$userid'";

$result=mysql_query($query);

echo"Database Updated";

}else{

echo"Sorry no update";

}

?>
[/code]
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

edited sorry i missed a while loop lol....................


[code]<?

echo"<a href='wahtever.php?&cmd=update'>update</a>";

$db=mysql_connect("localhost","name","password");

mysql_select_db("database_name",$db);

$query="SELECT * from research";

$result=mysql_query($query);

while($record=mysql_fetch_assoc($result){

while(list($key,$value)=each($record)) {

echo "<br> $key : $value <br>";
}
}
?>
[/code]
Link to comment
Share on other sites

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