Jump to content


Photo

php help send/update/display


  • Please log in to reply
29 replies to this topic

#1 Newbiephper

Newbiephper
  • Members
  • PipPip
  • Member
  • 21 posts

Posted 09 July 2006 - 01:02 PM

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.

#2 ShogunWarrior

ShogunWarrior
  • Members
  • PipPipPip
  • Advanced Member
  • 528 posts
  • LocationIreland

Posted 09 July 2006 - 01:07 PM

Not completely sure on your idea here, but to update you use this format:
"UPDATE {table} SET {field}={value} WHERE {PrimaryKey} = {value} "

So, for example:
"UPDATE research SET level=0 WHERE PRIMARY_KEY = 4"

:)
<a href="http://www.daviddora...nmedia.com/">My New Site/Blog</a> | <a href="http://www.daviddora...m/check/">Check your page for broken links/images/scripts</a>

Zend Certified Engineer
Follow me on Twitter: http://twitter.com/davidd

#3 Newbiephper

Newbiephper
  • Members
  • PipPip
  • Member
  • 21 posts

Posted 09 July 2006 - 01:17 PM

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.

#4 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 09 July 2006 - 01:17 PM

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'";

Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#5 Newbiephper

Newbiephper
  • Members
  • PipPip
  • Member
  • 21 posts

Posted 09 July 2006 - 01:22 PM

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.

#6 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 09 July 2006 - 01:31 PM



Is this the same out come cheers.

<?
$points=($_POST['points']);

$points=$points+1;

if($_POST['submit']){
$sql="update players set points='$points' where userid='$userid'";
}
?>
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#7 ShogunWarrior

ShogunWarrior
  • Members
  • PipPipPip
  • Advanced Member
  • 528 posts
  • LocationIreland

Posted 09 July 2006 - 01:34 PM

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:
<?
$points=($_POST['points']);

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

This will add the number of points in $_POST['submit'] 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 userid=$userid with no single quotes.
<a href="http://www.daviddora...nmedia.com/">My New Site/Blog</a> | <a href="http://www.daviddora...m/check/">Check your page for broken links/images/scripts</a>

Zend Certified Engineer
Follow me on Twitter: http://twitter.com/davidd

#8 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 09 July 2006 - 01:35 PM

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.


Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#9 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 09 July 2006 - 01:39 PM

Thanks mate that was what i wanted to know cheers.


Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#10 Newbiephper

Newbiephper
  • Members
  • PipPip
  • Member
  • 21 posts

Posted 09 July 2006 - 01:47 PM

<?
$points=($_POST['points']);

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


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. 

#11 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 09 July 2006 - 01:54 PM

1 on page bla bla bla
<a href='bla.php?&cmd=update'>update</a>
2.bla.php
<?
$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");
}
?>

Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#12 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 09 July 2006 - 02:00 PM

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:

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

Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#13 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 09 July 2006 - 02:05 PM


I thort this was a securty risk
<?php $_SERVER['PHP_SELF']; ?>

better to use action=" " or the page.php name.

ok may be wrong sorry.
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#14 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 09 July 2006 - 02:12 PM

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.
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#15 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 09 July 2006 - 02:16 PM

ok sorry

and the sec risk was using PHP_SELF wrongly but yours is correct as always.
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#16 Newbiephper

Newbiephper
  • Members
  • PipPip
  • Member
  • 21 posts

Posted 09 July 2006 - 02:23 PM

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.



#17 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 09 July 2006 - 02:30 PM

Can you post the whole code please cheers.

page 2

<?
$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";

}

?>


Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#18 Newbiephper

Newbiephper
  • Members
  • PipPip
  • Member
  • 21 posts

Posted 09 July 2006 - 02:44 PM

Ok the whole code from page 1:

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

?>

from page 2:

<?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";
}

?>

thats the whole code i took it down to bare essentials until i fully understand how everything works.

#19 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 09 July 2006 - 02:48 PM

page 1
<?

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

}

?>

page 2

<?
$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";

}

?>

Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#20 Newbiephper

Newbiephper
  • Members
  • PipPip
  • Member
  • 21 posts

Posted 09 July 2006 - 03:05 PM

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
{

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

}

and also what is that last bit trying to do?





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users